1. 使用maven-assembly-plugin插件打包
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>xxx.xxx.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
打包方式:mvn package assembly:single
打包后会在target目录下生成一个xxx-jar-with-dependencies.jar文件,这个文件不但包含了自己项目中的代码和资源,还包含了所有依赖包的内容。所以可以直接通过java -jar来运行。
由于加了配置<phase>package</phase>
、<goal>single</goal>
,可以直接通过mvn package
来打包,无需assembly:single
。这个配置表示在执行package打包时,执行assembly:single,所以可以直接使用mvn package打包。
这种方式会把第三方jar的代码解压后打包到xxx-jar-with-dependencies.jar中。
2. 使用maven-jar-plugin和maven-dependency-plugin插件打包
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>xxx.xxx.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
maven-jar-plugin
用于生成META-INF/MANIFEST.MF
文件的部分内容,<mainClass>
指定MANIFEST.MF
中的Main-Class
,<addClasspath>
会在MANIFEST.MF
加上Class-Path
项并配置依赖包,<classpathPrefix>
指定依赖包所在目录。
maven-dependency-plugin
插件用于将依赖包拷贝到指定的位置,即lib目录下。
配置完成后,通过mvn package指令打包,会在target目录下生成jar包,并将依赖包拷贝到target/lib目录下。
这种方式生成jar包只包含项目本身的代码、资源,所有的依赖包在lib目录中,运行时jar包必须和lib在同一个目录中。
3. 使用maven-shade-plugin插件打包
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>xxx.xxx.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
执行mvn package
后会在target目录下会生成两个jar包,xxx.jar和xxx-shaded.jar。xxx- shaded.jar和maven-assembly-plugin一样,包含了所有依赖,所以可以直接运行。
shade插件还有其它功能配置:
将部分jar包添加或排除
<executions>
<execution>
<configuration>
<artifactSet>
<excludes>
<exclude>jmock:*</exclude>
<exclude>*:xml-apis</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
<exclude>log4j:log4j:jar:</exclude>
</excludes>
<includes>
<include>junit:junit</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
将依赖jar包内部资源添加或排除
<executions>
<execution>
<configuration>
<filters>
<filter>
<artifact>junit:junit</artifact>
<includes>
<include>junit/framework/**</include>
<include>org/junit/**</include>
</includes>
<excludes>
<exclude>org/junit/experimental/**</exclude>
<exclude>org/junit/runners/**</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
自动将所有不使用的类排除
<executions>
<execution>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
将依赖的类重命名并打包进来 (隔离方案)
<configuration>
<relocations>
<relocation>
<pattern>io.netty</pattern>
<shadedPattern>shaded.io.netty</shadedPattern>
<excludes>
<exclude>org.appace</exclude>
</excludes>
</relocation>
</relocations>
</configuration>
如果项目中用到了Spring Framework,将依赖打到一个jar包中,运行时会出现读取XML schema文件出错。原因是Spring Framework的多个jar包中包含相同的文件spring.handlers
和spring.schemas
,如果生成一个jar包会互相覆盖。为了避免互相影响,可以使用AppendingTransformer来对文件内容追加合并:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.xxg.Main</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>