跟进this问题: 在不冲突的情况下,我无法找到有关如何构建多个(活动)maven配置文件的联合信息。
考虑以下示例: 我想控制我的测试用例如何使用maven-surefire-plugin运行。首先,我需要配置两个不同的环境(jenkins-CI-server)和本地环境。其次,我想启用运行不同的测试集。 我会有四个档案:
env_jenkins
在jenkins上运行env_local
在本地testset_A
用于运行某些测试testset_B
用于运行其他一些测试请注意,对于所有这些配置文件,我需要为maven-surefire-plugin定义插件配置。 (具体的xml配置见下文)
然后我想组合配置文件来运行 - 例如 - jenkins上的testset A.
我的问题:
示例pom:
<profiles>
<!-- handle system configurations (e.g. one for jenkins environment, one for local) -->
<profile>
<id>env_jenkins</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<arquillian.launch>jbossas-managed</arquillian.launch>
<jbossHttpPortOverride>8080</jbossHttpPortOverride>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>env_local</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<!-- define different test sets -->
<profile>
<id>testset_A</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>testset_B</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>TestB1.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
答案 0 :(得分:1)
它使用在父POM和子POM之间工作的相同inheritance/merging process。使用Maven帮助插件可以确认这一点(使用Maven 3.3.9):
POM中的个人资料声明顺序很重要。插件配置继承了在POM中出现的活动配置文件中声明的相同插件配置。
属性combine.self
和combine.children
可用于控制合并。
使用默认设置的示例(不使用combine.self
或combine.children
,即隐式使用combine.children="merge"
):
mvn help:effective-pom -Penv_local,testset_A
有效POM显示合并配置:
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
<configuration>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
</configuration>
</plugin>
...
combine.self="override"
的示例:
<profile>
<id>testset_A</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration combine.self="override">
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
mvn help:effective-pom -Penv_local,testset_A
有效POM仅显示testset_A
的配置:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration combine.self="override">
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration combine.self="override">
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>
combine.children="append"
的示例:
<profile>
<id>testset_A</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration combine.children="append">
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
mvn help:effective-pom -Penv_local,testset_A
有效POM显示合并配置。在这种情况下,它恰好与第一个示例相同。但是,如果testset_A
引入了env_local
中已存在的配置XML元素,则会附加它们:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration combine.children="append">
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration combine.children="append">
<systemPropertyVariables>
<arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
<jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
</systemPropertyVariables>
<includes>
<include>TestA1.java</include>
<include>TestA2.java</include>
<include>TestA3.java</include>
</includes>
</configuration>
</plugin>