Search code examples
mavenmaven-2maven-cargo

Executing cargo:run from a maven profile


I am trying to write a maven profile that will run the cargo:run goal and will Start a container and wait for the user to press CTRL + C to stop. However when I run mvn clean install -PstartApplication, the command completes successfully without waiting. What am I missing?

<profile>
   <id>startApplication</id>
   <build>
      <plugins>
         <plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <configuration>
               <wait>true</wait>
               <container>
                  <containerId>tomcat7x</containerId>
                  <type>installed</type>
                  <home>${catalina.home}</home>
               </container>
               <configuration>
                  <type>standalone</type>
                  <home>${project.basedir}/target/tomcat7x</home>
               </configuration>
               <deployables>
                  <deployable>
                     <properties>
                        <context>ROOT</context>
                     </properties>
                     <groupId>com.demo.web</groupId>
                     <artifactId>sample-web-app</artifactId>
                     <type>war</type>
                  </deployable>
               </deployables>
               <executions>
                  <execution>
                     <id>start-container</id>
                     <phase>pre-integration-test</phase>
                     <goals>
                        <goal>run</goal>
                     </goals>
                  </execution>
               </executions>
            </configuration>
         </plugin>
      </plugins>
   </build>
</profile>

Solution

  • Check the plugin configuration: the executions element in the code you posted is within the configuration element, which is not correct and as such it will be ignored by Maven. (Check the official documentation for more details).
    The executions section should be out of a configuration section (and at the same nested level). They could then also include a further configuration section, which will be the configuration used by that specific wrapping execution, while the previous would be a more generic configuration applied by default to all listed executions.

    In this specific case, the Cargo Plugin also provides a further configuration element within the Maven configuration section, which makes things a bit confusing and misleading (a different name should have been chosen, in my opinion).

    Hence, in your case, you should move out the executions section from the configuration section, as following:

    <profile>
       <id>startApplication</id>
       <build>
          <plugins>
             <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <configuration>
                   <wait>true</wait>
                   <container>
                      <containerId>tomcat7x</containerId>
                      <type>installed</type>
                      <home>${catalina.home}</home>
                   </container>
                   <configuration>
                      <type>standalone</type>
                      <home>${project.basedir}/target/tomcat7x</home>
                   </configuration>
                   <deployables>
                      <deployable>
                         <properties>
                            <context>ROOT</context>
                         </properties>
                         <groupId>com.demo.web</groupId>
                         <artifactId>sample-web-app</artifactId>
                         <type>war</type>
                      </deployable>
                   </deployables>
                </configuration>
                <executions>
                  <execution>
                     <id>start-container</id>
                     <phase>pre-integration-test</phase>
                     <goals>
                        <goal>run</goal>
                     </goals>
                  </execution>
                </executions>
             </plugin>
          </plugins>
       </build>
    </profile>