Maven常用插件

IDEA中POM变更

IDEA中由于POM中未配置maven-compiler-plugin插件可能导致每次POM发生变更时,Language level变成5,从而导致编译时不成功。

1
2
3
4
5
6
7
8
9
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

打jar包插件

Maven提供了三种打包插件:

  • maven-jar-plugin:Maven默认打包插件,用来创建Project JAR
  • maven-shade-plugin:用来打可执行包,fat Jar
  • maven-assembly-plugin:定制化打包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!-- 指定添加项目中使用的外部jar的classpath项 -->
<addClasspath>true</addClasspath>
<!-- 指定外部jar所在的路径 -->
<classpathPrefix>lib/</classpathPrefix>
<!-- 指定本项目jar包的Main-Class -->
<mainClass>com.long.ent.DemoApplication</mainClass>
</manifest>
</archive>
</configuration>
</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
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
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
<resource>META-INF/spring.factories</resource>
</transformer>
<!--<transformer implementation="org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer"/>-->
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.icloud.DemoApplication</mainClass>
</transformer>
<transformer implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer" />
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.github.edwgiz</groupId>
<artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
</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
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
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.icloud.DemoApplication</mainClass>
</manifest>
</archive>
</configuration>
<executions> <!--执行器 mvn assembly:assembly-->
<execution>
<id>make-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal><!-- 只运行一次 -->
</goals>
<configuration>
<skipAssembly>false</skipAssembly>
<appendAssemblyId>false</appendAssemblyId>
<finalName>ex-${assembly.finalName}</finalName>
<descriptors> <!--描述文件路径-->
<descriptor>${assembly.descriptor1}</descriptor>
</descriptors>
<outputDirectory>${assembly.outputDirectory}</outputDirectory>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
<execution>
<id>make-tar</id>
<phase>install</phase>
<goals>
<goal>single</goal><!-- 只运行一次 -->
</goals>
<configuration>
<skipAssembly>false</skipAssembly>
<appendAssemblyId>false</appendAssemblyId>
<finalName>${assembly.finalName}</finalName>
<descriptors> <!--描述文件路径-->
<descriptor>${project.basedir}/common_tar.xml</descriptor>
</descriptors>
<outputDirectory>${assembly.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

将依赖打入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
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.icloud.ICloudApplication</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

打war包插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<warName>project_name</warName>
<webResources>
<resource>
<directory>src/main/resources/spring/config/${config}</directory>
<targetPath>WEB-INF/classes/spring/config</targetPath>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<targetPath>WEB-INF/classes</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>

编译源代码插件

1
2
3
4
5
6
7
8
9
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

测试插件

JUnit可以通过@Ignore注解标记忽略测试方法。Java中主流的单元测试框架为JUnitTestNGmaven-surefire-plugin插件时测试运行器,它所作的只是在构建执行到特定声明周期阶段时,来执行JUnit或者TestNG的测试用例。

default生命周期中的test阶段被定义为使用单元测试框架运行测试maven-surefire-plugin插件的test目标内置绑定就是test阶段

默认情况下,maven-surefire-plugin插件的test目标会自动执行测试源码路径下(默认为src/test/java/)所有符合一组命名模式的测试类:

  • **Test*.java : 任何子目录下所有命名以Test开头的Java类
  • **/*Test.java:任何子目录下所有命名以Test结尾的Java类
  • **/*TestCase.java:任何子目录下所有命名以TestCase结尾的Java类

Tests结尾的测试类时不会得以自动执行的。

mvn package -DskipTests命令或者配置maven-surefire-plugin插件的<skipTests>true</skipTests>可以跳过测试,但不会跳过测试代码的编译。

mvn package -Dmaven.test.skip=true命令不仅会跳过测试,还会跳过测试代码的编译。maven.test.skip参数同时控制了maven-compiler-pluginmaven-surefire-plugin两个插件的行为。

还可以通过mvn test -Dtest = Random*Test,AccountTest命令指定执行的测试文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- Sets the VM argument line used when unit tests are run. -->
<argLine>${surefireArgLine}</argLine>
<!-- Maven打包时跳过测试 -->
<skipTests>true</skipTests>
<testFailureIgnore>true</testFailureIgnore>
<excludes>
<exclude>com.proto.core.services.it.**</exclude>
</excludes>
<includes>
<include>com.proto.core.services.ut.*</include>
<include>com.proto.core.services.ut.web.*</include>
</includes>
</configuration>
</plugin>

依赖复制插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

Tomcat插件

1
2
3
4
5
6
7
8
9
10
11
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<uriEncoding>utf-8</uriEncoding>
<port>8080</port>
<path>/</path>
<username>admin</username>
<password>admin</password>
</configuration>
</plugin>

Jetty插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.21.v20170120</version>
<configuration>
<webApp>
<contextPath>/ccps</contextPath>
</webApp>
<stopKey>exit</stopKey>
<stopPort>9090</stopPort>
<scanIntervalSeconds>1</scanIntervalSeconds>
<httpConnector>
<port>28074</port>
</httpConnector>
</configuration>
</plugin>

资源文件处理插件

1
2
3
4
5
6
7
8
9
10
11
12
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<delimiters>
<!-- @符号为结束符号,遇到就表示结束过滤 -->
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>

默认主资源文件目录为src/main/resources,需要添加额外的资源文件目录,可通过配置maven-resources-plugin插件来实现

XSD到Java文件的自动生成插件

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
<!-- 将XSD文件自动生成POJO对象,每次变更用Maven重新编译一下 -->
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.14.0</version>
<configuration>
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<generateDirectory>src/main/java/</generateDirectory>
<packageLevelAnnotations>false</packageLevelAnnotations>
<noFileHeader>true</noFileHeader>
<episode>false</episode>
<readOnly>true</readOnly>
<!-- 如果不加生成的类注释会中文乱码 -->
<encoding>UTF-8</encoding>
<!-- 设置生成的类的注解语言en为英文 -->
<locale>en</locale>
</configuration>
<executions>
<execution>
<id>xsd1-generate</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<args>
<!-- 使用XJC给生成Java类添加注解 -->
<arg>-Xannotate</arg>
<!-- 使用XJC给生成Java类添加父类 -->
<arg>-Xinheritance</arg>
<!-- 给生成Java类添加equals方法 -->
<arg>-Xequals</arg>
<!-- 给生成Java类添加hashCode方法 -->
<arg>-XhashCode</arg>
<arg>-Xvalue-constructor</arg>
<arg>-nv</arg>
</args>
<extension>true</extension>
<schemaIncludes>
<include>test.xsd</include>
</schemaIncludes>
<bindingIncludes>
<include>test.xjb</include>
</bindingIncludes>
<generatePackage>com.test.support.xml</generatePackage>
<plugins>
<!-- 基础插件依赖 -->
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>1.11.1</version>
</plugin>
<!-- -Xequals和-XhashCode参数用于生成equals和hashcode方法使用 -->
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-value-constructor</artifactId>
<version>3.0</version>
</plugin>
<!-- 使用XJC给生成Java类添加注解 -->
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>1.0.2</version>
</plugin>
</plugins>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
</dependencies>
</plugin>

每次变更Schema后通过Maven编译一下即可,若有多个Schema文件且需要将生成的POJO放置不同的目录下,只需要添加多个execution即可。

XSLT转换插件

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
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0.2</version>
<configuration>
<transformationSets>
<transformationSet>
<dir>src/main/resources/xsd</dir>
<includes>test.xsd</includes>
<outputDir>target/transformed-schema</outputDir>
<stylesheet>src/main/resources/xsl/test.xsl</stylesheet>
</transformationSet>
<transformationSet>
<dir>src/main/resources/xsd</dir>
<includes>test.xjb</includes>
<outputDir>target/transformed-jaxb-schema</outputDir>
<stylesheet>src/main/resources/xsl/test-jaxb.xsl</stylesheet>
</transformationSet>
</transformationSets>
</configuration>
<executions>
<execution>
<id>xslt-generate</id>
<!-- 将该插件的生命周期绑定到compile上 -->
<phase>compile</phase>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
</plugin>