先看官方文档:http://maven.apache.org/plugins/maven-assembly-plugin/index.html maven-assembly-plugin可以将依赖的第三方jar包打包到jar中,这样方便我们发布可执行的jar包。
用法部分的官方文档:http://maven.apache.org/plugins/maven-assembly-plugin/index.html
<plugin>
<artifactId>maven-assembly-pluginartifactId>
<version>3.1.0version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependenciesdescriptorRef>
descriptorRefs>
configuration>
<executions>
<execution>
<id>make-assemblyid>
<phase>packagephase>
<goals>
<goal>singlegoal>
goals>
execution>
executions>
plugin>
具体工程代码在这里。欢迎fork加星,谢谢!
我的pom文件如下
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.yqgroupId>
<artifactId>MavenPluginDemoartifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.0.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.1.33version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.5version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.7.0version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.7.0version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-spring-webartifactId>
<version>2.7.0version>
dependency>
dependencies>
<repositories>
<repository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/milestoneurl>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
repositories>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-pluginartifactId>
<version>3.1.0version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependenciesdescriptorRef>
descriptorRefs>
configuration>
<executions>
<execution>
<id>make-assemblyid>
<phase>packagephase>
<goals>
<goal>singlegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
执行mvn package
D:\E\workspaceGitub\springboot\MavenPluginDemo>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.yq:MavenPluginDemo >-----------------------
[INFO] Building MavenPluginDemo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ MavenPluginDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ MavenPluginDemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 4 source files to D:\E\workspaceGitub\springboot\MavenPluginDemo\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ MavenPluginDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\E\workspaceGitub\springboot\MavenPluginDemo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ MavenPluginDemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.20.1:test (default-test) @ MavenPluginDemo ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ MavenPluginDemo ---
[INFO] Building jar: D:\E\workspaceGitub\springboot\MavenPluginDemo\target\MavenPluginDemo-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-assembly-plugin:3.1.0:single (make-assembly) @ MavenPluginDemo ---
[INFO] Building jar: D:\E\workspaceGitub\springboot\MavenPluginDemo\target\MavenPluginDemo-1.0-SNAPSHOT-jar-with-dependencies.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.613 s
[INFO] Finished at: 2018-08-19T19:45:56+08:00
[INFO] ------------------------------------------------------------------------
D:\E\workspaceGitub\springboot\MavenPluginDemo>
此时执行>java -jar MavenPluginDemo-1.0-SNAPSHOT-jar-with-dependencies.jar
会报错误:MavenPluginDemo-1.0-SNAPSHOT-jar-with-dependencies.jar中没有主清单属性
修改pom文件中maven-assembly-plugin的设置
maven-assembly-plugin
3.1.0
true
lib/
com.yq.WebUserApplication
jar-with-dependencies
make-assembly
package
single
注意: **
**
本文示例代码是个spring boot工程,因此一般使用spring-boot-maven-plugin最适合。
org.springframework.boot
spring-boot-maven-plugin
关于spring-boot-maven-plugin请参考https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html
本文使用了maven-assembly-plugin,生成的jar启动时报如下错误。导致项目无法正常运行(这是因为我们的项目是springboot的项目,如果是普通的以main函数所在类启动的,不涉及springboot特有东西的不会出现如下错误)
Caused by: java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
at org.springframework.util.Assert.notEmpty(Assert.java:450) ~[MavenPluginDemo-1.0-SNAPSHOT-jar-with-dependencies.jar:na]