maven-surefire-plugin与配置文件无法正常工作

huangapple go评论69阅读模式
英文:

maven-surefire-plugin isn't working with profiles

问题

I have profile configuration in my POM with surefire-maven-plugin & junit connection to run only specific tests by profile. For example:

mvn clean test -Pgroup1 --also-make -DfailIfNoTests=false

It works as expected with following versions:

<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
<junit.version>4.12</junit.version>

But stops working normally when I try to upgrade them:

<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
<junit.version>4.13</junit.version>

In this case mvn test always run all tests as I wouldn't set profile in command line. My config of profiles is:

    <profiles>
        <profile>
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <includes>
                                <include>**/unit/*Test.java</include>
                            </includes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>group1</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <excludes>
                                <exclude>**/unit/**</exclude>
                            </excludes>
                            <groups>com.Group1</groups>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>group2</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <excludes>
                                <exclude>**/unit/**</exclude>
                            </excludes>
                            <groups>com.Group2</groups>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        ......................
    </profiles>

Every test class has connected interface linked to profile:

@Category(Group1.class)
@RunWith(JUnitParamsRunner.class)
public class Group1Test {

Playing with 'default' profile and 'activeByDefault' property also gave me no result. Any ideas how to fix it?

英文:

I have profile configuration in my POM with surefire-maven-plugin & junit connection to run only specific tests by profile. For example:

mvn clean test -Pgroup1 --also-make -DfailIfNoTests=false

It works as expected with following versions:

<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
<junit.version>4.12</junit.version>

But stops working normally when I try to upgrade them:

<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
<junit.version>4.13</junit.version>

In this case mvn test always run all tests as I wouldn't set profile in command line.
My config of profiles is:

    <profiles>
        <profile>
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <includes>
                                <include>**/unit/*Test.java</include>
                            </includes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>group1</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <excludes>
                                <exclude>**/unit/**</exclude>
                            </excludes>
                            <groups>com.Group1</groups>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>group2</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <excludes>
                                <exclude>**/unit/**</exclude>
                            </excludes>
                            <groups>com.Group2</groups>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        ......................
    </profiles>

Every test class has connected interface linked to profile:

@Category(Group1.class)
@RunWith(JUnitParamsRunner.class)
public class Group1Test {

Playing with 'default' profile and 'activeByDefault' property also gave me no result. Any ideas how to fix it?

答案1

得分: 2

我通过在默认插件和配置文件插件(顺便说一下,并不是默认插件的覆盖)中都使用了"executions"来使其工作。

<project>
  ...
  <build>
  ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M8</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals><goal>test</goal></goals> <!-- REQUIRED -->
            <configuration>
              <enableAssertions>true</enableAssertions>
              <systemPropertyVariables>
                <log4j.debug>true</log4j.debug>
                <client.test.url>http://localhost:8080/axis/services/MyService</client.test.url>
              </systemPropertyVariables>
              <excludes>
                <exclude>**/TestAccountOp</exclude> <!-- Tomcat required - use the profile -->
                <exclude>**/TestWsdl</exclude>
              </excludes>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <profiles>
    <profile>
      <id>myprofileid-testwithtomcat</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M8</version>
            <executions>
              <execution> <id>default-test</id> <phase>none</phase> </execution> <!-- Disable default Maven execution -->
              <execution>
                <id>myexecutionid-testwithtomcat</id>
                <phase>test</phase>
                <goals><goal>test</goal></goals> <!-- REQUIRED -->
                <configuration>
                  <enableAssertions>true</enableAssertions>
                  <systemPropertyVariables>
                    <log4j.debug>true</log4j.debug>
                    <client.test.url>http://localhost:8080/axis/services/MyService</client.test.url>
                  </systemPropertyVariables>
                  <excludes>
                    <exclude>**/TestWsdl</exclude>
                  </excludes>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    ...
  </profiles>
</project>

