将字符串列表映射到依赖集的文件资源集合 [英] Map a list of strings to a collection of file resources for a dependset

查看:26
本文介绍了将字符串列表映射到依赖集的文件资源集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我从 XML 文件中读取名称,并希望将它们映射到构建任务的源和目标路径.我不是一个有经验的 Ant 用户,我正在寻找一种方法是

Background: I read names from an XML file and want to map them to source and target paths for a build task. I am not an experienced Ant user and I'm looking for a way to that is

  1. 可读"
  2. 健壮和
  3. 可用于确定目标是否已过期(最好使用来自 Ant 或 Ant Contrib 的任务).

示例 xml:

<list><value>The first name<value><value>The second name</value></list>

期望的结果集:

${dir}/The first name.${ext}
${dir}/The second name.${ext}

我可以使用 pathconvertmappedresources 构建每个文件的路径,但我无法将任一结果映射回我可以的文件资源集合在 dependset 中使用.这个问题有优雅的解决方案吗?

I can build the path to each file using pathconvert or mappedresources but I haven't been able to map either result back to a collection of file resources that I can use in a dependset. Is there an elegant solution to this problem?

推荐答案

ANT 不是一种编程语言.易于嵌入 groovy.

ANT is not a programming language. Easy to embed groovy.

├── build.xml
├── sample.xml
├── src
│   ├── file1.txt
│   ├── file2.txt
│   └── file3.txt
└── target
    ├── file1.txt
    └── file2.txt

如下运行

$ ant
Buildfile: /.../build.xml

install-groovy:

build:
     [copy] Copying 2 files to /.../target
     [copy] Copying /.../src/file1.txt to /.../target/file1.txt
     [copy] Copying /.../src/file2.txt to /.../target/file2.txt

BUILD SUCCESSFUL

示例.xml

<list>
<value>file1</value>
<value>file2</value>
</list>

build.xml

<project name="demo" default="build">

    <!--
    ================
    Build properties
    ================
    -->
    <property name="src.dir"   location="src"/>
    <property name="src.ext"   value="txt"/>
    <property name="build.dir" location="target"/>

    <available classname="org.codehaus.groovy.ant.Groovy" property="groovy.installed"/> 

    <!--
    ===========
    Build targets
    ===========
    -->    
    <target name="build" depends="install-groovy" description="Build the project">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
        <groovy>
        def xmllist = new XmlSlurper().parse(new File("sample.xml"))

        ant.copy(todir:properties["build.dir"], verbose:true, overwrite:true) {
           fileset(dir:properties["src.dir"]) {
              xmllist.value.each {
                include(name:"${it}.${properties["src.ext"]}") 
              }
           }
        }
        </groovy>
    </target>

    <target name="clean" description="Cleanup project workspace">
       <delete dir="${build.dir}"/>
    </target>

    <target name="install-groovy" description="Install groovy" unless="groovy.installed">
        <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.3.6/groovy-all-2.3.6.jar"/>
        <fail message="Groovy has been installed. Run the build again"/>
    </target>

</project>

这篇关于将字符串列表映射到依赖集的文件资源集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