Using Ant to Combine Two Different Property Files

I have a default properties file and some properties of specific properties for deployment that override specific default settings based on the deployment environment. I would like my Ant build script to combine the two properties files (overwriting the default values ​​with the specific deployment values), and then output the resulting properties to a new file.

I tried to do it like this, but I was unsuccessful:

<target depends="init" name="configure-target-environment"> <filterset id="application-properties-filterset"> <filtersfile file="${build.config.path}/${target.environment}/application.properties" /> </filterset> <copy todir="${web-inf.path}/conf" file="${build.config.path}/application.properties" overwrite="true" failonerror="true" > <filterset refid="application-properties-filterset" /> </copy> </target> 
+7
source share
5 answers

I did it like this:

 <property prefix="app.properties" file="custom.application.properties" /> <property prefix="app.properties" file="default.application.properties" /> <echoproperties destfile="application.properties"> <propertyset> <propertyref prefix="app.properties"/> <mapper type="glob" from="app.properties.*" to="*"/> </propertyset> </echoproperties> 
+3
source

Perhaps you should look at the concat ant task for this.

+2
source

I get it. It is necessary to create an additional properties file with each key / value in the following format: mail.server.host = @ mail.server.host @, etc.

Then specify this "template" file in the "file" attribute of the task. Also, in the filter set, specify a few with the least important, indicated first.

So it will look like this:

 <copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" > <filterset refid="application-properties-filterset" /> </copy> 

+2
source

I personally use this:

 <copy todir="${web-inf.path}/conf" filtering="true"> <fileset dir="${build.config.path}" includes="*.properties" /> <filterset> <filtersfile file="application-properties-filterset" /> </filterset> </copy> 
0
source

The other answers are fine, but I need one without these restrictions:

  • Need to specify all properties as templates with @tokens @ (first answer)
  • Property extension - for example. I have properties defined as prop2 = $ {prop1} that will be extended by any solution that loads and stores echo properties.
  • EchoProperties (@ user2500146) avoids characters, such as colons, that annoy URL properties (not Ant error, these are standard Java properties that allow: instead of =)
  • Duplicate properties of concat-based solutions (this works because the second definition is ignored, but I didn't want repetitions

In the end, I had to resort to javascript in the filter, but my solution brings the default properties if and only if they are not defined in the main properties file. It works by loading basic properties using an obscure prefix, then copying it to the destination, and then merging the default properties when filtering any default properties that were loaded in the first step.

You can use this verbatim form, but you probably want to display the log instructions or change them to the debug level as soon as you make sure

 <!-- merge the main.properties.file with the default.properties.file into the output.properties.file (make sure these are defined) --> <target name="merge"> <!--Obscure enough prefix to ensure the right props are handled--> <property name="prefix" value="__MY_PREFIX__"/> <!--Load the main properties so we can tell if the default is needed--> <property prefix="${prefix}" file="${main.properties.file}"/> <!--Copy the main properties, then append the defaults selectively--> <copy file="${main.properties.file}" tofile="${output.properties.file}" overwrite="true"/> <concat destfile="${output.properties.file}" append="true"> <fileset file="${default.properties.file}"/> <filterchain> <!--Filter out lines with properties that were already in the main properties --> <scriptfilter language="javascript"> <![CDATA[ var line = self.getToken(); project.log("line: " + line); var skipLine = false; // lines that do not define properties are concatenated if (line.indexOf("=") != -1) { // get the property name from the line var propName = line.substr(0, line.indexOf('=')); project.log("line prop: " + propName); var loadedPropName = "__MY_PREFIX__" + propName; if (project.getProperty(loadedPropName) != null) { project.log("prop has original: " + project.getProperty(loadedPropName)); // skip this line, the property is defined skipLine = true; } } if (skipLine) { project.log("skipping line: " + line); self.setToken(null); } else { // else leave the line in as it was project.log("adding default line: " + line); self.setToken(line); } ]]> </scriptfilter> </filterchain> </concat> </target> 
0
source

All Articles