问题
I was wondering if you could help. I am new to ant scripting.
I want to be able to compare two lists. File1.txt will contain a list of a lot of parameters and file2.txt will only contain a section of those parameters.
File1.txt
dbipAddress=192.168.175.130
QAGENT_QCF=AGENT_QCF
QADJUST_INVENTORY_Q=ADJUST_INVENTORY_Q
QCREATE_ORDER_Q=CREATE_ORDER_Q
QLOAD_INVENTORY_Q=LOAD_INVENTORY_Q
File2.txt
AGENT_QCF
ADJUST_INVENTORY_Q
CREATE_ORDER_Q
I want to know that all the Qs in file1.txt are contained in file2.txt, after the '=', if they aren't then the ant script will stop and an Echo message will be displayed if they are then the script will move to the next .
So in the example above the script will stop as it does not contain the following Q; QLOAD_INVENTORY_Q=LOAD_INVENTORY_Q.
回答1:
ANT is not a programming language, so I think you need to embed one.
The following example uses Groovy:
<project name="demo" default="check">
<target name="bootstrap">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/groovy.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.3/groovy-all-2.1.3.jar"/>
</target>
<target name="check">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<groovy>
def props = new Properties()
new File("File1.txt").withInputStream { stream -> props.load(stream) }
def requiredValues = new File("File2.txt").text.split()
requiredValues.each { check ->
if (! props.find{it.value == check}) {
ant.fail "Cannot find ${check} in File1.txt"
}
}
</groovy>
</target>
</project>
来源:https://stackoverflow.com/questions/16741906/using-ant-check-or-compare-all-items-listed-in-a-file-appear-in-another-file-or