我正在尝试使用 Maven 配置文件来分离不同类型的测试(单元、集成、验收)。这是主 pom 文件的一部分:
<properties>
<build.profile.id>dev</build.profile.id>
<skip.unit.tests>false</skip.unit.tests>
<skip.integration.tests>true</skip.integration.tests>
<skip.acceptance.tests>true</skip.acceptance.tests>
</properties>
<profiles>
<profile>
<id>dev</id>
</profile>
<profile>
<id>integration-test</id>
<properties>
<build.profile.id>integration-test</build.profile.id>
<skip.unit.tests>true</skip.unit.tests>
<skip.integration.tests>false</skip.integration.tests>
<skip.acceptance.tests>true</skip.acceptance.tests>
</properties>
</profile>
<profile>
<id>acceptance-test</id>
<properties>
<build.profile.id>acceptance-test</build.profile.id>
<skip.unit.tests>true</skip.unit.tests>
<skip.integration.tests>true</skip.integration.tests>
<skip.acceptance.tests>false</skip.acceptance.tests>
</properties>
</profile>
</profiles>
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<skipTests>${skip.unit.tests}</skipTests>
<includes>
<include>**/*UnitTests.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${skip.integration.tests}</skipTests>
<includes>
<include>**/*IntegrationTests.java</include>
</includes>
</configuration>
</execution>
<execution>
<id>acceptance-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${skip.acceptance.tests}</skipTests>
<includes>
<include>**/*AcceptanceTests.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
正如您所看到的,我正在使用配置文件信息来根据所使用的配置文件运行某些类型的测试。默认配置文件是 dev,它只会运行单元测试。它们可以这样执行:
mvn clean test
对于集成和验收测试,我使用故障安全插件:运行集成测试的示例是:
mvn clean verify -P integration-test
当我从主 pom 模块运行它时,它工作正常,但当我从子模块运行它时,它不起作用。测试只是被忽略。查看子模块的有效 pom,我没有看到配置文件。我做错了什么还是这是 Maven 的预期行为?如果配置文件继承(需要级联到层次结构中最深层的模块)无法以这种方式实现,那该怎么办?
更新:这是项目层次结构 项目目录
--main module
--commons module
--administration
----domain
----data
----business
----web
最佳答案
对于多模块项目,您通常不直接执行模块。相反,您应该始终执行主模块,但仅指定所需的子模块 via -pl
parameter 。还有很多与直接运行模块相关的问题。
刚刚仔细检查了我参与过的一些多模块项目,我们正在使用 <pluginManagement>
将插件配置从父 POM 传播到子项目。
关于java - 子模块中不会继承 Maven 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26079112/