title: How to make maven wait till tomcat stops
body: <p>I am looking for a way to wait till my tomcat is stopped properly.</p>
  
  <p>What I do:</p>
  
  <p>I am starting my tomcat with a jacoco agent  pointing to a jacoco.exec file. The coverage data is written to the exec file at the tomcat shutdown.</p>
  
  <p>So I want to wait till the tomcat is properly shut down in my maven projects.</p>
  
  <p>When I Start tomcat I use maven wait to listen till the server responds for a URL.</p>
  
  <pre><code>&lt;profile&gt;
      &lt;id&gt;start-tomcat&lt;/id&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                  &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.2.1&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;start-tomcat&lt;/id&gt;
                          &lt;phase&gt;initialize&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;exec&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;executable&gt;${tomcat.home}/bin/catalina.sh&lt;/executable&gt;
                              &lt;arguments&gt;
                                  &lt;argument&gt;jpda&lt;/argument&gt;
                                  &lt;argument&gt;start&lt;/argument&gt;
                              &lt;/arguments&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.sonatype.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;wait-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.0&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;tomcat-server&lt;/id&gt;
                          &lt;phase&gt;initialize&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;wait&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;protocol&gt;http&lt;/protocol&gt;
                              &lt;host&gt;${mc.host}&lt;/host&gt;
                              &lt;port&gt;${tomcat.port}&lt;/port&gt;
                              &lt;file&gt;/application-url.jsf&lt;/file&gt;
                              &lt;maxcount&gt;20&lt;/maxcount&gt;
                              &lt;timeout&gt;10000&lt;/timeout&gt;
                              &lt;read&gt;true&lt;/read&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
      &lt;activation&gt;
          &lt;activeByDefault&gt;false&lt;/activeByDefault&gt;
      &lt;/activation&gt;
  &lt;/profile&gt;
  </code></pre>
  
  <p>This works fine with the server start.</p>
  
  <p>But for the stop I can only do </p>
  
  <pre><code>&lt;profile&gt;
      &lt;id&gt;stop-tomcat&lt;/id&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                  &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.2.1&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;id&gt;stop-tocat&lt;/id&gt;
                          &lt;phase&gt;initialize&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;exec&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;executable&gt;${tomcat.home}/bin/catalina.sh&lt;/executable&gt;
                              &lt;arguments&gt;
                                  &lt;argument&gt;stop&lt;/argument&gt;
                                  &lt;argument&gt;-force&lt;/argument&gt;
                              &lt;/arguments&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
      &lt;activation&gt;
          &lt;activeByDefault&gt;false&lt;/activeByDefault&gt;
      &lt;/activation&gt;
  &lt;/profile&gt;
  </code></pre>
  
  <p>Which issues the command and the shell exits after that while making the shutting down a background process.</p>
  
  <p>But by the time the jacoco.exec has not finalized </p>
  
  <p>Can someone please help.</p>