0

2 つの (Java) プロパティ ファイルを比較する Ant タスクを知っている人はいますか? 何も見つかりませんでしたが、出かけて実装する前に確認したいと思います。

  • 入力: 2 つのプロパティ ファイル
  • 出力: 一方のファイルにあり、他方のファイルにないプロパティ キーのリスト。

明確にするために、プロパティファイルの構文を認識した比較を実行し、キーの存在を比較する必要がありますが、値は無視します。

4

3 に答える 3

2

最適なオプションは、PropDiffユーティリティを使用することです。Java プロパティ ファイルを比較、結合、交差するオプションがあります。オープンソースなので、必要に応じて変更できます。

PropDiffのドキュメントは次のとおりです。

于 2015-11-04T12:23:31.253 に答える
1

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

改訂例

2 番目のファイルにない最初のファイルのプロパティを返します。

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>
于 2013-01-17T20:12:04.180 に答える
1

ant-contrib ライブラリのみを必要とする別のアプローチを次に示します。

<target name="checkPropertyFiles">
    <antcall target="ensureTwoFilesHaveSameProperties">
        <param name="file1" value="file1.properties"/>
        <param name="file2" value="file2.properties"/>
    </antcall>
</target>

<target name="ensureTwoFilesHaveSameProperties">
    <loadproperties srcFile="${file1}" prefix="prefixfile1"/>
    <loadproperties srcFile="${file2}" prefix="prefixfile2"/>

    <propertyselector property="file1.list" delimiter="," match="prefixfile1\.(.+)" select="\1"/>
    <propertyselector property="file2.list" delimiter="," match="prefixfile2\.(.+)" select="\1"/>

    <for list="${file1.list}" param="file1.property">
        <sequential>
            <if>
                <not>
                    <matches pattern=",@{file1.property}," string=",${file2.list}," />
                </not>
                <then>
                    <property name="error.encountered" value="true"/>
                    <echo message="Property @{file1.property} is present in ${file1} but not in ${file2}"/>
                </then>
            </if>
        </sequential>
    </for>
    <for list="${file2.list}" param="file2.property">
        <sequential>
            <if>
                <not>
                    <matches pattern=",@{file2.property}," string=",${file1.list}," />
                </not>
                <then>
                    <property name="error.encountered" value="true"/>
                    <echo message="Property @{file2.property} is present in ${file2} but not in ${file1}"/>
                </then>
            </if>
        </sequential>
    </for>

    <fail message="Property files do not have the same set of keys.">
        <condition>
            <isset property="error.encountered"/>
        </condition>
    </fail>

    <echo message="OK: ${file1} has same properties as ${file2}"/>
</target>
于 2013-05-16T12:24:42.147 に答える