1

Another Maven question. I have app with TestNG tests running by maven-surefire-plugin. I have created 2 profiles, for pdoruction and for testing.

I'm building my app by 'mvn clean install' command. Now my goal is to run TestNG tests only when I specify test profile.

Code:

profiles>
    <profile>
        <id>prod</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>

    <profile>
        <id>test</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19</version>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>${basedir}/target/test-classes/firstTest.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

</profiles>

But the problem is that tests are running everytime when I build my app... no matter if 'test' profile is specified, or not. Why?

G.Spansky
  • 852
  • 6
  • 17
  • 32
  • The test phase is part of Maven's default build life cycle. It's always executed unless you explicitly disable it. Put this snippet in your production profile true – Hung Tran Dec 17 '15 at 12:19

3 Answers3

4

There is a solution to run tests ONLY in case maven profile "test" is activated:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>test</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <skipTests>false</skipTests>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

See also How to keep Maven profiles which are activeByDefault active even if another profile gets activated?

Community
  • 1
  • 1
Vadzim
  • 24,954
  • 11
  • 143
  • 151
2

You can run mvn clean install -DskipTests or change your production profile definition:

 <profile>
     <id>prod</id>
     <activation>
         <activeByDefault>true</activeByDefault>
     </activation>
     <build>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>2.19</version>
                 <configuration>
                     <skipTests>true</skipTests>
                 </configuration>
             </plugin>
         </plugins>
     </build>
 </profile>
Héctor
  • 24,444
  • 35
  • 132
  • 243
1

I think you have to skip the tests explicitly.

Try to add the following configuration to your default profile.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.19</version>
      <configuration>
        <skipTests>true</skipTests>
      </configuration>
    </plugin>
  </plugins>
</build>

Hope it helps.

DirkNM
  • 2,614
  • 15
  • 21