插件

指定打包版本

1
2
3
4
5
6
7
8
9
10
11
12
<plugins>
<plugin>
<!-- 编译时使用 JDK 版本 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>

SpringBoot 打包插件

1
2
3
4
5
6
7
8
9
10
11
<plugins>
<plugin>
<!-- 设置工程主类路径 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.kewen.demo.SpringBootApp</mainClass>
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>

Assembly 全量打包插件

在普通 Maven 工程打包时默认仅会编译工程中新建的 java 文件并存储其 .class 文件,对于 POM 文件中引用的第三方依赖并不会一同打包。

Assembly 翻译为组装,通过 assembly 插件即可将 POM 配置中的所有依赖一同打包编译至 JAR 文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<plugin>  
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- Set jar file name -->
<finalName>${project.artifactId}-${project.version}-all</finalName>
<appendAssemblyId>false</appendAssemblyId>
<attach>false</attach>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
<executions>
<!--execution 标签定义了 assembly 插件的作用阶段,如这里设置了在 Maven package 即打包阶段生效。-->
<execution>
<!-- Set effect phase -->
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

Shade

Shade 插件的功能更为强大,其提供了两个功能:

  • 第一个即与 assembly 类似可实现依赖的打包编译,与 assembly 不同的是 Shade 提供了更灵活的执行策略,可指定需要打包编译的依赖集合。
  • 实现包的重命名功能,我们都知道 Maven 并不允许在一共工程中同时引入单个依赖的不同版本,而通过 Shade 插件即可实现二次包装从而绕开该限制。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<plugin>  
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<!-- Working phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<minimizeJar>true</minimizeJar>
<!-- Defined what dependencies to pull into the uber JAR -->
<artifactSet>
<!--通过 includes 标签可以指定需要一同打包编译的第三方依赖。-->
<includes>
<include>com.fasterxml.jackson.core:jackson-core</include>
</includes>
</artifactSet>
<!-- 通过 relocations 标签即可实现模块的重命名功能。 Rename the package -->
<relocations>
<relocation>
<!-- Old name 需要重命名的模块包-->
<pattern>com.fasterxml.jackson.core</pattern>
<!-- New name 重命名后的模块名-->
<shadedPattern>com.ibudai.fasterxml.jackson.core</shadedPattern>
</relocation>
</relocations>
<!-- 可以实现非必要文件的排除,如常见的协议文件等,可通过文件名或类型实现匹配 Exclude the file that didn't want -->
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/license/**</exclude>
<exclude>META-INF/*</exclude>
<exclude>LICENSE</exclude>
<exclude>NOTICE</exclude>
</excludes>
</filter>
</filters>
</configuration>
</plugin>

编译配置

文件排除

排除不打包相关的文件路径,比如配置文件不打包,在服务器上配置,这样安全

1
2
3
4
5
6
7
8
9
<resources>
<resource>
<!-- 设置编译去除 yml 配置文件 -->
<directory>src/main/resources</directory>
<excludes>
<exclude>application.yml</exclude>
</excludes>
</resource>
</resources>

引用