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 $)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…