This question is a result of an answer to this question from yesterday
run a java application and a web application in a single maven build within a reactor project<
Having tried various options, I've found that the process-exec-maven-plugin to be best. I realize that the OP is asking about maven-antrun-plugin specifically but that is because of an answer received to a generic question.
https://github.com/bazaarvoice/maven-process-plugin
An example of using it to start a nodeJs server which is used as part of integration tests:
<plugin>
<groupId>com.bazaarvoice.maven.plugins</groupId>
<artifactId>process-exec-maven-plugin</artifactId>
<version>${process-exec-maven-plugin.version}</version>
<executions>
<!-- Start process -->
<execution>
<id>node-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<name>node-server</name>
<workingDir>../../test-server-dir</workingDir>
<waitForInterrupt>false</waitForInterrupt>
<healthcheckUrl>http://localhost:3000/</healthcheckUrl>
<arguments>
<argument>node</argument>
<argument>./app.js</argument>
</arguments>
</configuration>
</execution>
<!--Stop all processes in reverse order-->
<execution>
<id>stop-all</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop-all</goal>
</goals>
</execution>
</executions>
</plugin>
You can use the jps utility that is bundled inside the JDK to get the process ID of a running Java executable:
The
jps
tool lists the instrumented HotSpot Java Virtual Machines (JVMs) on the target system. The tool is limited to reporting information on JVMs for which it has the access permissions.
Then, when we have retrieved the process ID, we can kill it using taskkill on Windows or kill
or Unix system.
This would be a sample configuration of the maven-antrun-plugin
. It declares the jps
executable and redirect the result of its invocation in the property process.pid
with the <redirector> attribute. The result is filtered with <outputfilterchain> so that only the lines corresponding to the executable AppServer
are kept. jps
output is in the form [PID] [NAME]
so the name is then removed with <replacestring>; this way, we only keep the PID. Finally, there are two exec
configuration depending on the OS.
<configuration>
<target>
<property name="runtime_classpath" refid="maven.runtime.classpath" />
<exec executable="java" spawn="true">
<arg value="-classpath" />
<arg value="${runtime_classpath}" />
<arg value="somepackage.AppServer" />
</exec>
<exec executable="${JAVA_HOME}/bin/jps">
<arg value="-l" />
<redirector outputproperty="process.pid">
<outputfilterchain>
<linecontains>
<contains value="somepackage.AppServer" />
</linecontains>
<replacestring from=" somepackage.AppServer" />
</outputfilterchain>
</redirector>
</exec>
<exec executable="taskkill" osfamily="winnt">
<arg value="/PID" />
<arg value="${process.pid}" />
</exec>
<exec executable="kill" osfamily="unix">
<arg value="-15" />
<arg value="${process.pid}" />
</exec>
</target>
</configuration>
Since you mentioned "gracefully", I used the -15
option on Unix and didn't include the /F
option for Windows. If you want to force the exit, you can use kill -9
on the Unix system and add the /F
option on Windows.