user755806
user755806

Reputation: 6815

Loading properties based on maven profiles?

I am using spring 3.2. I am trying to load properties file based on maven profile as below.

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

here {env} should be replaced by maven profile.but it is not replacing. am getting below exception.

org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [props/connection-{env}.properties] cannot be opened because it does not exist.

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.

any help?

Upvotes: 0

Views: 5609

Answers (4)

Natalia
Natalia

Reputation: 4532

try to add symbol $:

connection-${env}.properties

Upvotes: 2

user3487063
user3487063

Reputation: 3682

try to setup something like below in your maven for each of your profiles:

For example dev profile :

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

Upvotes: 0

Wesley Porter
Wesley Porter

Reputation: 1451

A few months ago, I was learning this very thing, of how to use maven profiles. The guide can be shown on Apache's site, but I will show what is needed below.

It looks like you set up your maven profile correctly. However, you seem to be missing your profile setting in .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:

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.

Upvotes: 0

Riduidel
Riduidel

Reputation: 22292

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)

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

Lookup specifically for calls to 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]   ....

Upvotes: 0

Related Questions