【问题标题】:Exclude embedded server based on maven profile排除基于 maven 配置文件的嵌入式服务器
【发布时间】:2020-04-17 23:48:56
【问题描述】:

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

我已经检查了两个如何使用下面的 sn-p 排除 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>

提前致谢。

【问题讨论】:

  • 我不明白您为什么要使用 maven 配置文件而不是 Spring Boot 配置文件来实现您的目的......如果您需要制作一个带有嵌入式服务器的服务器和一个没有您应该制作两个不同的它的工件,也可以非常干净地处理不同的依赖关系。我强烈反对使用具有不同依赖项的配置文件。如果您想构建版本,这将导致问题...
  • @khmarbaise 我使用了 maven 配置文件,因为我想在打包期间根据配置文件排除 application.properties。我相信 spring 配置文件和 maven 配置文件是不同的东西。如果我错了,请纠正我。

标签: java spring-boot maven pom.xml embedded-tomcat


【解决方案1】:

尝试在您的配置文件中使用依赖项标签,仅包含您想要该配置文件的依赖项。

<profiles>
    <profile>
        <id>dev</id>
        <dependencies>
            // do stuff
        </dependencies>
    </profile>
</profiles>

【讨论】:

    【解决方案2】:

    试试这样的:

        <!-- 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(用于开发环境)切换为生产环境的战争(因为您将排除嵌入式服务器)。

    【讨论】:

      猜你喜欢
      • 2015-05-31
      • 2011-10-22
      • 2012-09-16
      • 1970-01-01
      • 2012-10-23
      • 2016-01-30
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      相关资源
      最近更新 更多