0

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?

kryger
  • 12,906
  • 8
  • 44
  • 65
user755806
  • 6,565
  • 27
  • 106
  • 153
  • 1
    Any reason you're using Maven (build time) profiles instead of Spring (runtime) profiles in this case, or are the Maven profiles also used for other things? – Emerson Farrugia Aug 19 '14 at 14:50

4 Answers4

2

try to add symbol $:

connection-${env}.properties

catch23
  • 17,519
  • 42
  • 144
  • 217
Natalia
  • 4,362
  • 24
  • 25
0

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]   ....
Riduidel
  • 22,052
  • 14
  • 85
  • 185
0

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.

Wesley Porter
  • 1,401
  • 2
  • 13
  • 15
0

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>
user3487063
  • 3,672
  • 1
  • 17
  • 24
  • then how to issue the goal ? – user755806 Aug 19 '14 at 14:37
  • you can use mvn -P clean install or set something like true in your profile which you want to run, is this what you are looking for? – user3487063 Aug 19 '14 at 14:58
  • Also, please have a look at the accepted answer in this question:http://stackoverflow.com/questions/11874017/controlling-a-project-with-maven-and-spring-how-to-set-spring-config-file-using – user3487063 Aug 19 '14 at 15:16