当前位置: 首页 > 知识库问答 >
问题:

如何用maven配置文件设置spring活动配置文件

柴衡
2023-03-14

我有一个用maven作为构建工具的应用程序。

我正在使用maven概要文件从不同的概要文件设置不同的属性。

<profiles>
    <profile>
        <id>profile1</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
        </properties>
    </profile>
    <profile>
        <id>profile2</id>
        <properties>
        </properties>
    </profile>
    <profile>
        <id>development</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
        </properties>
    </profile>
    <profile>
        <id>production</id>
        <properties>    
        </properties>
    </profile>
</profiles>

假设我运行带有out的maven,并指定我希望spring的任何其他概要文件,将profile1development作为活动概要文件。

共有1个答案

司徒茂实
2023-03-14

对于Spring Boot应用程序,可以在pom.xml的Maven概要文件中添加一个属性,然后在application.properties中引用该属性。

将Maven概要文件添加到pom.xml中,例如,使用一个名为spring.profile.from.Maven的属性:

<profiles>
    <profile>
        <id>postgres</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <spring.profile.from.maven>postgres</spring.profile.from.maven>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
    </profile>
    <profile>
        <id>noDb</id>
        <properties>
            <spring.profile.from.maven>noDb</spring.profile.from.maven>
        </properties>
    </profile>
</profiles>

application.properties中引用Maven属性:

spring.profiles.include=@spring.profile.from.maven@

在这种设置下,使用Postgresmaven概要文件或不使用概要文件运行maven,将PostgresSpring概要文件添加到Spring的活动概要文件列表中,而使用NODBmaven概要文件运行maven,将NODBSpring概要文件添加到Spring的活动概要文件列表中。

 类似资料:
  • 问题内容: 我有一个使用maven作为构建工具的应用程序。 我正在使用Maven配置文件从不同的配置文件设置不同的属性。 我想做的是将maven中的所有活动配置文件也移植到spring活动配置文件中,以便我可以在bean签名()中引用它们。但我不确定该怎么做。 例如:考虑以下Maven设置 假设我在未指定任何其他配置文件的情况下运行maven,而我希望spring具有和 配置为活动配置文件。 问题

  • 问题内容: 我正在尝试根据某个Maven配置文件是否处于活动状态来使用数据库信息配置Spring配置文件。我已经看到了一些答案,但是很难将它们放在一起。 我有一个这样的Maven个人资料: 还有一个settings.xml文件,如下所示: 并在servlet-context.xml中: 我的问题基本上是,如何将Maven属性放入servlet- context.xml文件中?我需要一个.prope

  • 我有两个环境来设置我的API,所以我有生产和开发。我首先需要的是找到一种方法来自动完成这一任务,例如,在运行时,不发送任何参数(-d)应用程序找到了一种方法来识别环境,但我没有找到任何方法来完成这一任务。 所以我看了一个教程,在同样有一个方法放了一个环境变量,并定义了我的application.properties。所以在下面的步骤中: > 我在我的应用程序中定义了3个文件,application

  • 问题内容: 我正在尝试使用Maven 3 在Spring Boot应用程序中设置活动配置文件。在我的pom.xml中,将默认的活动配置文件和属性spring.profiles.active设置 为development: 但是每次我运行应用程序时,都会在日志中收到以下消息: 并且将SpringBoot配置文件设置为默认值(读取application.properties而不是application

  • 我有spring boot的申请。我需要将spring boot配置文件连接到maven配置文件,所以当我调用命令时 或 它应该调用spring boot加载application-dev.yml或application-prod.yml。当我打电话的时候 它应该在启动之前调用application-dev.yml文件。因此需要从2个命令调用spring boot开发配置文件。我有一个问题,每次我

  • 如果未设置,我希望默认活动配置文件为。 Spring-boot版本=1.3.5。发行版