简体   繁体   English

加载基于Maven配置文件的属性?

[英]Loading properties based on maven profiles?

I am using spring 3.2. 我正在使用Spring 3.2。 I am trying to load properties file based on maven profile as below. 我试图加载基于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 applicationContext.xml

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

here {env} should be replaced by maven profile.but it is not replacing. 这里的{env}应该被maven配置文件替换。但是不能替换。 am getting below exception. 正在低于异常。

org.springframework.beans.factory.BeanInitializationException: Could not load properties; org.springframework.beans.factory.BeanInitializationException:无法加载属性。 nested exception is java.io.FileNotFoundException: class path resource [props/connection-{env}.properties] cannot be opened because it does not exist. 嵌套的异常是java.io.FileNotFoundException:类路径资源[props / connection- {env} .properties]无法打开,因为它不存在。

I am loading application context as: 我将应用程序上下文加载为:

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

when i do clean install {env} value has to be replaced by maven profile. 当我执行clean install {env}值必须替换为Maven配置文件。

any help? 有什么帮助吗?

try to add symbol $ : 尝试添加符号$

connection-${env}.properties connection-$ {env} .properties

Your applicationContext.xml file should be in src/main/resources and your maven build should include a profiles section containing at least (I'm typing without checking) 您的applicationContext.xml文件应位于src/main/resources并且您的maven构建应包括一个配置文件部分,该部分至少包含(我输入时未检查)

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

If all these conditions are met, I suggest you run mvn -X -e which will log you all what happens during build. 如果满足所有这些条件,建议您运行mvn -X -e ,它将记录您在构建过程中发生的所有情况。

Lookup specifically for calls to org.apache.maven.plugins:maven-resources-plugin . 查找专门用于调用org.apache.maven.plugins:maven-resources-plugin There should be at least one call to that plugin and its configuration should include 该插件至少应有一个调用,其配置应包括

[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]   ....

A few months ago, I was learning this very thing, of how to use maven profiles. 几个月前,我正在学习有关如何使用Maven配置文件的知识。 The guide can be shown on Apache's site , but I will show what is needed below. 该指南可以在Apache的站点上显示,但是我将在下面显示所需的内容。

It looks like you set up your maven profile correctly. 看来您正确设置了Maven个人资料。 However, you seem to be missing your profile setting in .m2/settings.xml: 但是,您似乎缺少.m2 / settings.xml中的配置文件设置:

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

Optionally, you can set your maven profile when building with the following command: (可选)您可以在构建时使用以下命令设置Maven配置文件:

mvn -Denv=dev

But if you don't want to specify this flag everytime you build, it is recommended to place your profile in your maven settings.xml file. 但是,如果您不想在每次构建时都指定此标志,建议将您的概要文件放置在maven settings.xml文件中。

try to setup something like below in your maven for each of your profiles: 尝试在Maven中为每个配置文件设置如下所示的内容:

For example dev profile : 例如dev个人资料:

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

Test Profile: 测试资料:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Maven 属性加载顺序 - Maven properties loading order 使用Spring配置文件加载环境属性 - Loading environment properties using Spring profiles maven型材或弹簧型材? - maven profiles or spring profiles? 在 Maven 中为 Selenium/Cucumber 使用不同环境的配置文件属性 - Use profiles properties for different enviroments in Maven for Selenium/Cucumber 通过在命令行上指定多个Maven配置文件来堆叠属性 - Stacking properties by specifying multiple Maven profiles on the command line 我可以根据maven中的配置文件安装特定的依赖项 - Can I install specific dependencies based on profiles in maven 如何排除默认的application.properties在Spring启动项目的Maven中使用配置文件添加自定义属性文件? - How to exclude default application.properties add custom properties file using profiles in maven for spring boot project? Maven 配置文件 - 如何获取分配给.properties 文件中的属性的值并将它们分配给我的 class 中的变量? - Maven Profiles - How do i get the values assigned to properties in the .properties file and assign them to variables in my class? Pom中的Maven个人资料 - Maven profiles in pom Maven 配置文件相当于 Gradle - Maven profiles equivalent of Gradle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM