spring 没有嵌入tomcat的spring boot war
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25991789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring boot war without tomcat embedded
提问by niels
I want to create a war file without embedded tomcat with maven. Here the relevant part of my pom
我想用maven创建一个没有嵌入tomcat的war文件。这是我的 pom 的相关部分
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Add tomcat only if I want to run directly -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
...
How ever if I run mvn package I get a war, where the tomcat*.jar are in a provided-lib folder but still in the lib-folder. I read build-tool-plugins-maven-packaging, but can't find what's wrong.
如果我运行 mvn package 我得到了一场War,其中 tomcat*.jar 在提供的 lib 文件夹中,但仍在 lib 文件夹中。我阅读了build-tool-plugins-maven-packaging,但找不到问题所在。
I know a main idea is to run it as an application, how ever our customer want's to deploy it on his application-server.
我知道一个主要的想法是将它作为一个应用程序运行,我们的客户希望如何将它部署在他的应用程序服务器上。
回答by niels
Following the Hint from M. Deinum I excluded the tomcat-depedency.
按照 M. Deinum 的提示,我排除了 tomcat 依赖。
With the following pom.xml (relevant snippet) a maven clean packagehas the result I want to get.
使用以下 pom.xml(相关代码段)amaven clean package得到了我想要的结果。
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Add tomcat only if I want to run directly -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
...
Warning for idea-user: You must activate "Include dependencies with the provided scope" in the run-configuration (see Unable to start spring-boot application in IntelliJ Ideafor more information)
对于idea-user 的警告:您必须在运行配置中激活“包含具有提供范围的依赖项”(有关更多信息,请参阅无法在 IntelliJ Idea 中启动 spring-boot 应用程序)
回答by Yonatan
I'm not sure if that's the spring-boot way of doing it, but you can exclude the tomcat jars using the maven-war-pluginconfiguration. That is, add the following to your pom.xml:
我不确定这是否是 spring-boot 的做法,但您可以使用maven-war-plugin配置排除 tomcat jar 。也就是说,将以下内容添加到您的 pom.xml 中:
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<packagingExcludes>WEB-INF/lib/tomcat-*.jar</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
Using this approach, the war generated is not executable (cannot be run on command line using java -jar ) but can only be deployed to any servlet container
使用这种方法,生成的War是不可执行的(不能使用 java -jar 在命令行上运行),而只能部署到任何 servlet 容器
回答by Lopestt
I had this same need but removing the mentioned dependency didn't worked. I was able to get the WAR file by adding this <packaging>war</packaging>dependency to my pom file.
我有同样的需求,但删除提到的依赖项没有用。我能够通过将此<packaging>war</packaging>依赖项添加到我的 pom 文件来获取 WAR文件。
I used this Springarticle as a guide... sharing so this may help other people as well.
我使用这篇Spring文章作为指南......分享所以这也可以帮助其他人。
回答by Tadele Ayelegn
Changing spring-boot-starter dependency from
更改 spring-boot-starter 依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
To this one will exclude the embedded tomcat server.
到这一点将排除嵌入式tomcat服务器。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
回答by zappee
I think that the easiest way to build a final WAR from your existing Spring Boot project without embedded Tomcat is the following:
我认为在没有嵌入式 Tomcat 的情况下从现有的 Spring Boot 项目构建最终 WAR 的最简单方法如下:
1) Set WARpackaging for your artifact: <packaging>war</packaging>
1) 为您的工件设置 WARpackaging: <packaging>war</packaging>
2) Set the Tomcat Server dependency to provide:
2)设置Tomcat Server依赖提供:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
If some of your existing Spring Boot dependencies contain it by default, just exclude it. Example:
如果您现有的一些 Spring Boot 依赖项默认包含它,只需排除它。例子:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
That is it.
这就对了。

