Search code examples
mavenmaven-3pom.xmlexec-maven-plugin

How can I prevent tests from being run when using exec-maven-plugin


I am running JavaScript unit tests in a maven project using exec-maven-plugin

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3</version>
    <executions>
        <execution>
            <id>run-karma</id>
            <phase>test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>${node.bin.folder}/node</executable>
                <workingDirectory>${project.basedir}</workingDirectory>
                <arguments>
                    <argument>node_modules/karma/bin/karma</argument>
                    <argument>start</argument>
                    <argument>--single-run</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

I was under the assumption that if I passed -DskipTests, the entire test phase would be skipped but that's not the case, the skipTests flag is only honored when using "Surefire, Failsafe and the Compiler Plugin"

Question: How can I make it so that execution depends on the skipTests flag?


Solution

  • You can use the skip option of the exec-maven-plugin as following:

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.5.0</version>
                <executions>
                    <execution>
                        <id>run-karma</id>
                        <phase>test</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>cmd</executable>
                            <arguments>
                                <argument>/C</argument>
                                <argument>echo</argument>
                                <argument>hello</argument>
                            </arguments>
                            <skip>${skipTests}</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    Note the usage of ${skipTests} as part of its value. Also note I used a newer version, 1.5.0, recommended.

    Running:

    mvn clean test -DskipTests
    

    The output would contain:

    [INFO] Tests are skipped.   
    [INFO]   
    [INFO] --- exec-maven-plugin:1.5.0:exec (run-karma) @ sample-project ---   
    [INFO] skipping execute as per configuration   
    [INFO] ------------------------------------------------------------------------   
    [INFO] BUILD SUCCESS   
    

    While executing:

    mvn clean test
    

    The output would be

    Tests run: 8, Failures: 0, Errors: 0, Skipped: 0   
    
    [INFO]   
    [INFO] --- exec-maven-plugin:1.5.0:exec (run-karma) @ sample-project ---   
    hello   
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS