Spring Snippet - Spring 프로젝트에 jar를 직접 include 시켰을 때 발생하는 NoClassDefFoundError 해결

상황

  • Java apns 라이브러리인 javapns-jdk16을 아래와 같이 직접 프로젝트를 include 시도함
<dependency>
    <groupId>com.github.fernandospr</groupId>
    <artifactId>javapns-jdk16</artifactId>
    <version>2.4.01</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/javapns-jdk16-2.4.01.jar</systemPath>
</dependency>
  • NoClassDefFoundError 에러 발생
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'apnsTask': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.togethers.service.time.service.ApnsService com.togethers.service.time.task.ApnsTask.apnsService; nested exception is java.lang.NoClassDefFoundError: javapns/notification/Payload
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
  • 해결책
    • maven-war-plugin 을 아래와 같이 추가함
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <webResources>
            <resource>
                <directory>lib</directory>
                <targetPath>WEB-INF/lib/</targetPath>
                <!-- 스프링 부트는  <targetPath>BOOT-INF/lib/</targetPath> -->
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>