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

前端 未结 2 915
栀梦
栀梦 2021-01-14 14:53

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<

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-14 15:38

    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 attribute. The result is filtered with 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 ; this way, we only keep the PID. Finally, there are two exec configuration depending on the OS.

    
        
            
            
                
                
                
            
            
                
                
                    
                        
                            
                        
                        
                    
                
            
            
                
                
            
            
                
                
            
        
    
    

    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.

提交回复
热议问题