Ant任务比较两个属性文件(Ant task to compare two properties files)

编程入门 行业动态 更新时间:2024-10-23 18:34:36
Ant任务比较两个属性文件(Ant task to compare two properties files)

有没有人知道可以比较两个(Java)属性文件的Ant任务? 我找不到任何东西,但在我下台实施之前我想确定一下。

输入 :两个属性文件 输出 :一个文件中但不在另一个文件中的属性键列表。

需要明确的是:它应该执行属性 - 文件语法感知比较,比较键的存在,但忽略值。

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.

Input: two properties files Output: list of property keys that are in one file but not in the other.

Just to be clear: it should perform properties-file-syntax-aware comparison, comparing the existence of keys, but ignoring the values.

最满意答案

您可以尝试将groovy ant任务与java-diff-utils库结合使用

├── build.xml ├── file1.properties └── file2.properties

运行构建会产生以下输出:

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]]

build.xml文件

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

笔记:

使用Apache ivy下载依赖项

file1.properties

one=1 two=2 three=3 four=4 five=5

file2.properties

one=1 three=3 threeandhalf=3.5 four=4 five=55555

修改过的例子

返回第二个文件中缺少的第一个文件中的属性:

diff: [groovy] Missing keys: [two]

build.xml文件

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

You could try to combine a groovy ant task with the java-diff-utils library

Example

├── 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]]

build.xml

<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:

Uses Apache ivy to download dependencies

file1.properties

one=1 two=2 three=3 four=4 five=5

file2.properties

one=1 three=3 threeandhalf=3.5 four=4 five=55555

Revised Example

Returns the properties in the first file missing in the second:

diff: [groovy] Missing keys: [two]

build.xml

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

更多推荐

属性,文件,keys,Ant,电脑培训,计算机培训,IT培训"/> <meta name="description&qu

本文发布于:2023-08-05 04:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428544.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   两个   文件   Ant   files

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!