我如何在构建中多次 运行 maven exec 插件

How do I run the maven exec plugin multiple times in a build

下面是我的 POM 的相关部分 -

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.5.0</version>
    <executions>
        <execution>
            <id>StartHub</id>
            <phase>test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>java</executable>
                <arguments>
                    <argument>-jar</argument>
                    <argument>${basedir}/src/lib/Hub/selenium-server-standalone-2.53.0.jar</argument>
                    <argument>-role</argument>
                    <argument>hub</argument>
                    <argument>-throwOnCapabilityNotPresent</argument>
                    <argument>false</argument>
                    <argument>-newSessionWaitTimeout</argument>
                    <argument>20000</argument>
                </arguments>
            </configuration>
        </execution>
        <execution>
            <id>StartNode</id>
            <phase>test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>java</executable>
                <arguments>
                    <argument>-jar</argument>
                    <argument>${basedir}/src/lib/Node/selenium-server-standalone-2.53.0.jar</argument>
                    <argument>-hub</argument>
                    <argument>http://127.0.0.1:4444/register/grid</argument>
                    <argument>-port</argument>
                    <argument>5555</argument>
                    <argument>
                        Dwebdriver.chrome.driver=${basedir}/chromedriver.exe</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

我试图使用 exec 插件在两种配置(集线器和节点)中执行一个 jar(selenium 独立服务器)。

我的问题是 jar 在第一对 "execution" 标记之间指定的配置中仅执行一次。我广泛搜索了一个解决方案,我唯一发现的是不同执行的 id 需要不同,我纠正了这一点。我似乎仍然无法设法 运行 罐子两次,即使我可以 运行 如果我注释掉另一个的执行,我可以成功地 运行 其中任何一个。

我还应该提到我在 Eclipse 中 运行 宁项目,所以理想情况下我想要做的是右键单击 POM.xml 单击 运行 -as 和 select "Maven test".

这不是 Maven 执行问题,而是与您正在执行的内容相关的问题:一个阻塞进程(第一个服务器)会停止构建监听一个永远不会连接的节点(因为它的执行赢了开始)。

将您的 pom 更改为以下内容:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.5.0</version>
    <executions>
        <execution>
            <id>StartHub</id>
            <phase>test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>java</executable>
                <arguments>
                    <argument>-version</argument>
                </arguments>
            </configuration>
        </execution>
        <execution>
            <id>StartNode</id>
            <phase>test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>java</executable>
                <arguments>
                    <argument>-version</argument>
                </arguments>
            </configuration>
        <a/execution>
    </executions>
</plugin>

它将完美地执行两次 exec 执行,打印两次 Java 版本。注意:该片段是您问题的复制和粘贴,只是将 java 命令的参数替换为其 version 选项。


您最有可能寻找的是在后台或至少以非阻塞方式执行这两个命令,以便执行第二个命令,然后继续构建(执行我认为的一些测试) .

查看 this SO question 如何实现:不使用 exec-maven-plugin,而是使用 maven-antrun-plugin 启动两个 selenium 命令。


作为进一步说明,您可能应该考虑改用 maven-failsafe-plugin 并执行集成测试,将上面的执行附加到 pre-integration-test 阶段并在 post-integration-test 阶段停止它们为了正确清洁它们。