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

时光毁灭记忆、已成空白 提交于 2019-12-01 07:06:16

问题


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.


回答1:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/35453479/gracefully-stopping-a-java-process-started-by-maven-antrun-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!