问题
I have one deploy.path1=A,B,C,D,E
and deploy.path2=1,2,3,4
and I want to use A and 1 in one target Deploy like below eg at one time means both should be available at same time like A,1 then next time B,2 and so on...
<target name"deploy">
<echo message="${A}"/>
<echo message="${1}"/>
</target>
How can i do this?
Currently I'm using
<foreach list="${deploy.loc2}" param="deploy_javapass" target="copy_patch_javapass" delimiter="," inheritall="true" inheritrefs="true"
parallel="false" trim="true"/>
task but in this way we can pass only one parameter at one time.
回答1:
I rarely use the ant-contrib tasks. If you need complex logic, I'd suggest embedding a proper scripting language, for example the groovy task.
The following example invokes the "deploy" task in the manner you've described:
<project name="demo" default="run">
<property name="deploy.path1" value="A,B,C,D,E"/>
<property name="deploy.path2" value="1,2,3,4,5"/>
<target name="run">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy">
<classpath>
<pathelement location="/path/to/groovy/jar/groovy-all-1.8.5.jar"/>
</classpath>
</taskdef>
<groovy>
def values1 = properties."deploy.path1".split(",")
def values2 = properties."deploy.path2".split(",")
values1.eachWithIndex { value1, i ->
properties.val1 = value1
properties.val2 = values2[i]
ant.project.executeTarget('deploy')
}
</groovy>
</target>
<target name="deploy">
<echo message="path1 = ${val1}"/>
<echo message="path2 = ${val2}"/>
</target>
</project>
Enhanced example using ivy to managed 3rd party jars
For new ANT installations, I added an extra target to the build file:
$ ant install-ivy
This will setup ivy by downloading the ivy task jar.
Using ivy for one ANT task is over-kill. However it more than pays off when you use it to manage your various classpaths.
Maven central has a vast range of open source libraries available for download:
http://search.maven.org/
build.xml
<project name="demo" default="run" xmlns:ivy="antlib:org.apache.ivy.ant">
<property name="deploy.path1" value="A,B,C,D,E"/>
<property name="deploy.path2" value="1,2,3,4,5"/>
<target name="install-ivy">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.2.0/ivy-2.2.0.jar"/>
</target>
<target name="init">
<ivy:resolve/>
<ivy:cachepath pathid="build.path" conf="build"/>
</target>
<target name="run" depends="init">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<groovy>
def values1 = properties."deploy.path1".split(",")
def values2 = properties."deploy.path2".split(",")
values1.eachWithIndex { value1, i ->
properties.val1 = value1
properties.val2 = values2[i]
ant.project.executeTarget('deploy')
}
</groovy>
</target>
<target name="deploy">
<echo message="path1 = ${val1}"/>
<echo message="path2 = ${val2}"/>
</target>
</project>
ivy.xml:
ivy file listing the build's dependencies:
<ivy-module version="2.0">
<info organisation="myorg" module="mymodule" />
<configurations>
<conf name="build" description="Build dependencies"/>
</configurations>
<dependencies>
<dependency org="org.codehaus.groovy" name="groovy-all" rev="1.8.5" conf="build->default"/>
</dependencies>
</ivy-module>
来源:https://stackoverflow.com/questions/9131091/how-can-i-pass-two-variable-in-one-ant-target-by-spliting-commas