18

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

  1. pre-integration-phase :使用主类启动基于 java 的应用程序(使用 exec-maven-plugin)
  2. integration-phase :运行集成测试用例(使用 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(或者缺少功能?),因为应用程序进程阻塞了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 的任何论点吗?

4

4 回答 4

4

您可以使用 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>
于 2018-04-25T17:47:21.250 回答
3

(仅限 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}
于 2015-01-20T15:20:37.990 回答
3

我正在使用https://github.com/bazaarvoice/maven-process-plugin插件,而此问题仍处于打开状态https://github.com/mojohaus/exec-maven-plugin/issues/18

于 2018-02-16T10:37:36.493 回答
1

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

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

于 2012-03-30T06:44:28.883 回答