I have a spring boot project based on maven.
I have to include a local jar that provided by another team.So I follow the accepted answer from :How to include local jar files in Maven project
So I use:
<dependency>
<groupId>com.netease</groupId>
<artifactId>thrift</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/prome-thrift-client-1.0.0.jar</systemPath>
</dependency>
That works fine at the IDEA. But when I tried to run the package jar with:
mvn clean package
java -jar target/prome-data.jar
Stacktrace is like:
....
....
Caused by: java.lang.ClassNotFoundException: com.netease.thrift.client.ThriftRpcClient
Is there anything I am missing
I have a similar issue today, and stuck me half day to fix it.
For most case, using below is fine for developing.
<dependency>
<groupId>com.netease</groupId>
<artifactId>thrift</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/prome-thrift-client-1.0.0.jar</systemPath>
</dependency>
If you put the jar to a correct path, then it's fine to run in both IDEA and Eclipse.
After you deploy the jar to a server or run the jar locally, then it may throws ClassNotFoundException
.
If you are using spring-boot, you still need below plugin:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
After run mvn clean package
, then you can find the jar under /BOOT_INF/lib
.
Between, if your package is war, then you still need this plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<!-- <targetPath>WEB_INF/lib/</targetPath> just for none spring-boot project -->
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
-----------------Another Way--------------------
You can using this plugin to replace maven-war-plugin
:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<extdirs>${project.basedir}/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
And add the resource:
<resources>
<resource>
<directory>lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<targetPath>BOOT-INF/classes/</targetPath>
</resource>
</resources>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With