我不知道我是如何保持缺少這一點,但Tomcat的Maven插件現在支持生成一個可執行WAR :
http://tomcat.apache.org/maven-plugin-2.0/executable-war-jar.html
UPDATE,如果你需要一個Tomcat 8,你需要一個特殊的配置。您的必須使用插件的2.0版本,因爲2.2 Tomcat 7插件無法正常工作,您需要第三方插件tomcat8-war-runner。下面我附加了你需要的maven配置,這也允許你選擇你想要的正確版本的Tomcat 8(tomcat.version
屬性)。
Tomcat8 maven插件處於混亂狀態,仍然未被釋放(請在jira上投票並投訴給他們完成)。
最後,這個maven插件方法的一個主要警告是插件僞嵌入。那就是你的應用程序不會像Tomcat一樣運行在相同的類加載器中,而是在初始化期間實際部署的。這種方法有利有弊。
另一方面,Spring Boot和example from 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>
在webapp中嵌入tomcat ...爲了什麼? – aviad
對不起,我錯過了這個問題。我的意思是結合,而不是嵌入到webapp本身。胖罈子的想法是[簡化DevOps](http://dropwizard.codahale.com/getting-started/#building-fat-jars)。所有你需要運行的應用程序是'java -jar my-webapp.jar'。 –
這裏是另一個Fat Jar創建的例子:http://one-jar.sourceforge.net/ –