使用 exec-maven-plugin 时如何防止测试 运行

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

我 运行 JavaScript 在使用 exec-maven-plugin 的 maven 项目中进行单元测试

<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>

我假设如果我通过 -DskipTests,整个测试阶段将被跳过,但事实并非如此,skipTests flag is only honored when using "Surefire, Failsafe and the Compiler Plugin"

问题:我怎样才能使执行依赖于skipTests标志?

您可以使用 exec-maven-pluginskip 选项,如下所示:

<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>

请注意 ${skipTests} 的用法作为其值的一部分。另请注意,我使用了较新的版本 1.5.0,推荐。

运行:

mvn clean test -DskipTests

输出将包含:

[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   

执行时:

mvn clean test

输出将是

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