maven打包项目时,一般有三个打包plugin,分别是
- maven-jar-plugin
- maven-shade-plugin
- maven-assembly-plugin
其中maven-jar-plugin是maven默认打包方式,用来创建project jar。maven-shade-plugin用来打包可执行包。maven-assembly-plugin支持定制化打包方式。
推荐项目使用maven-assembly-plugin打包项目。maven-assembly可以使用 descriptorRefs(官方提供的定制化打包方式),官方提供的 descriptorRef 有 bin, jar-with-dependencies, src, project。也可以使用 descriptors,指定打包文件 xx/xx/assembly.xml,在该配置文件内指定打包操作。
用maven 打包时运用 java -jar xxx.jar运行jar包时,需要在pom.xml指定启动的Main class,具体是通过配置一个manifest标签来配置Main函数的入口。
使用maven打包工具需要引入spring-boot-maven-plugin,一般pom.xml中不同时引入两种打包plugin。
SpringBoot项目的部分pom.xml如下。
<?xml version="1.0" encoding="UTF-8"?>
<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">
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<mainClass>main类</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>main类</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>