没有tomcat嵌入式的spring boot war

nie*_*els 24 spring war maven spring-boot

我想创建一个没有嵌入式tomcat和maven的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>
...
Run Code Online (Sandbox Code Playgroud)

如果我运行mvn包我得到一个战争,其中tomcat*.jar在提供的lib文件夹中,但仍然在lib文件夹中.我读了build-tool-plugins-maven-packaging,但找不到什么问题.

我知道一个主要的想法是将其作为一个应用程序运行,我们的客户希望如何将其部署在他的应用程序服务器上.

nie*_*els 29

根据M. Deinum的提示,我排除了tomcat依赖.

使用以下pom.xml(相关代码段)maven 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>
...
Run Code Online (Sandbox Code Playgroud)

idea-user警告:您必须在运行配置中激活"包含所提供范围的依赖项"(有关更多信息,请参阅IntelliJ Idea中无法启动spring-boot应用程序)


Yon*_*tan 10

我不确定这是否是弹簧启动方式,但您可以使用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>
Run Code Online (Sandbox Code Playgroud)

使用这种方法,生成的war不可执行(不能使用java -jar在命令行上运行),但只能部署到任何servlet容器


小智 5

我有同样的需要,但删除提到的依赖项没有用。通过将此<packaging>war</packaging>依赖项添加到pom文件中,我可以获得WAR 文件。

我将此春季文章用作指导...共享,因此这也可能对其他人有所帮助。


Tad*_*egn 5

更改 spring-boot-starter 依赖项

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

到这一点将排除嵌入式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>
Run Code Online (Sandbox Code Playgroud)