我不知道我是怎么一直错过这个的,但是 Tomcat Maven 插件现在支持生成可执行的 WAR:
http://tomcat.apache.org/maven-plugin-2.0/executable-war-jar.html
更新如果你需要 Tomcat 8,你需要一个特殊的配置。您必须使用 2.0 版本的插件,因为 2.2 Tomcat 7 插件不起作用,您需要第三方插件tomcat8-war-runner。下面我附上了您需要的 Maven 配置,它还允许您选择所需的正确版本的 Tomcat 8(tomcat.version
属性)。
Tomcat8 maven 插件处于乱七八糟的状态,仍然没有发布(请去 jira 投票并投诉他们完成它)。
最后,这个 maven 插件方法的一个主要警告是插件伪嵌入。也就是说,您的应用程序不会在与 Tomcat 相同的类加载器中运行,而是在初始化期间实际部署。这种方法有利有弊。
另一方面, Spring Boot 和Oracle 的这个示例将在与您的应用程序相同的类加载器中运行 Tomcat(我认为它是真正嵌入的)。
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<port>${tomcat.port}</port>
<path>/</path>
</configuration>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>exec-war-only</goal>
</goals>
<phase>package</phase>
<configuration>
<path>/</path>
<port>${tomcat.port}</port>
<attachArtifactClassifierType>war</attachArtifactClassifierType>
<mainClass>org.apache.tomcat.maven.runner.Tomcat8RunnerCli</mainClass>
<extraDependencies>
<extraDependency>
<groupId>com.nitorcreations</groupId>
<artifactId>tomcat8-war-runner</artifactId>
<version>1.0</version>
</extraDependency>
</extraDependencies>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-util</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-coyote</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper-el</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-el-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-tribes</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina-ha</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-annotations-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-juli</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-log4j</artifactId>
<version>${tomcat.version}</version>
</dependency>
</dependencies>
</plugin>