How to stop Maven Execution

26.9k views Asked by At

I have a mock server which I need to start in Jenkins before running my Automation tests and then stop the mock server after my tests have run.

This mock server is a maven project and is using exec-maven-plugin. I am able to start this server by running the command mvn exec:java. However I am unable to stop this maven project through Jenkins. I have read about it and most answers tell to find the Process ID of this Maven project and then kill it. Is there a cleaner and easier way to stop this project ?

3

There are 3 answers

0
Robert Scholte On

The key to this issue is indeed that you don't know the processId. Consider using exec:exec instead with async. You'll see that asyncDestroyOnShutdown is set to true by default, meaning it'll shut down once Maven is shutting down.

0
Essex Boy On

You need to start and stop your sever using goals which are part of the maven life cycle.

An example taken from this answer

<plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
       <executions>
          <execution>
            <id>tomcat-run</id>
            <goals>
              <goal>run-war-only</goal>
            </goals>
            <phase>pre-integration-test</phase>
                <configuration>

                    <fork>true</fork> 
                </configuration>

           </execution>
           <execution>
            <id>tomcat-shutdown</id>
            <goals>
              <goal>shutdown</goal>
            </goals>
            <phase>post-integration-test</phase>
          </execution>
        </executions>
      </plugin>
0
ankit On

If you are running linux:

ps aux |grep <project-name> and then kill -9 <process id>