如何在Spring Boot应用程序中排除嵌入式Tomcat

15

我们如何从Spring Boot应用程序中排除嵌入式的Tomcat服务器,以便可以在JBoss服务器上运行该jar包?


2
您无需将其排除,它将在服务器上运行并可执行。 - M. Deinum
1
好的,这意味着即使我们在生产服务器上运行jar包,在我们的情况下我们正在使用Jboss,它也会自动部署在jboss而不是Tomcat上? - Deepesh Rathore
1
我原本以为是一个war文件而不是jar文件。你至少应该将依赖项的范围设置为“provided”,这样它们就会被移动到另一个目录中。该目录由Spring Boot使用,但在部署文件时不使用。 - M. Deinum
对于jar文件,它会做什么呢?它会部署在嵌入式Tomcat上还是在JBoss上部署? - Deepesh Rathore
7个回答

19

您可以在pom文件中进行排除:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>tomcat-embed-el</artifactId>
            <groupId>org.apache.tomcat.embed</groupId>
        </exclusion>
        <exclusion>
            <artifactId>tomcat-embed-core</artifactId>
            <groupId>org.apache.tomcat.embed</groupId>
        </exclusion>
        <exclusion>
            <artifactId>tomcat-embed-websocket</artifactId>
            <groupId>org.apache.tomcat.embed</groupId>
        </exclusion>
    </exclusions>
</dependency>
您可以跟随这个链接(附有屏幕截图):link Spring文档中关于嵌入式Servlet容器的内容请参考:Spring documentation on Embedded servlet container

当我按照上述建议运行我的应用程序时,它会给我一个错误:“在关闭时注销JMX公开的Bean”。 - Tatkal

10
您可以按照以下方式修改您的 POM.xml 文件:
  <!-- Removing the dependency for the embedded tomcat -->
            <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>

2

另一种方法是在你的pom.xml中将tomcat依赖项的scope标记为provided

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-websocket</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <scope>provided</scope>
    </dependency>

1
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <excludeGroupIds>org.apache.tomcat.embed</excludeGroupIds>
    </configuration>
</plugin>

您可以查看本文。依赖排除


1
在依赖项中添加<exclusions>标签,其中<artificatId>为'spring-boot-starter-web'。
<exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
</exclusions>

你的最终依赖项应该如下所示:

<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>
</dependencies>

0

最好提到

在pom.xml文件中提供的范围

从Spring Boot Maven应用程序中排除嵌入式Tomcat服务器。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

0
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
        <exclusions>
           <exclusion>
              <artifactId>tomcat-embed-el</artifactId>
              <groupId>org.apache.tomcat.embed</groupId>
           </exclusion>
           <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-web</artifactId>        
        <exclusions>
           <exclusion>
              <artifactId>tomcat-embed-el</artifactId>
              <groupId>org.apache.tomcat.embed</groupId>
           </exclusion>
           <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-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

对于Spring Boot 2.7.12版本的示例,请参考以下内容。如果您正在使用验证功能,请不要忘记进行排除。 附注:不要从目标文件夹复制war文件。它有一个lib-provided文件夹。请使用maven-war-plugin并从其输出文件夹复制。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接