Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminate java app launched from maven

Tags:

java

jvm

maven

I use the exec-maven-plugin to execute a java application :

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>upload-res</id>
            <phase>package</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>java</executable>
                <arguments>
                                        <argument>${jvmEncoding}</argument>
                                        <argument>${jvmMaxPermSize}</argument>
                    <argument>-classpath</argument>
                    <classpath />
                    <argument>${myClass}</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

The application can potentially run for some minutes depending on the internet connection. If I simply terminate:

mvn clean install

before the lauched app is finished it still runs in the background. Is it possible to terminate the java application if the maven build is interrupted?

like image 364
u123 Avatar asked Nov 25 '25 18:11

u123


1 Answers

The difficulty is that the JVM is launched in a separate process, and you can no longer communicate with it using straight within-JVM Java code. You need some kind of inter-process communication. There are two ways to do it: 1) have your app listen on a port for a signal, and have it terminate itself, or 2) record the pid of the process somewhere, and then issue an operating system kill command. Both are kind of ugly.

The best approach depends on what the app is. We use option 1 when we launch Jetty for debugging. Some app servers will listen for a terminate signal automatically.

like image 97
ccleve Avatar answered Nov 28 '25 08:11

ccleve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!