我有一个deploy.path1=A,B,C,D,E
和deploy.path2=1,2,3,4
我希望在一个目标中使用A和1如下所示部署,例如,同时意味着两个应该同时可用,如A,1然后下次B,2等等......
<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"/>
任务,但通过这种方式,我们一次只能传递一个参数。
答案 0 :(得分:1)
我很少使用ant-contrib任务。如果您需要复杂的逻辑,我建议您嵌入适当的脚本语言,例如groovy task。
以下示例以您描述的方式调用“部署”任务:
<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>
对于新的ANT安装,我在构建文件中添加了一个额外的目标:
$ ant install-ivy
这将通过下载常春藤任务jar来设置常春藤。
将常春藤用于一个ANT任务是过度杀戮。但是,当您使用它来管理各种类路径时,它会得到回报。
Maven Central提供了大量可供下载的开源库:
<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-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>