logo头像
Snippet 博客主题

maven-plugin整理

一般情况下maven的配置文件对开发而言都是一次配完,基本不会去关心的事情,但是如果对这个东西不熟悉,还是会遇到一些坑。

deploy 把代码部署到自己的nexus上去

具体的 maven 依赖

1
2
3
4
5
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${version}</version>
</plugin>

如果你的项目不需要发布到私服上去,可以配置 configuration skip 为true。

1
2
3
4
5
6
7
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>

默认该插件是默认集成的,所以我们可以不用显示的在pom文件里面导入该plugin,只需要配置

1
2
3
4
5
6
7
8
9
10
<distributionManagement>
<repository>
<id>releases</id>
<url>${releasesUrl}</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>${snapshotUrl}</url>
</snapshotRepository>
</distributionManagement>

maven-surefire-plugin 执行

默认集成的插件,所以你默认执行mvn clean package的时候会默认执行mvn test ,如果你写了测试用例,但是用例写了一半或者是不规范,就会导致打包失败。

如果需要跳过测试用例,有下面几种方式:

  1. 在properties中声明
    在properties配置中声明跳过测试用例
    1
    2
    3
    <properties>
    <maven.test.skip>true</maven.test.skip>
    </properties>

或:

1
2
3
<properties>
<skipTests>true</skipTests>
</properties>

2.在configuration中声明 skipTests

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.19</version>
</dependency>
</dependencies>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>

3.在执行命令中声明

1
mvn test -Dmaven.test.skip=true

或者

1
mvn test -DskipTests=true

maven-pmd-plugin 代码规范检查

目前ali的规范就有基于pmd执行的代码规范

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
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.8</version>
<configuration>
<printFailingErrors>true</printFailingErrors>
<verbose>true</verbose>
<rulesets>
<ruleset>rulesets/java/ali-comment.xml</ruleset>
<ruleset>rulesets/java/ali-concurrent.xml</ruleset>
<ruleset>rulesets/java/ali-constant.xml</ruleset>
<ruleset>rulesets/java/ali-exception.xml</ruleset>
<ruleset>rulesets/java/ali-flowcontrol.xml</ruleset>
<ruleset>rulesets/java/ali-naming.xml</ruleset>
<ruleset>rulesets/java/ali-oop.xml</ruleset>
<ruleset>rulesets/java/ali-orm.xml</ruleset>
<ruleset>rulesets/java/ali-other.xml</ruleset>
<ruleset>rulesets/java/ali-set.xml</ruleset>
</rulesets>
<excludes>
<exclude>排除的类</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>pmd-check-verify</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.alibaba.p3c</groupId>
<artifactId>p3c-pmd</artifactId>
<version>1.3.6</version>
</dependency>
</dependencies>
</plugin>

通过这个插件,可以写符合自己公司习惯的插件。只要替换掉p3c-pmd包就OK了。