Gangmax Blog

Maven的maven.test.skip和skipTests

我之前一直以为maven的这两个参数是一个意思,今天才发现其实这两者是有区别的。

事情的起因是这样:我们在同一个parent下面有两个module,moduleB依赖于moduleA,比较特殊的一点是,不光是src依赖,test代码也要依赖于moduleA的test代码。可以看出:实际上这里的moduleA是一个base artifact,moduleB是具体的实现,这样的设计是为了以后如果有同样依赖于moduleA的moduleC时,能节省代码。

为了让moduleB的test代码能够依赖于moduleA的test代码,则必须要在moduleA build的时候,生成一个test-jar文件,其中包含moduleA的所有test代码,具体做法是在moduleA中使用如下的maven plugin:

1
2
3
4
5
6
7
8
9
10
11
12
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>

在moduleB中使用如下的dependency表示test-jar的依赖:

1
2
3
4
5
6
7
<dependency>
<groupId>com.test</groupId>
<artifactId>moduleA</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>

这时,在使用”mvn clean install -Dmaven.test.skip=true”命令build整个工程时,会报”moduleA-test.jar”找不到的错误。

最后还是从这里找到了问题的答案:原来加入“maven.test.skip=true”参数会导致整个构建过程跳过了test phase,而”moduleA-test.jar”就是在moduleA的test phase编译出来的。在这种情况下,不应该skip test phase,而是应该skip test execution。这时就应该使用”skipTests(或者maven.test.skip.exec=true)”,这个参数不会跳过test phase,只会跳过test execution。

Comments