Launching a windows batch script using Maven exec plugin blocks the build even though the script uses "start"

Edmondo1984 picture Edmondo1984 · Sep 26, 2012 · Viewed 41.8k times · Source

I am trying to perform integration tasting of the deployment of my application on the top of a custom container. Since my container is custom, I cannot use Maven Cargo plugin to setup the container.

My container:

  • Has to be started though a proper bat file, which is in the path of the machine where the tests are run.
  • Can be manually closed since I have a single maven module containing all my integration tests, even if one day I would like to know how to shut the process down after my tests are completed.

My problem is that I have to run my container in a different process, because it needs to keep running while my tests are performed. Furthermorely, I have an API in my tests that let me wait for the container to be ready (a sort of lookup with timeout).

I have added the following lines to my pom.xml

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>scripts/start-agent.bat</executable>
            </configuration>
        </plugin>

This will call a script, which contains only

start call gs-agent.bat

However the mvn exec plugin gets stucked and my tests are not run. According to what is suggested in How do I run a batch file from my Java Application? , I have modified my pom.xml as the following:

 <configuration>
                <executable>cmd</executable>
                <arguments>
                    <argument>/C</argument>
                    <argument>start</argument>
                    <argument>gs-agent.bat</argument>
                </arguments>
            </configuration>

But this does not seem to solve the issue:

Maven halted

Answer

eis picture eis · Sep 26, 2012

exec plugin is not able to do this, and I found the issue for it, too: http://jira.codehaus.org/browse/MEXEC-87 (link now dead due to codehaus rampdown, can be found from web archive)

In the jira issue linked above, there is a mention and a link of a fork for exec plugin that would have the functionality.

Other than that, I think you'll need to use an antrun-plugin for the time being.

Here's an example taken from working configuration and run with mvn verify. This needs to be in the <plugins>, not <pluginManagement> (exec could reside in pluginmanagement just fine).

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <phase>pre-integration-test</phase>
            <configuration>
                <target>
                    <exec executable="cmd.exe"
                          spawn="true">
                        <arg value="/c"/>
                        <arg value="D:\myfolder\test.bat"/>
                    </exec>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
       </execution>
   </executions>      
</plugin>

Note that spawn="true" is key here if you don't want the execution to block, like specified in the question. If you do want it to block and see the output immediately, set it to false.