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
<target name="merge"> <property name="prefix" value="__MY_PREFIX__"/> <property prefix="${prefix}" file="${main.properties.file}"/> <copy file="${main.properties.file}" tofile="${output.properties.file}" overwrite="true"/> <concat destfile="${output.properties.file}" append="true"> <fileset file="${default.properties.file}"/> <filterchain> <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>
Rhubarb
source share