当配置文件标识符在多个项目中时,Maven测试运行两次。为什么?

时间:2014-01-08 22:32:25

标签: maven testing intellij-idea

我在IntelliJ中有很多项目,每个项目都有一个pom.xml,每个项目的poms都从master pom.xml继承。一个配置文件(称为test1)存在于两个pom中(对于project2和project4)。当我从命令行运行maven时,指定一个项目和配置文件名称,它可以工作(该项目中的测试执行一次)这是命令:

mvn test -pl project2 -am -P test1

当我指定两个项目(两个项目都具有相同的配置文件)时,project4中的测试将执行两次。这是命令:

mvn test -pl project2,project4 -am -P test1

我希望测试只执行一次。我正在运行maven 3.1.1。

更复杂的是,当我只指定project4时,project2中的测试执行一次,而project4中的测试根本没有执行。这是命令:

mvn test -pl project4 -am -P test1

这是project2的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns stuff...>
    <parent>
        <artifactId>parent artifact id</artifactId>
        <groupId>group id</groupId>
        <version>version</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <name>project2</name>

    <artifactId>project2</artifactId>
    <packaging>jar</packaging>

    <profiles>
        <profile>
            <id>test1</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.9</version>
                        <executions>
                            <execution>
                                <id>execute-tests-1</id>
                                <phase>test</phase>
                                <goals>
                                    <goal>test</goal>
                                </goals>
                                <configuration>
                                    <skip>false</skip>
                                    <excludes>
                                        <exclude>com/path/to/exclude/**/*.java</exclude>
                                    </excludes>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <build>
        <plugins>
            <!-- We don't want to run any tests without an active profile -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <!-- This exports the classes in the tests for use with our other modules' tests -->
            <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>
        </plugins>
    </build>

    <dependencies>
        [ dependencies ...]
    </dependencies>
</project>

这是project4的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns stuff>
    <parent>
        <artifactId>[parent artifact id]</artifactId>
        <groupId>[group id]</groupId>
        <version>[version]</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <name>project4</name>


    <artifactId>project4</artifactId>
    <packaging>jar</packaging>
    <dependencies>
        [ dependencies ...]
    </dependencies>

    <profiles>
        <profile>
            <id>test1</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.9</version>
                        <executions>
                            <execution>
                                <id>execute-tests-2</id>
                                <phase>test</phase>
                                <goals>
                                    <goal>test</goal>
                                </goals>
                                <configuration>
                                    <includes>
                                        <include>com/path/to/tests/*.java</include>
                                    </includes>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
            <dependencies>
                [ dependencies...]
            </dependencies>
        </profile>
    </profiles>
</project>

1 个答案:

答案 0 :(得分:4)

我找到了自己问题的答案(仔细查看我们的其他项目的Maven测试设置)。我不得不做两件事:

  1. 在surefire插件中的<skip>false</skip>聚合中加入<configuration>个元素。
  2. <plugins>部分之外添加通用的surefire <profiles>聚合。这个<skip>设置为true并阻止测试运行,除非它们在配置文件中。以下是该部分的内容:

    <build>
        <plugins>
            <!-- We don't want to run any tests without an active profile -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
    
        </plugins>
    </build>
    

    问题是测试正在运行默认测试生命周期阶段,然后它们在测试阶段再次运行。在我做出改变之后,他们只在测试阶段跑了。