【问题标题】:Gracefully stopping a java process started by maven-antrun-plugin优雅地停止由 maven-antrun-plugin 启动的 java 进程
【发布时间】:2016-05-28 23:12:31
【问题描述】:

这个问题是昨天对这个问题的回答的结果

run a java application and a web application in a single maven build within a reactor project

所以正如上面问题中的回答,我现在有一个 maven-antrun-plugin,它分叉一个子进程并使用这样的配置运行我的 java appserver -

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

以上配置顺利启动我的appserver作为后台进程。

现在我的问题是,在我通过构建启动它之后,是否有一种简单的方法可以找到这个过程并在需要时停止它。

【问题讨论】:

    标签: java maven kill-process maven-antrun-plugin


    【解决方案1】:

    您可以使用捆绑在 JDK 中的 jps 实用程序来获取正在运行的 Java 可执行文件的进程 ID:

    jps 工具列出了目标系统上检测的 HotSpot Java 虚拟机 (JVM)。该工具仅限于报告有关其具有访问权限的 JVM 的信息。

    然后,当我们检索到进程 ID 后,我们可以在 Windows 或 kill 或 Unix 系统上使用 taskkill 杀死它。

    这将是maven-antrun-plugin 的示例配置。它声明了jps 可执行文件,并使用&lt;redirector&gt; 属性在process.pid 属性中重定向其调用结果。结果用&lt;outputfilterchain&gt; 过滤,因此只保留与可执行文件AppServer 对应的行。 jps 输出格式为[PID] [NAME],因此名称随后会被&lt;replacestring&gt; 删除;这样,我们只保留PID。最后,有两个exec 配置取决于操作系统。

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

    由于您提到“优雅地”,我在 Unix 上使用了 -15 选项,而在 Windows 上没有包含 /F 选项。如果要强制退出,可以在Unix系统上使用kill -9,在Windows上添加/F选项。

    【讨论】:

    • 如果这行得通.. 将是相当令人印象深刻.. 你能确认它适用于你吗?我现在要试试:)
    • @Gautam 是的,我通过启动一个运行 while(true); 的 Java 类进行了测试,它正确地杀死了它:)。
    【解决方案2】:

    尝试了各种选项后,我发现 process-exec-maven-plugin 是最好的。我意识到 OP 是专门询问 maven-antrun-plugin 的,但这是因为收到了对一般问题的回答。

    https://github.com/bazaarvoice/maven-process-plugin

    使用它来启动 nodeJs 服务器的示例,该服务器用作集成测试的一部分:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 2019-11-10
      • 2020-04-07
      • 1970-01-01
      • 2010-11-05
      相关资源
      最近更新 更多