Search code examples
maventomcatmaven-cargo

Create A War With Bundled Tomcat


My project structure looks like this:

+Parent  
|   
+---dist
|   
+---module1 (war)
|   
+---module2 (jar)

I want the dist module to package the war from module1 with an instance of Tomcat and zip it all up so that I can deploy it by unzipping on the server.

I know about plugins that create runnable wars and embedded tomcat that starts from a jar but I want the whole Tomcat in a zip with my war in the webapps dir.

I could potentially do it by hand with the assembly plugin but the cargo plugin seems like it should do what I want.

My dist pom looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>my-parent</artifactId>
    <groupId>com.my.package</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>dist</artifactId>
  <packaging>pom</packaging>

  <dependencies>
    <dependency>
      <groupId>${project.parent.groupId}</groupId>
      <artifactId>my-war</artifactId>
      <version>${project.parent.version}</version>
      <type>war</type>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.cargo</groupId>
        <artifactId>cargo-maven2-plugin</artifactId>
        <version>1.4.15</version>
        <configuration>
          <container>
            <!--containerId must be equal to one of the containers supported by Cargo -->
            <!--https://codehaus-cargo.github.io/cargo/Container.html-->
            <containerId>tomcat8x</containerId>
            <artifactInstaller>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat</artifactId>
            <version>8.0.24</version>
            </artifactInstaller>
          </container>
          <configuration>
            <type>standalone</type>
            <home>${basedir}/target/cargo/installs/tomcat-8.0.24</home>
          </configuration>
          <deployables>
            <deployable>
              <groupId>${project.parent.groupId}</groupId>
              <artifactId>my-war</artifactId>
              <type>war</type>
            </deployable>
          </deployables>
        </configuration>
        <executions>
          <execution>
            <id>package</id>
            <phase>package</phase>
            <goals>
              <goal>package</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

First of all it seems strange that I have to specify the home dir when I am using the artifact installer.

Second, the output structure looks like this:

+---dist
|   \---target
|       +---cargo
|       |   \---installs
|       |       \---tomcat-8.0.24
|       |           \---apache-tomcat-8.0.24
|       |               +---bin
|       |               +---conf
|       |        ..........
|       |        ..........
|       |              
|       \---package
|           +---apache-tomcat-8.0.24
|           |   +---bin
|           |   +---conf
|           |   ..........
|           |   ..........
|           +---bin
|           +---lib
|           \---temp

Note how the tomcat dir is a sub-dir of package but there is already a stub of the tomcat dir the package dir.

Third and most importantly, my war isn't anywhere in the dist/target dir!


Solution

  • I'm not sure where that extra dir was coming from but it has resolved now. I think I might have been running package from the dist module instead of the root module which is why the war wasn't there.

    The following sample works for me then I add the maven assembly plugin to zip it up.

     <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.stackoverflow</groupId>
      <artifactId>question</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>war</packaging>
    
      <properties>
        <tomcat.version>8.0.24</tomcat.version>
      </properties>
    
      <build>
        <plugins>
          <!--Create a war-->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <!--This is an empty demo project-->
            <configuration>
              <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
          </plugin>
          <!--Create the Tomcat bundle with our war in it-->
          <plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <version>1.4.15</version>
            <configuration>
              <container>
                <!--containerId must be equal to one of the containers supported by Cargo -->
                <!--https://codehaus-cargo.github.io/cargo/Container.html-->
                <containerId>tomcat8x</containerId>
                <artifactInstaller>
                  <groupId>org.apache.tomcat</groupId>
                  <artifactId>tomcat</artifactId>
                  <version>${tomcat.version}</version>
                </artifactInstaller>
              </container>
              <configuration>
                <type>standalone</type>
                <home>${project.build.directory}/cargo/installs/tomcat-${tomcat.version}/apache-tomcat-${tomcat.version}
                </home>
              </configuration>
              <deployables>
                <deployable>
                  <groupId>${project.groupId}</groupId>
                  <artifactId>${project.artifactId}</artifactId>
                  <type>war</type>
                </deployable>
              </deployables>
            </configuration>
            <executions>
              <execution>
                <id>cargo-deploy</id>
                <phase>package</phase>
                <goals>
                  <goal>deploy</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>