相信每次新建maven项目要打包成可执行jar都很让人崩溃,百度搜索都是用啥maven-assembly-plugin
,maven-jar-plugin
什么的啊,然后自己搞了很久,还是不行,特别是自己加了很多别的依赖包,有时候还会导致依赖包冲突。
下面我这里彻底的解决下吧,大家直接拷贝进去修改下就可以完美的解决。
项目为SpringBoot项目
这个是最简单方便的,因为springboot封装好了插件,直接在pom.xml加入如下内容即可,也不需要指定主类。
<build>
<finalName>suibibk</finalName>
<plugins>
<!-- 可以将项目打包成可执行jar包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<finalName>suibibk</finalName>
是指定生成jar包的名称。
非springboot项目
如果是普通的项目,spring或者其他小程序项目,想要打包成一个可执行jar,只要在pom.xml加入如下内容即可。
<build>
<finalName>wordcount</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<!-- 执行package的phase -->
<phase>package</phase>
<!-- 为这个phase绑定goal -->
<goals>
<goal>shade</goal>
</goals>
<configuration>
<!-- 过滤掉以下文件,不打包 :解决包重复引用导致的打包错误-->
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<!-- 打成可执行的jar包 的主方法入口-->
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.suibibk.App</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
大家只需要拷贝进去,指定filename和主类路径即可,不需要考虑其他的,或者进行任何删除修改。
maven install即可在target看到jar文件。