【问题标题】:how to inject maven profile's properties in application.properties with spring boot如何使用spring boot在application.properties中注入maven配置文件的属性
【发布时间】:2017-06-17 00:15:41
【问题描述】:

我在 pom.xml 中定义了三个配置文件

<profiles>
    <profile>
        <id>development</id>

        <properties>
            <activatedProperties>development</activatedProperties>
            <env.identifier>dev</env.identifier>

        </properties>

        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>

    <profile>
        <id>qualification</id>

        <properties>
            <activatedProperties>qualification</activatedProperties>
            <env.identifier>klif</env.identifier>

        </properties>
    </profile>

    <profile>
        <id>production</id>

        <properties>
            <activatedProperties>production</activatedProperties>
            <env.identifier>prod</env.identifier>

        </properties>
    </profile>
</profiles>

我还将构建节点修改为:

<build>
    <finalName>myApplication</finalName>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

我创建了三个属性文件,并将此模式命名为:application-[profileId].properties。 在 application.properties 这是我定义的一般属性:

spring.profiles.active=@activatedProperties@
指定 spring boot 使用哪个配置文件。 我还为我的日志文件定义了一个文件,如下所示:
logging.file=logs/facade_@env.identifier@.log
问题是,当我启动我的应用程序时,即使指定了配置文件,我总是将其作为我的 env.identifier 开发人员的值。 如何解决这个问题?谢谢

【问题讨论】:

  • 你不应该这样做。不要使用 Maven 配置文件来创建多个工件。这基本上意味着您将使用未经测试的工件进行生产。只需在针对不同环境启动应用程序时指定这些参数,而不是尝试为此使用 maven 配置文件。

标签: java spring maven spring-boot properties-file


【解决方案1】:

更新

您可能缺少 maven 资源插件:

<plugins>


      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
         <configuration>
           <delimiters>
             <delimiter>@</delimiter>
           </delimiters>
           <useDefaultDelimiters>false</useDefaultDelimiters>
         </configuration>
      </plugin>
      ...
    </plugins>

然后在你的命令中你只需要添加:

mvn resources:resources

【讨论】:

  • 你能解释一下这个插件是做什么的吗?
  • 你可以去这里maven.apache.org/plugins/maven-resources-plugin。在您已经指定的某些资源上,我总是将它与 true 结合使用。在你的属性文件中,我认为你有 ${env.properties} 而不是@env.properties@。试一试
  • 不幸的是,它不起作用。语法 @env.properties@ 是 Spring Boot 中的新语法。它不再识别 ${}
  • 还是什么都没有。事实上,spring boot 似乎无法从 pom.xml 中注入任何东西:/ 我不知道我做错了什么......
  • 通常我只是生成我的 jar 并启动它,我会执行以下操作:mvn clean package,然后我使用 java -jar application.jar -D profileName 运行我的应用程序。我还尝试了您的资源:资源,但没有达到我们的预期
猜你喜欢
  • 2016-08-10
  • 2017-08-27
  • 2016-08-24
  • 2021-11-24
  • 2018-10-24
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多