Maven 打包Plugins集成

目标

  • jar冲突检测
  • 源码编译
  • 源码生成jar
  • pom依赖jar拷贝
  • spring boot可执行jar生成
  • 脚本动态生成
  • 生成目录和打包tar

版本及配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<properties>
<app.main.class></app.main.class>
<maven-enforcer-plugin>1.4.1</maven-enforcer-plugin>
<maven-compiler-plugin>3.1</maven-compiler-plugin>
<maven-jar-plugin>2.6</maven-jar-plugin>
<maven-dependency-plugin>2.10</maven-dependency-plugin>
<maven-source-plugin>3.0.1</maven-source-plugin>
<spring-boot-maven-plugin>1.5.13.RELEASE</spring-boot-maven-plugin>
<src.data.dir>src/main/data</src.data.dir>
<dependency.dist.dir>${project.build.directory}/dist</dependency.dist.dir>
<dependency.lib.dir>${dependency.dist.dir}/lib</dependency.lib.dir>
<dependency.data.dir>${dependency.dist.dir}/data</dependency.data.dir>
<dependency.bin.dir>${dependency.dist.dir}/bin</dependency.bin.dir>
<dependency.log.dir>${dependency.dist.dir}/logs</dependency.log.dir>
</properties>

资源目录

1
2
3
4
5
6
7
8
9
10
11
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
<!--非资源目录-->
<resource>
<directory>bin</directory>
<filtering>true</filtering>
</resource>
</resources>

plugins插件

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<plugins>
<!--jar冲突排查-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${maven-enforcer-plugin}</version>
<executions>
<execution>
<id>enforce</id>
<configuration>
<rules>
<dependencyConvergence>
<uniqueVersions>true</uniqueVersions>
</dependencyConvergence>
</rules>
</configuration>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
</plugin>

<!--jdk版本编译-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin}</version>
<configuration>
<useIncrementalCompilation>false</useIncrementalCompilation>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<skip>true</skip>
</configuration>
</plugin>


<!-- 用于生成jar包的plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin}</version>
<configuration>
<outputDirectory>${dependency.lib.dir}</outputDirectory>
<skip>true</skip>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${app.main.class}</mainClass>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>

<!-- 用于拷贝maven依赖的plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin}</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- 把依赖的所有jar包拷贝到lib目录中-->
<outputDirectory>${dependency.lib.dir}</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteReleases>true</overWriteReleases>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
<configuration>
<excludeScope>provided</excludeScope>
<includeScope>runtime</includeScope>
</configuration>
</plugin>

<!--将Spring Boot应用打包为可执行的jar或war文件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-maven-plugin}</version>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>

<!--源码打包并发布-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven-source-plugin}</version>
<configuration>
<outputDirectory>${project.build.directory}/sources-lib</outputDirectory>
</configuration>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- copy文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<mkdir dir="${src.data.dir}"/>
<mkdir dir="${dependency.dist.dir}"/>
<mkdir dir="${dependency.lib.dir}"/>
<mkdir dir="${dependency.log.dir}"/>
<mkdir dir="${dependency.data.dir}"/>
<mkdir dir="${dependency.bin.dir}"/>
<copy todir="${dependency.dist.dir}" overwrite="true">
<fileset dir="${src.data.dir}"/>
</copy>
<chmod file="${dependency.bin.dir}/app.sh" perm="ugo+rx"/>
<tar destfile="${project.build.directory}/${project.artifactId}-${project.version}.tar"
basedir="${project.build.directory}/dist"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>ru.yaal.maven</groupId>
<artifactId>write-text-files-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<charset>UTF-8</charset>
<files>
<file>
<path>${dependency.bin.dir}/app.sh</path>
<lines>
<line>#!/bin/sh</line>
<line>exit 0</line>
</lines>
</file>
</files>
</configuration>
<executions>
<execution>
<id>write-text-files</id>
<phase>prepare-package</phase>
<goals>
<goal>write-text-files</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
------ 本文结束------

本文标题:Maven 打包Plugins集成

文章作者:Perkins

发布时间:2019年11月30日

原始链接:https://perkins4j2.github.io/posts/22633/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。