Build an Executable War/Jar

Since version 2.0 you can now build an executable war/jar with an embedded Apache Tomcat7.

This is only supported with the tomcat7 plugin.

Note Your project must have packaging pom or war.

Additional Artifact to your war module

<project>
  ...
  <packaging>war or pom</packaging>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.0</version>
        <executions>
          <execution>
            <id>tomcat-run</id>
            <goals>
              <goal>exec-war-only</goal>
            </goals>
            <phase>package</phase>
            <configuration>
              <path>foo</path>
              <!-- optional only if you want to use a preconfigured server.xml file -->
              <serverXml>src/main/tomcatconf/server.xml</serverXml>
              <!-- optional values which can be configurable -->
              <attachArtifactClassifier>default value is exec-war but you can customize</attachArtifactClassifier>
              <attachArtifactClassifierType>default value is jar</attachArtifactClassifierType>
            </configuration>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>

Additional Artifact to your pom module

<project>
  ...
  <packaging>war</packaging>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.0</version>
        <executions>
          <execution>
            <id>tomcat-run</id>
            <goals>
              <goal>exec-war-only</goal>
            </goals>
            <phase>package</phase>
            <configuration>
              <!-- optional only if you want to use a preconfigured server.xml file -->
              <!--
              <serverXml>src/main/tomcatconf/server.xml</serverXml>
              -->
              <warRunDependencies>
                <warRunDependency>
                  <dependency>
                    <groupId>a groupId</groupId>
                    <artifactId>and artifactId</artifactId>
                    <version>version</version>
                    <type>war</type>
                  </dependency>
                  <contextPath>/</contextPath>
                </warRunDependency>
              </warRunDependencies>
              <!-- naming is disable by default so use true to enable it -->
              <enableNaming>true</enableNaming>
              <!-- extra dependencies to add jdbc driver mail jars -->
              <extraDependencies>
                <extraDependency>
                  <groupId>org.apache.derby</groupId>
                  <artifactId>derby</artifactId>
                  <version>10.1.3.1</version>
                </extraDependency>
                <extraDependency>
                  <groupId>javax.mail</groupId>
                  <artifactId>mail</artifactId>
                  <version>1.4</version>
                </extraDependency>
              </extraDependencies>
            </configuration>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>

Generated executable jar/war

You will be able to execute the generated jar which contains an embedded tomcat container

  java -jar yourjar

Help output

usage: java -jar [path to your exec war jar]
 -ajpPort <ajpPort>                     ajp port to use
 -clientAuth                            enable client authentication for
                                        https
 -D <arg>                               key=value
 -extractDirectory <extractDirectory>   path to extract war content,
                                        default value: .extract
 -h,--help                              help
 -httpPort <httpPort>                   http port to use
 -httpProtocol <httpProtocol>           http protocol to use: HTTP/1.1 or
                                        org.apache.coyote.http11.Http11Nio
                                        Protocol
 -httpsPort <httpsPort>                 https port to use
 -keyAlias <keyAlias>                   alias from keystore for ssl
 -loggerName <loggerName>               logger to use: slf4j to use slf4j
                                        bridge on top of jul
 -obfuscate <password>                  obfuscate the password and exit
 -resetExtract                          clean previous extract directory
 -serverXmlPath <serverXmlPath>         server.xml to use, optional
 -X,--debug                             debug