Does anyone know of an Ant task that would compare two (Java) properties files? I could not find any but I would like to be sure before I go off and implement it.
Just to be clear: it should perform properties-file-syntax-aware comparison, comparing the existence of keys, but ignoring the values.
You could try to combine a groovy ant task with the java-diff-utils library
├── build.xml
├── file1.properties
└── file2.properties
Running the build produces the following output:
diff:
[groovy] [DeleteDelta, position: 1, lines: [two=2]]
[groovy] [InsertDelta, position: 3, lines: [threeandhalf=3.5]]
[groovy] [ChangeDelta, position: 4, lines: [five=5] to [five=55555]]
<project name="demo" default="diff" xmlns:ivy="antlib:org.apache.ivy.ant">
<target name="resolve">
<ivy:cachepath pathid="build.path">
<dependency org="org.codehaus.groovy" name="groovy-all" rev="2.1.0-rc-1" conf="default"/>
<dependency org="com.googlecode.java-diff-utils" name="diffutils" rev="1.2.1" conf="default"/>
</ivy:cachepath>
</target>
<target name="diff" depends="resolve">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<groovy>
import difflib.*
def original = new File("file1.properties").readLines()
def revised = new File("file2.properties").readLines()
Patch patch = DiffUtils.diff(original, revised)
patch.getDeltas().each {
println it
}
</groovy>
</target>
</project>
Notes:
one=1
two=2
three=3
four=4
five=5
one=1
three=3
threeandhalf=3.5
four=4
five=55555
Returns the properties in the first file missing in the second:
diff:
[groovy] Missing keys: [two]
<project name="demo" default="diff" xmlns:ivy="antlib:org.apache.ivy.ant">
<target name="resolve">
<ivy:cachepath pathid="build.path">
<dependency org="org.codehaus.groovy" name="groovy-all" rev="2.1.0-rc-1" conf="default"/>
</ivy:cachepath>
</target>
<target name="diff" depends="resolve">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<groovy>
def source = new Properties()
def target = new Properties()
new File("file1.properties").withInputStream { source.load(it) }
new File("file2.properties").withInputStream { target.load(it) }
def diff = source.findResults { k,v ->
k in target ? null : k
}
println "Missing keys: ${diff}"
</groovy>
</target>
</project>