构建可执行 War/Jar

从插件的版本 2.0 开始,您可以使用嵌入式 Apache Tomcat7 构建可执行 war/jar。

此功能仅受 tomcat7 插件支持。

注意 您的项目必须具有 pomwar 的打包值。

要添加到您的 war 模块中的工件:

<project>
  ...
  <packaging>war or pom</packaging>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <executions>
          <execution>
            <id>tomcat-run</id>
            <goals>
              <goal>exec-war-only</goal>
            </goals>
            <phase>package</phase>
            <configuration>
              <path>foo</path>
              <!-- optional, needed 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>

要添加到您的 pom 模块中的工件:

<project>
  ...
  <packaging>war</packaging>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</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 disabled by default so use true to enable it -->
              <enableNaming>true</enableNaming>
              <!-- extra dependencies to add jdbc driver, mail jars, etc. -->
              <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>

生成的可执行 jar/war

使用上述配置,您可以执行生成的 jar,因为它具有嵌入式 Tomcat 容器

  java -jar yourjar

帮助输出

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