【问题标题】:Loading properties based on maven profiles?根据 Maven 配置文件加载属性?
【发布时间】:2014-08-19 14:01:59
【问题描述】:

我使用的是 Spring 3.2。我正在尝试根据以下 Maven 配置文件加载属性文件。

<build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
            <includes>
                <include>**/*Test.java</include>
            </includes>
        </configuration>
    </plugin>

    </plugins>
  </build>

  <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <env>dev</env>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>
    </profiles>

applicationContext.xml

<bean id="props" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:props/connection-${env}.properties"/>
    </bean>

这里的{env} 应该替换为 maven profile.but 它没有替换。低于异常。

org.springframework.beans.factory.BeanInitializationException:无法加载属性;嵌套异常是 java.io.FileNotFoundException:类路径资源 [props/connection-{env}.properties] 无法打开,因为它不存在。

我正在加载应用程序上下文:

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");

当我做 clean install {env} 值时,必须用 maven 配置文件替换。

有什么帮助吗?

【问题讨论】:

  • 在这种情况下您使用 Maven(构建时)配置文件而不是 Spring(运行时)配置文件的任何原因,或者 Maven 配置文件是否也用于其他用途?

标签: java spring maven


【解决方案1】:

尝试添加符号$

连接-${env}.properties

【讨论】:

  • 确实,这就是问题所在。
  • 我加了$。还是一样的问题
  • 你是对的。那还不够。 1. 您需要将属性添加到配置文件dev1)。 2. 向资源添加过滤src/main/resourcestrue)。在此之后,您可以通过 ${} 使用参数
【解决方案2】:

您的 applicationContext.xml 文件应该在 src/main/resources 中,并且您的 maven 构建应该包括一个配置文件部分,其中至少包含(我输入时没有检查)

    <profile>
        <id>prod</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <env>prod</env>
        </properties>
    </profile>

如果满足所有这些条件,我建议您运行mvn -X -e,它将记录您在构建过程中发生的所有事情。

专门查找对org.apache.maven.plugins:maven-resources-plugin 的调用。应该至少有一个对该插件的调用,并且它的配置应该包括

[DEBUG] Configuring mojo org.apache.maven.plugins:maven-resources-plugin:2.6:copy-resources from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-resources-plugin:2.6, parent: sun.misc.Launcher$AppClassLoader@77abfbdc]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-resources-plugin:2.6:copy-resources' with basic configurator -->
[DEBUG]   ....
[DEBUG]   (s) filtering = true
[DEBUG]   ....

【讨论】:

    【解决方案3】:

    几个月前,我正在学习如何使用 Maven 配置文件。该指南可以在 Apache 的site 上显示,但我将在下面显示需要的内容。

    您似乎正确设置了 Maven 配置文件。但是,您似乎缺少 .m2/settings.xml 中的个人资料设置:

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <environment>dev</environment>
            </properties>
        </profile>
    </profiles>
    

    或者,您可以在构建时使用以下命令设置您的 Maven 配置文件:

    mvn -Denv=dev
    

    但是如果你不想在每次构建时都指定这个标志,建议将你的配置文件放在你的 maven settings.xml 文件中。

    【讨论】:

      【解决方案4】:

      尝试在您的 Maven 中为您的每个配置文件设置如下内容:

      例如开发者简介:

      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
            <systemPropertyVariables>
               <env>dev</env>
            </systemPropertyVariables>
         </configuration>
      </plugin>
      

      测试配置文件:

      <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-surefire-plugin</artifactId>
             <configuration>
                <systemPropertyVariables>
                   <env>test</env>
                </systemPropertyVariables>
             </configuration>
          </plugin>
      

      【讨论】:

      • 那么如何下达目标呢?
      • 您可以使用 mvn -P clean install 或在您要运行的配置文件中设置类似 true 的内容,这是什么你在找吗?
      • 另外,请看一下这个问题中接受的答案:stackoverflow.com/questions/11874017/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 2012-05-27
      • 2013-09-24
      • 2017-05-06
      • 2020-06-03
      • 2018-07-15
      • 1970-01-01
      相关资源
      最近更新 更多