首页 > 解决方案 > 排除基于 maven 配置文件的嵌入式服务器

问题描述

我在 pom.xml 中定义了两个不同的配置文件(dev 和 prod)。我不想在使用 prod 配置文件构建项目时包含嵌入式服务器。我知道即使我不从 jar 中排除嵌入式服务器,我也可以将它部署在其他服务器上。

我已经使用以下代码段检查了两个排除 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>
</dependency>

我无法弄清楚如何根据所选配置文件排除它。下面是我的 POM.xml 的构建和配置文件属性。请指导。

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>application.properties</include>
                    <include>application-${profileName}.properties</include>
                    <include>*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
</build>
<profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <profileName>dev</profileName>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profileName>prod</profileName>
            </properties>
        </profile>
</profiles>

提前致谢。

标签: javaspring-bootmavenpom.xmlembedded-tomcat

解决方案


尝试这样的事情:

    <!-- Other profiles -->

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

    <!-- Other profiles -->

</profiles>

但是,您必须小心应用程序打包。也许您需要将 fat jar(用于开发环境)切换为生产环境的战争(因为您将排除嵌入式服务器)。


推荐阅读