spring - 在 POM 中访问 Spring 配置文件特定的应用程序属性

标签 spring maven spring-boot

我找不到任何解释如何在 pom.xml 中访问 spring 配置文件特定应用程序属性的内容。这可能吗?我找到了很多关于做相反事情(访问应用程序中的 Maven 配置文件属性)的信息,但没有关于此的信息。

我的具体用例是能够从如下所示的属性文件中设置 Tomcat 插件中的 Tomcat 管理器 URL。请记住,我的问题只是标题中的内容,我并不是在寻求有关配置 Tomcat 插件的帮助,因此我不会将其包含在问题标签中。

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <url>${tomcat.manager.url}</url>
        <server>${tomcat.server.name}</server>
    </configuration>
</plugin>

我的 POM 中有这些配置文件:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <activatedProperties>dev</activatedProperties>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <activatedProperties>test</activatedProperties>
        </properties>
    </profile>
</profiles>

这在同一个 POM 的构建元素中:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

在我的 application.properties 中:

# active profile placeholder for the active maven profile. Defaults to dev.
spring.profiles.active=@activatedProperties@

在我的application-test.properties中:

tomcat.manager.url=http://mydomain:8080/manager/text
tomcat.server.name=myserver

但是当我跑的时候_

mvn tomcat7:redeploy -DskipTests -P test

它不会尝试部署到 http://mydomain:8080/manager/text,而是使用默认的 localhost:8080

最佳答案

参见 read-project-properties Properties Maven Plugin 的目标:

The read-project-properties goal reads property files and URLs and stores the properties as project properties.

因此,将以下内容添加到您的 POM 中:

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0.0</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>${project.basedir}/src/main/resources/application-test.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

关于spring - 在 POM 中访问 Spring 配置文件特定的应用程序属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48690397/

相关文章:

maven - 合并 Surefire 报告

spring-boot - 使用gradle bootRun启动redirect()不会在Grails中解析@ info.app.name @

android - 如何下载带有所有依赖项的 Artifact ?

spring - Spring Boot 中用于应用程序身份验证和管理身份验证的不同凭据?

java - 无法使用 mapstruct 将 List<Source> 映射到 List<Target>

javascript - 在 iframe 中调用 jsp 页面

java - Spring Boot 不记录 CQL 查询

java - 如何为第三方枚举自定义反序列化器?

java - Spring hibernate : Getting list of objects from a list of unique values

maven - 当将null作为varargs参数发送时,Gradle和Maven的行为不同