【问题标题】:Process spawned by exec-maven-plugin blocks the maven processexec-maven-plugin 生成的进程会阻塞 maven 进程
【发布时间】:2012-04-13 19:25:58
【问题描述】:

我正在尝试使用 maven 执行以下场景:

  1. 预集成阶段:使用主类(使用 exec-maven-plugin)启动基于 java 的应用程序
  2. 集成阶段:运行集成测试用例(使用 maven-failsafe-plugin)
  3. post-integration-phase:优雅地停止应用程序(使用 exec-maven-plugin)

这里是 pom.xml 片段:

    <plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <id>launch-myApp</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <executable>java</executable>
            <arguments>
                <argument>-DMY_APP_HOME=/usr/home/target/local</argument>
                <argument>-Djava.library.path=/usr/home/other/lib</argument>
                <argument>-classpath</argument>
                <classpath/>
                <argument>com.foo.MyApp</argument>
            </arguments>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.12</version>
        <executions>
            <execution>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <forkMode>always</forkMode>
        </configuration>
    </plugin>
</plugins>

如果我执行 mvn post-integration-test,我的应用程序将作为 maven 进程的子进程启动,但应用程序进程阻止 maven 进程执行下一阶段的集成测试。后来发现maven exec插件里面有个bug (or missing functionality?),就是因为这个应用进程阻塞了maven进程。为了解决这个问题,我将 MyApp.java 的调用封装在一个 shell 脚本中,然后附加“/dev/null 2>&1 &”来生成一个单独的后台进程。这是来自 runTest.sh 的片段(这只是片段而不是实际片段):

java - DMY_APP_HOME =$2 com.foo.MyApp > /dev/null 2>&1 &

虽然这解决了我的问题,但还有其他方法吗?我是否缺少 exec-maven-plugin 的任何参数?

【问题讨论】:

    标签: maven integration-testing maven-plugin exec-maven-plugin


    【解决方案1】:

    您可以使用 async 配置参数来实现您所需要的。另请参阅 asyncDestroyOnShutdown ,默认情况下将在 JVM 退出时关闭应用程序。

    https://www.mojohaus.org/exec-maven-plugin/exec-mojo.html#async

    如果设置为 true,则子进程异步执行,构建执行并行继续。

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <id>launch-myApp</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>java</executable>
                <arguments>
                    <argument>-DMY_APP_HOME=/usr/home/target/local</argument>
                    <argument>-Djava.library.path=/usr/home/other/lib</argument>
                    <argument>-classpath</argument>
                    <classpath/>
                    <argument>com.foo.MyApp</argument>
                </arguments>
                <async>true</async>
            </configuration>
        </plugin>
    

    【讨论】:

      【解决方案2】:

      (仅适用于 Unix) 使用 exec:exec 并启动一个脚本。 这个脚本应该调用

      java -DMY_APP_HOME =$2 com.foo.MyApp > /dev/null 2>&1 &
      

      首先,在脚本完成之前检查启动。 程序是否在 2 秒内崩溃?

      当您的 java 应用程序需要一些时间来启动时,您可以添加一个等待完全启动的脚本。

         retrymax=30
         retry=0
         RETURNCODE=1
         while [ ${retry} -lt ${retrymax} ]; do
            todo_your_test
            RETURNCODE=$?
            if [ ${RETURNCODE} -eq 0 ]; then
               echo MyApp started
               break
            else
               (( retry = retry + 1 ))
               sleep 2
            fi
         done
         exit ${RETURNCODE}
      

      【讨论】:

        【解决方案3】:

        【讨论】:

        • 确实,虽然这个插件可能不如基本的 exec-maven-plugin 灵活,但它更适合我的目的......谢谢你的提示。
        • 这应该是公认的答案。这涵盖了完整的集成测试阶段,包括等待生成的进程启动。
        【解决方案4】:

        在这个线程中有一个使用 Ant 的可能解决方案:可能的解决方案在这里:Maven and Exec: forking a process?

        与您当前的解决方案相同的缺点是它也依赖于平台。您当前的解决方案只能在类 Unix/Linux 环境中运行,而基于 Ant 的解决方案正在启动一个 cmd 的实例,该实例仅适用于 Windows。当然,使用 Maven 配置文件,可以创建一个配置,使用它运行的操作系统来确定启动哪个命令 shell。

        【讨论】:

        • 是的,你是对的。 Windows 将成为我修复的问题。
        猜你喜欢
        • 1970-01-01
        • 2020-10-04
        • 2011-10-05
        • 1970-01-01
        • 2011-06-07
        • 2020-01-25
        • 1970-01-01
        • 2014-09-10
        • 2014-04-23
        相关资源
        最近更新 更多