Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: Different property files for different profiles

Tags:

java

maven-2

I'm using different maven profiles to deploy my application to different environments. (With the weblogic-maven-plugin, but I think this is not important)

In the application I use Spring Web Services. Now I'd like to change the endpoint depending on the environment. (The endpoint is defined in Spring's applicationContext.xml)

My idea is to read the value from a property file. This property file will be written (or copied) during Mavens package phase.

Is this a good idea?

If yes: How do I create this property (or the whole file) with maven.

If no: What would be a better aproach to solve this problem?

like image 671
Sebi Avatar asked Nov 30 '11 11:11

Sebi


1 Answers

I achieved something similar, but having the variables in the pom.xml instead in propreties files. So my properties files have variables that would be changed by Maven in packaging.

First I defined these vars in the profiles section of the pom:

<profiles>
    <profile>
        <id>dev</id>
        <activation><activeByDefault>true</activeByDefault></activation>
        <properties>
            <props.debug>true</props.debug>
            <props.email>false</props.email>
                            <what.ever.you.want>value for dev profile</what.ever.you.want>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <props.debug>false</props.debug>
            <props.email>true</props.email>
            <what.ever.you.want>value for prod profile</what.ever.you.want>
        </properties>
    </profile>
</profiles>

Then activated the maven procesing and filtering of resources. So in your build section:

        <build>
                    <resources>
        <resource>
             <directory>src/main/resources</directory>
             <filtering>true</filtering>
        </resource>
    </resources>
             </build>

Finally I can have "vars" in my properties files, configuration files. For example, In my project I have a email.properties file for configuring the emails sending. The property "sendEmail" indicates if I have to send the email (prod profile) or to print it in debug (dev profile). With dev profile this var will be put to false whereas with the profile prod the property will have the true value.

sendEmail=${props.email}

This not only works with properties files, also with XML (I guess with every text file)

The contras are:

  • Part of the configuration is dispersed along in the pom file
  • Maven packaging lasts more (because the filtering)
  • Putting vars in XML files makes them syntaxlly erroneous (becouse the character $)
like image 109
polypiel Avatar answered Sep 23 '22 13:09

polypiel