maven常用插件使用范例:

  • maven-assembly-plugin
  • exec-maven-plugin
  • docker-maven-plugin
  • maven-antrun-plugin
  • maven-jar-plugin
  • maven-dependency-plugin
  • wagon-maven-plugin
  • tomcat7-maven-plugin
  • maven-shade-plugin
  • git-commit-id-plugin
  • properties-maven-plugin

(maven-assembly-plugin)

定制化打包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>org.struy.MainTets</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

(exec-maven-plugin)

执行本地脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>build-docker-image</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${basedir}/build.sh</executable>
</configuration>
</execution>
</executions>
</plugin>

(docker-maven-plugin)

构建docker镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.10</version>
<configuration>
<imageName>docker.dapeng.example/hello-service:1.0-SNAPSHOT</imageName>
<dockerDirectory>${basedir}/docker</dockerDirectory>
</configuration>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>

(maven-antrun-plugin)

在maven中运行Ant任务,比如在打包阶段,对文件进行复制

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
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>copy files</id>
<phase>package</phase>
<configuration>
<!-- copy child's output files into target/docker -->
<tasks>
<copy todir="${basedir}/docker/${project.artifactId}/">
<fileset dir="${project.build.directory}/${project.artifactId}"/>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>clean up the docker folder</id>
<phase>clean</phase>
<configuration>
<!-- delete folder under docker -->
<tasks>
<delete dir="${basedir}/docker/${project.artifactId}"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

(maven-jar-plugin)

jar指定目录,设定manifest的参数,或指定运行的Main class,还有依赖的jar包,加入classpath中

1
2
3
4
5
6
7
8
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<outputDirectory>${project.build.directory}/${project.artifactId}/</outputDirectory>
</configuration>
</plugin>

(maven-dependency-plugin)

复制依赖的jar包到指定的文件夹里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.artifactId}/
</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeArtifactIds>dapeng-core</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>

(wagon-maven-plugin)

用于一键部署,把本地打包的jar文件,上传到远程服务器上,并执行服务器上的shell命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<serverId>crawler</serverId>
<fromDir>target</fromDir>
<includes>*.jar,*.properties,*.sh</includes>
<url>sftp://59.110.162.178/home/zhangxianhe</url>
<commands>
<command>chmod 755 /home/zhangxianhe/update.sh</command>
<command>/home/zhangxianhe/update.sh</command>
</commands>
<displayCommandOutputs>true</displayCommandOutputs>
</configuration>
</plugin>

(tomcat7-maven-plugin)

用于远程部署Java Web项目

1
2
3
4
5
6
7
8
9
10
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://59.110.162.178:8080/manager/text</url>
<username>linjinbin</username>
<password>linjinbin</password>
</configuration>
</plugin>

(maven-shade-plugin)

用于把多个jar包,打成1个jar包一般Java项目都会依赖其他第三方jar包,最终打包时,希望把其他jar包包含在一个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
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.meiyou.topword.App</Main-Class>
<X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
<X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

(git-commit-id-plugin)

获取git的commit-id插件,比如在打包jar包和构建docker镜像时用于区分版本(配合properties-maven-plugin 可使用${git.commit.id.abbrev}使用此变量在其他插件中获取commit-id)

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
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
<phase>package</phase>
</execution>
</executions>

<configuration>
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>

<!-- @since 2.2.0 -->
<dateFormatTimeZone>${user.timezone}</dateFormatTimeZone>

<generateGitPropertiesFile>true</generateGitPropertiesFile>

<generateGitPropertiesFilename>${project.build.directory}/git.ini</generateGitPropertiesFilename>

<format>properties</format>

<skipPoms>false</skipPoms>

<injectAllReactorProjects>false</injectAllReactorProjects>
<skip>false</skip>

<!-- @since 2.1.12 -->
<runOnlyOnce>false</runOnlyOnce>

<!-- @since 2.1.9 -->
<includeOnlyProperties>
<includeOnlyProperty>^git.branch$</includeOnlyProperty>
<includeOnlyProperty>^git.commit.id.abbrev$</includeOnlyProperty>
</includeOnlyProperties>

<useNativeGit>true</useNativeGit>

<abbrevLength>7</abbrevLength>
<commitIdGenerationMode>flat</commitIdGenerationMode>

<gitDescribe>
<skip>true</skip>
<always>false</always>
<abbrev>7</abbrev>
<dirty>-dirty</dirty>
<match>*</match>
<forceLongFormat>false</forceLongFormat>
</gitDescribe>
</configuration>
</plugin>

(properties-maven-plugin)

读取指定properties(这里是配合git-commit-id-plugin 以读取commit-id信息)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${project.build.directory}/git.ini</file>
</files>
</configuration>
</execution>
</executions>
</plugin>