Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

java - Maven: Different property files for different profiles

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...