0

Using Maven, I need to automatically deploy a web application to a Tomcat server, then run a MainClass in order to do some post-deploy operations.

These two things alone are already working, through cargo-maven2-plugin respectively exec-maven-plugin. However I don't know how bind them together.

I see two options:

  • Making the "official" maven deploy goal to simply execute cargo-plugin, then exec-maven and nothing else

  • Binding the execution of exec-maven to the completion of cargo:deploy

The first is my favouire. Unfortunately I don't know how to implement any of them.

The current pom.xml:

<plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <version>1.4.15</version>
    <configuration>
        <container>
            <containerId>tomcat8x</containerId>
            <type>remote</type>
            <systemProperties>
                <cargo.jvmargs>-XX:MaxPermSize=256M -Xmx1024m</cargo.jvmargs>
            </systemProperties>
        </container>
        <configuration>
            <type>runtime</type>
            <properties>
                <cargo.hostname>${my.hostname}</cargo.hostname>
                <cargo.servlet.port>${my.port}</cargo.servlet.port>
                <cargo.tomcat.manager.url>${my.hostname}/manager</cargo.tomcat.manager.url>
                <cargo.remote.username>tomcat</cargo.remote.username>
                <cargo.remote.password>tomcat</cargo.remote.password>
            </properties>
        </configuration>
        <deployables>
            <deployable>
                <location>${project.build.directory}/${project.build.finalName}.war</location>
                <type>war</type>
                <properties>
                    <context>/${project.build.finalName}</context>
                </properties>
            </deployable>
        </deployables>
    </configuration>
</plugin>

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.4.0</version>
    <executions>
        <execution>
            <!-- NEED TO BE AFTER DEPLOY -->
            <phase>package</phase>
            <goals>
                <goal>java</goal>
            </goals>
            <configuration>
                <mainClass>ch.MainClass</mainClass>
                <arguments>
                    <argument>Will be forwarded to main()</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>
Shepard
  • 801
  • 3
  • 9
  • 17

1 Answers1

1

I would suggest to bind cargo-maven2-plugin to the pre-integration-test phase and the exec-maven-plugin in your case to the integration-test phase which is after the package phase. See also the documentation about the default life cycle phases.

The deploy phase is usually used to deploy the generated artifacts to a maven repository so it does not really make sense to bind running an integration test to this phase.

<plugin>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
    <executions>
        <execution>
            <phase>integration-test</phase>
            <goals>
                <goal>xxxx</goal>
            </goals>
            <configuration>
               ....
            </configuration>
        </execution>
    </executions>
</plugin>

The above configuration can be applied to both of your plugins exec-maven-plugin as well as cargo-maven2-plugin...

The best approach is to separate such integration test scenarios into a separate module or if you have only a single module use a profile to active integration tests.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235