【问题标题】:How can I stop maven from running TEST twice?如何阻止 Maven 运行 TEST 两次?
【发布时间】:2016-12-23 16:10:35
【问题描述】:

我有一个包含大约 5000 个测试用例的大项目。当运行mvn clean install 时,它将运行test 目标两次(一次作为安装的一部分,第二次作为surefire 插件的一部分)。

为什么需要第二次运行test?是否可以强制surefire 使用test 目标结果而不是重新调用它自己的结果? 我认为这是浪费时间和机器资源,尤其是最近第二轮运行test 导致PermGen 构建错误,无论我将多少堆泵入maven runner,它仍然在第二轮测试中死亡。

这是我的万无一失的插件配置:

 <plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <executions>
       <execution>
         <id>default-test</id>
         <phase>test</phase>
         <goals>
             <goal>test</goal>
         </goals>
         <configuration>
            <parallel>classes</parallel>
            <threadCount>3</threadCount>
         </configuration>
      </execution>
   </executions>
</plugin>

有没有办法调整插件以更好地处理机器资源?

这是执行的完整的默认 maven 配置文件:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                    </manifest>
                    <manifestEntries>
                        <Build-Number>${build.number}</Build-Number>
                        <Job-Name>${job.name}</Job-Name>
                        <Build-Url>${build.url}</Build-Url>
                        <Git-Commit>${git.commit}</Git-Commit>
                        <Git-Branch>${git.branch}</Git-Branch>
                        <Timestamp>${maven.build.timestamp}</Timestamp>
                        <StyleGuide-Version>${styleguide.version}</StyleGuide-Version>
                    </manifestEntries>
                </archive>
                <warName>pss</warName>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.cj.jshintmojo</groupId>
            <artifactId>jshint-maven-plugin</artifactId>
            <version>1.3.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>lint</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <options>maxparams:5,camelcase,eqeqeq,forin,immed,latedef,noarg,noempty,nonew,expr</options>
                <directories>
                    <directory>src/main/webapp/js/page</directory>
                </directories>
                <excludes>
                    <exclude>src/main/webapp/js/page/marketingPreferences.js</exclude>
                    <exclude>src/main/webapp/js/page/changeCarParkingDetails.js</exclude>
                    <exclude>src/main/webapp/js/page/angularjs-app.js</exclude>
                    <exclude>src/main/webapp/js/page/content-cover.js</exclude>
                    <exclude>src/main/webapp/js/page/amendmentConfirm.js</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.lesscss</groupId>
            <artifactId>lesscss-maven-plugin</artifactId>
            <version>1.3.3</version>
            <executions>
                <execution>
                    <id>bingleless</id>
                    <configuration>
                        <sourceDirectory>${project.basedir}/src/main/webapp/app-resources/</sourceDirectory>
                        <outputDirectory>${project.basedir}/src/main/webapp/app-resources/</outputDirectory>
                        <includes>
                            <include>**\/policy-self-service\/**\/*pss-sg.less</include>
                        </includes>
                        <compress>true</compress>
                    </configuration>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>yuicompressor-maven-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compress</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <excludes>
                    <exclude>**/*.min.js</exclude>
                    <exclude>**/*.min.css</exclude>
                    <exclude>**/style-guide/**</exclude>
                    <exclude>**/generated/**</exclude>
                    <exclude>**/app-resources/common/**</exclude>
                    <exclude>**/app-resources/bower_components/**</exclude>
                    <exclude>**/app-resources/policy-self-service/**</exclude>
                </excludes>
                <nosuffix>true</nosuffix>
                <jswarn>false</jswarn>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <configuration>
                <logViolationsToConsole>true</logViolationsToConsole>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire-plugin.version}</version>
            <configuration>
                <parallel>classes</parallel>
                <threadCount>3</threadCount>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>${maven-surefire-report-plugin.version}</version>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>report-only</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

【问题讨论】:

  • 您是否尝试删除执行部分?
  • 请添加(修剪)mvn clean install 的输出,尤其是以[INFO] --- maven-surefire-plugin 开头的行 - 这应该有助于跟踪插件执行的时间和次数。
  • 只运行一次,有其他插件吗?
  • @Zelldon 是的,我尝试删除执行部分,仍然调用了两次。
  • @ravthiru 唯一的其他插件是maven-surefire-report-plugin。您可以在上面看到完整的 maven

标签: java maven unit-testing maven-surefire-plugin


【解决方案1】:

我认为pom.xml 中的&lt;execution&gt; 会导致第二次测试运行。 Maven 将其视为测试阶段默认目标之外的另一个执行目标。

由于maven-surefire-plugin是Maven默认用于测试阶段的插件,你只需要在&lt;execution&gt;之外提供&lt;configuration&gt;部分。修改你的pom.xml如下

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
        <parallel>classes</parallel>
        <threadCount>3</threadCount>
    </configuration>
  </plugin>
</plugins>

【讨论】:

  • 这是正确的,因为 maven-surefire-plugin 默认已经绑定到生命周期。最好是通过 pluginManagement 而不是在构建中进行配置..
  • @Rocherlee 试过了,也没用。仍然调用 TESTS 两次。
  • @DhafirNz 你找到解决方案了吗?
  • 找出发生了什么的好方法是使用:mvn help:effective-pom
【解决方案2】:

如果在您的 pom 中同时存在 cobertura 和 surefire 插件,测试将执行两次。为了避免在 cobertura 插件中将执行阶段添加为 none,如下所述:

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <executions>
                <execution>
                    <phase>none</phase>
                </execution>
            </executions>
</plugin>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多