如何通过拆分逗号在一个 Ant 目标中传递两个变量 [英] How can i pass two variable in one Ant target by spliting commas
问题描述
我有一个 deploy.path1=A,B,C,D,E
和 deploy.path2=1,2,3,4
我想使用一个目标中的 A 和 1 如下部署,例如,一次意味着两者都应该同时可用,例如 A,1 然后下一次 B,2 等等...
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>
我该怎么做?
目前我正在使用
<foreach list="${deploy.loc2}" param="deploy_javapass" target="copy_patch_javapass" delimiter="," inheritall="true" inheritrefs="true"
parallel="false" trim="true"/>
task 但是这样我们一次只能传递一个参数.
task but in this way we can pass only one parameter at one time.
推荐答案
我很少使用 ant-contrib一>任务.如果您需要复杂的逻辑,我建议嵌入适当的脚本语言,例如 groovy 任务.
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>
<小时>
使用 ivy 管理 3rd 方 jar 的增强示例
对于新的 ANT 安装,我在构建文件中添加了一个额外的目标:
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
这将通过下载 ivy 任务 jar 来设置 ivy.
This will setup ivy by downloading the ivy task jar.
将 ivy 用于一项 ANT 任务是过度的.然而,当您使用它来管理您的各种类路径时,它会得到更多的回报.
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 中心有大量可供下载的开源库:
Maven central has a vast range of open source libraries available for download:
<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文件:
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>
这篇关于如何通过拆分逗号在一个 Ant 目标中传递两个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!