使用 Maven 的基于 Spring Boot 配置文件的 WAR

Web*_*oob 2 java deployment war maven spring-boot

我需要根据特定的环境属性文件创建 WAR 文件。

所以我创建了2个属性文件,

  • 应用程序.DEV.属性

  • 应用程序.PROD.属性

现在,当我使用 eclipse 嵌入式 tomcat 运行项目时,我传递了-Dspring.profiles.active=DEVas VM 参数。然后,当我到达端点时,我可以看到返回的 DEV 相关消息。当我将 PROD 作为参数传递时,情况也是如此。

现在,我想做的是使用 maven 命令创建一个 WAR 文件,并以加载我的特定属性文件的方式传递参数。所以我参考了谷歌和stackoverflow,找到了如下所示的各种选项,

  1. mvn clean install -Drun.profiles=DEV
  2. mvn clean install -Drun.jvmArguments="-Dspring.profiles.active=DEV"
  3. mvn clean install -Dspring.profiles.active =“DEV”
  4. mvn clean install -Dspring.profiles.active=DEV

我尝试了以上所有方法。当我输入命令时,就会生成 WAR。但它没有部署在 tomcat 上,因为它无法读取属性文件并给出错误。似乎配置文件特定的属性文件没有加载到 WAR 中。

我想知道-Dspring.profiles.active=DEV当我想使用 maven 生成 WAR 文件时有什么替代方案?

如何生成 WAR 以正确包含正确的配置文件特定属性文件?

我正在使用 Spring Boot 1.5.14.RELEASE。

Adr*_*n H 6

正如 Mickael 对此答案的评论,您可以从 Maven 文档中获取有关如何使用配置文件的帮助:https://maven.apache.org/guides/introduction/introduction-to-profiles.html

使用 Maven 选择配置文件的常用方法是

mvn -Pprod package
Run Code Online (Sandbox Code Playgroud)

其中 prod 是您的个人资料的名称。如果你想使用 dev 配置文件进行构建,那就是

mvn -Pdev package
Run Code Online (Sandbox Code Playgroud)

pom.xml此类配置文件在您的文件中定义project>profiles>profile。在那里,您可以指定包装选项。

这是这样一个简介:

<profile>
    <id>dev</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <!-- log configuration -->
        <logback.loglevel>DEBUG</logback.loglevel>
        <!-- default Spring profiles -->
        <spring.profiles.active>dev${profile.no-liquibase}</spring.profiles.active>
    </properties>
</profile>
Run Code Online (Sandbox Code Playgroud)

  • `mvn clean install -Pdev` **不是** `mvn clean install -Dspring.profiles.active=dev` 的替代品 (3认同)