Map a list of strings to a collection of file reso

2019-03-06 02:57发布

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. ”readable”
  2. robust and
  3. can be used to determine if targets are out of date (preferably using tasks from Ant or Ant Contrib).

Sample xml:

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

Desired resultset:

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

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
1条回答
SAY GOODBYE
2楼-- · 2019-03-06 03:38

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

Example

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

Run as follows

$ 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

sample.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>
查看更多
登录 后发表回答