根据maven配置文件加载属性?

时间:2014-08-19 14:01:59

标签: java spring maven

我正在使用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替换。但它不会被替换。我低于例外。

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

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

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

当我执行clean install {env}值必须由maven个人资料替换。

任何帮助?

4 个答案:

答案 0 :(得分:2)

尝试添加符号$

  

连接 - $ {ENV}的.properties

答案 1 :(得分:0)

您的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]   ....

答案 2 :(得分:0)

几个月前,我正在学习如何使用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文件中。

答案 3 :(得分:0)

尝试在您的maven中为每个配置文件设置类似下面的内容:

例如dev个人资料:

<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>