javamavenkill-processmaven-antrun-plugin

Gracefully stopping a java process started by maven-antrun-plugin


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

So as answered in the above question I now have a maven-antrun-plugin that forks a child process and runs my java appserver using a configuration like this -

<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
  <phase> verify </phase>
  <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>  
    </target>
  </configuration>
  <goals>
    <goal>run</goal>
  </goals>
</execution>
</executions>
</plugin>

The above configuration smoothly starts my appserver as a background process.

Now my question is if there is an easy way I can locate this process and stop it if I need after I start it through my build.


Solution

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