最近用mvn打包jar包发现,总是几十kb,换个环境运行就会报错。能够确保jar包换个环境依旧能跑起来,就需要把所有依赖一起打进去,默认不配置mvn的pom文件是不会把所有依赖打进去的。
正常情况下不知道怎么编辑pom.xml,不妨试一下这个命令:
mvn help:effective-pom
编辑pom.xml文件,在<build><plugins><plugin>段里加上一个maven-assembly-plugin插件即可。如果没有<build><plugins><plugin>,新建就行。打完包后,target目录下会有两个jar文件,一个是包含了依赖的jar文件,一个是没有依赖的文件。
打包命令:
mvn clean compile package
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!--这里配置打包的java版本-->
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<!--给jar包起的别名-->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!--添加项目中主类-->
<mainClass>com.xxxx.xxx.xx.x</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>