前言:maven-将依赖的 jar包一起打包到项目 jar 包中
有时候在项目开发中,需要很多依赖的 jar 包,其中依赖的 jar 包也会依赖其他的 jar 包,导致jar 包的管理很容易不全。
如果使用Maven默认的打包方式的话,打成项目jar包后,会导致其他第三方jar包无法找到的异常信息,报错ClassNotFoundException。
以下有两种方法可以规避这个问题。
一、在pom.xml 文件中配置,将依赖的 jar 包放到指定的文件夹中
在 pom.xml 的 build-plugins 节点上添加以下配置即可:
这里相当于将第三方依赖在打包的时候,将第三方jar包放到了target下指定的lib文件夹里,但是没有打到jar包内。
org.apache.maven.plugins
maven-dependency-plugin
3.0.1
copy-dependencies
package
copy-dependencies
${project.build.directory}/lib
false
false
true
二、将项目打jar 包时,将依赖的 jar 包一起并入到项目 jar 包中
在 pom.xml 的 build-plugins 节点上添加以下配置即可:
这里相当于将第三方jar包一起并入到项目jar包中去了。
org.apache.maven.plugins
maven-assembly-plugin
3.0.0
jar-with-dependencies
make-assembly
package
single
三、执行Maven打包命令即可
完整pom文件:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.dream.invengo
license-client
0.0.1-SNAPSHOT
commons-codec
commons-codec
1.10
commons-io
commons-io
2.4
com.google.code.gson
gson
2.8.2
org.slf4j
slf4j-log4j12
1.7.21
license-client
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
UTF-8
org.apache.maven.plugins
maven-dependency-plugin
3.0.1
copy-dependencies
package
copy-dependencies
${project.build.directory}/lib
false
false
true
org.apache.maven.plugins
maven-assembly-plugin
3.0.0
jar-with-dependencies
make-assembly
package
single
run as maven : clean maven执行即可