在配置文件中,我移除了"default-test",否则"excludes"会从默认配置中设置,但这可能是因为在我将"configurations"移到"executions"之前发生的。你必须记住,"default-test" 在你不禁用它的情况下是活动的,但在主体中不使用"execution"对我来说行不通。

"default-test"是Maven在主体中的"id",所以我在主体中的"execution"中使用了那个名字。

我认为你可以不用去管"phase"元素,因为那是"test"目标的默认值,但我很确定你需要这个目标。

祝你好运!

英文:

I got this to work by using "executions" in both the default plugin and in the profile plugin (which is not, by the way, an override of the default one)

&lt;project&gt;
  ...
  &lt;build&gt;
  ...
      &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
        &lt;version&gt;3.0.0-M8&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;id&gt;default-test&lt;/id&gt;
            &lt;phase&gt;test&lt;/phase&gt;
            &lt;goals&gt;&lt;goal&gt;test&lt;/goal&gt;&lt;/goals&gt; &lt;!-- REQUIRED --&gt;
            &lt;configuration&gt;
              &lt;enableAssertions&gt;true&lt;/enableAssertions&gt;
              &lt;systemPropertyVariables&gt;
                &lt;log4j.debug&gt;true&lt;/log4j.debug&gt;
                &lt;client.test.url&gt;http://localhost:8080/axis/services/MyService&lt;/client.test.url&gt;
              &lt;/systemPropertyVariables&gt;
              &lt;excludes&gt;
                &lt;exclude&gt;**/TestAccountOp&lt;/exclude&gt; &lt;!-- Tomcat required - use the profile --&gt;
                &lt;exclude&gt;**/TestWsdl&lt;/exclude&gt;
              &lt;/excludes&gt;
            &lt;/configuration&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;
  &lt;profiles&gt;
    &lt;profile&gt;
      &lt;id&gt;myprofileid-testwithtomcat&lt;/id&gt;
      &lt;build&gt;
        &lt;plugins&gt;
          &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.0.0-M8&lt;/version&gt;
            &lt;executions&gt;
              &lt;execution&gt; &lt;id&gt;default-test&lt;/id&gt; &lt;phase&gt;none&lt;/phase&gt; &lt;/execution&gt; &lt;!-- Disable default Maven execution --&gt;
              &lt;execution&gt;
                &lt;id&gt;myexecutionid-testwithtomcat&lt;/id&gt;
                &lt;phase&gt;test&lt;/phase&gt;
                &lt;goals&gt;&lt;goal&gt;test&lt;/goal&gt;&lt;/goals&gt; &lt;!-- REQUIRED --&gt;
                &lt;configuration&gt;
                  &lt;enableAssertions&gt;true&lt;/enableAssertions&gt;
                  &lt;systemPropertyVariables&gt;
                    &lt;log4j.debug&gt;true&lt;/log4j.debug&gt;
                    &lt;client.test.url&gt;http://localhost:8080/axis/services/MyService&lt;/client.test.url&gt;
                  &lt;/systemPropertyVariables&gt;
                  &lt;excludes&gt;
                    &lt;exclude&gt;**/TestWsdl&lt;/exclude&gt;
                  &lt;/excludes&gt;
                &lt;/configuration&gt;
              &lt;/execution&gt;
            &lt;/executions&gt;
          &lt;/plugin&gt;
        &lt;/plugins&gt;
      &lt;/build&gt;
    &lt;/profile&gt;
    ...
  &lt;/profiles&gt;
&lt;/project&gt;

In the profile, I removed the "default-test", otherwise the "excludes" were set from the default one, but that might have been before I moved the "configurations" into the "executions". You have to remember that "default-test" is active unless you disable it, but not using an "execution" in the main body did not work for me.

"default-test" is Maven's "id" for the one in the main body, so that is why I used that name in the "execution" in the main body.

I think you can get away with not bothering with the "phase" elements, because that's the default for the "test" goal, but I'm pretty sure that you need the goal.

Good luck!

huangapple
  • 本文由 发表于 2020年8月17日 21:28:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63451828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定