Process spawned by exec-maven-plugin blocks the maven process
Asked Answered
C

4

18

I am trying to execute the following scenario using maven :

  1. pre-integration-phase : Start a java based application using a main class (using exec-maven-plugin)
  2. integration-phase : Run the integration test cases (using maven-failsafe-plugin)
  3. post-integration-phase: Stop the application gracefully (using exec-maven-plugin)

Here is pom.xml snip:

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

If I execute mvn post-integration-test, my application is getting started as a child process of the maven process, but the application process is blocking the maven process from executing the integration tests which comes in the next phase. Later I found that there is a bug (or missing functionality?) in maven exec plugin, because of which the application process blocks the maven process. To address this issue, I have encapsulated the invocation of MyApp.java in a shell script and then appended “/dev/null 2>&1 &” to spawn a separate background process. Here is the snip (this is just a snip and not the actual one) from runTest.sh:

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

Although this solves my issue, is there any other way to do it? Am I missing any argument for exec-maven-plugin?

Coeducation answered 30/3, 2012 at 5:25 Comment(0)
Z
4

You can use the async configuration parameter to achieve what you require. Also see asyncDestroyOnShutdown which will shutdown the app on JVM exit by default.

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

If set to true the child process executes asynchronously and build execution continues in parallel.

    <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>
Zenobiazeolite answered 25/4, 2018 at 17:47 Comment(0)
I
3

(Unix only) Use exec:exec and start a script. This script should call

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

first, and check the startup before the script finishes. Did the program crash in 2 seconds?

When your java app needs some time to start you can add a script that waits for a complete startup.

   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}
Imine answered 20/1, 2015 at 15:20 Comment(0)
R
3

I am using the https://github.com/bazaarvoice/maven-process-plugin plugin while this issue is still open https://github.com/mojohaus/exec-maven-plugin/issues/18

Reachmedown answered 16/2, 2018 at 10:37 Comment(2)
Indeed, while this plugin may not be as flexible as the basic exec-maven-plugin, it is much better suited for my purposes... Thank you for this hint.Denumerable
That should be the accepted answer. This covers the complete integration test-phase including waiting for the spawned processes to be started.Siskin
R
1

There's a possible solution in this thread using Ant: Possible solution here: Maven and Exec: forking a process?

The same disadvantage as in your current solution is that it's platform dependent as well. Your current solution will only run in Unix/Linux-like environments, while the Ant-based solution is starting an instance of cmd, which only works on Windows. Using Maven profiles, one could create a configuration that uses the OS it's running on to determine which command shell to start, of course.

Rosabelle answered 30/3, 2012 at 6:44 Comment(1)
Yes, you are right. Windows is going to be a problem with my fix.Coeducation

© 2022 - 2024 — McMap. All rights reserved.