1、maven-jar-plugin(默认的打包方式)
需设置Main入口,在pom.xml中添加如下配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>package.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
注意:此方法打出的Jar包不包含依赖的其他Jar包,可能会报类找不到的异常。
2、maven-shade-plugin(推荐)
使用方法:把下面的插件配置加入pom.xml中,修改mainClass。再使用mvn package命令打包即可。生成的jar包在target目录中。
<span style="white-space:pre"> </span><plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.wind.option.server.StartUp</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>
shade插件默认是能把配置文件也一起打到jar包中的,如果用mvnclean package会打不进来,在Eclipse里面clean然后用mvn package是可以的。
不知道为啥,暂时记一下。。。
3、maven-assembly-plugin
没用过,暂时记一下。用法与2相同。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.mkyong.core.utils.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
4、maven-onejar-plugin(没用过)
注意:
如果出现找不到Spring配置项的问题,可参考
http://blog.youkuaiyun.com/bluishglc/article/details/7596118
打Jar包时,跳过测试代码的配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>