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

1

11

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?

Grayson answered 16/6, 2016 at 12:7 Comment(1)
I guess you either wrap running the karma test into a script and check in there if the skipTests property is set and then decide to run the node/karma command. Another solution might be to move this config into a profile and use activation: maven.apache.org/pom.html#Activation or only activate it when wanted.Allanite
A
18

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   
Amie answered 16/6, 2016 at 12:20 Comment(1)
Thanks for the link to the exec-maven-plugin, feeling ashamed that I didn't go there first :) It worked fine on version 1.3 by the way.Grayson

© 2022 - 2024 — McMap. All rights reserved.