最近搞电子签章的项目,依赖第三方的sdk,未发布到maven,采取本地jar的方式进行依赖,
如下:
<dependency>
<groupId>com.qiyuesuo.sdk</groupId>
<artifactId>sdk-java</artifactId>
<version>3.3.9</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/sdk-java-3.3.9.jar</systemPath>
</dependency>
本地包目录为:
打包后,运行,报错:
Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.whwater.signature.service.impl.BizSignatoryServiceImpl] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@31cefde0]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:507) ~[spring-core-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:404) ~[spring-core-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:389) ~[spring-core-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:248) ~[spring-beans-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]
... 26 common frames omitted
Caused by: java.lang.NoClassDefFoundError: com/qiyuesuo/sdk/v2/http/FileItem
at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_181]
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_181]
at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_181]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:489) ~[spring-core-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
... 29 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.qiyuesuo.sdk.v2.http.FileItem
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_181]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_181]
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:92) ~[app.jar:0.0.1-SNAPSHOT]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_181]
... 33 common frames omitted
原因是因为打包后,本地的jar没有打包到正确的lib目录,导致找不到
解决方法如下:
在pom中plugin的spring-boot-maven-plugin下
添加配置:
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--本地依赖的system作用域包打包到项目jar -->
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
includeSystemScope作用是maven打包时会将外部引入的jar包(比如在根目录下或resource文件下新加外部jar包)打包到项目jar,在服务器上项目才能运行。
不加此配置,本地可以运行,因为本地可以在lib下找到外部包,但是打包后的jar中是没有的。