Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve file order in Ant concat?

Tags:

ant

How to preserve file order in Ant concat?

Simple concat with fileset & includesfile produces rather "random" order, as order is not guaranteed:

<concat destfile="C:/targetdir/concatenated.file">
    <fileset dir="C:/sourcedir/">
        <includesfile name="C:/targetdir/includes.file" />
    </fileset>
</concat>

What I need is concatenation in specific order that the files are listed in the includes file.

So far I've found resourcelist, which should preserve order, but I can't seem to be able to produce any concatenated file with it. :/

<concat destfile="C:/targetdir/concatenated.file">
    <resourcelist>
        <file file="C:/targetdir/includes.file"/>
        <filterchain>
            <striplinecomments>
                <comment value="#"/>
            </striplinecomments>
            <prefixlines prefix="C:/sourcedir/"/>
        </filterchain>
    </resourcelist>
</concat>

Plus, the resourcelist can't seem to handle rows like

LibraryX/A/Stuff/Morestuff/*

Instead the row just produces a ".../Morestuff/* does not exist." -error

Includes file has list of relative paths:

LibraryX/A/Stuff/FileA.txt
LibraryX/A/Stuff/FileB.txt
LibraryX/A/Stuff/FileC.txt
LibraryX/A/Stuff/FileY.txt
like image 618
crappish Avatar asked May 02 '11 11:05

crappish


3 Answers

I was able to get a filelist working pretty easily:

<concat destfile="C:/targetdir/concatenated.file">
    <filelist dir="C:/sourcedir/">
        <file name="i.txt" />
        <file name="n.txt" />

        <file name="o.txt" />
        <file name="r.txt" />
        <file name="d.txt" />
        <file name="e.txt" />
        <file name="r.txt" />
    </filelist>
</concat>

Hope that helps!

like image 67
Adam C Avatar answered Oct 09 '22 12:10

Adam C


If you are using Ant 1.7+, you can use the sort command

   <concat destfile="C:/targetdir/concatenated.file">
        <sort>
            <fileset dir="C:/sourcedir/">   
                <include name="C:/targetdir/*.file" />                      
            </fileset>            
        </sort>            
   </concat>

You can find the documentation of sort here

like image 26
Ara Yeressian Avatar answered Oct 09 '22 12:10

Ara Yeressian


[On Ant 1.8.2+] You can also pass the fileset via a sort, and sort on filename, like below:

<concat destfile="./${dir.publish}/${dir.js}/b.main-${build.number}.debug.js">
     <sort xmlns:rcmp="antlib:org.apache.tools.ant.types.resources.comparators">
          <fileset dir="./${dir.publish}/">
              <include name="**/${dir.js.main}/**/*.js"/>
              <exclude name="**/${dir.js.main}/**/*.min.js"/>
          </fileset>
          <rcmp:name />
     </sort>
  </concat>

Couple of things to watch out for:

  1. Directories are sorted before files
  2. Capitals come before lowercase

UPDATE: Another alternative if you need to manually specify order:

<!-- create a ordered list of all the build files so that CIAPI & CIAPI.widget are built first  
    (can't find a smarter way to do this, since ant filesets are unordered) -->
<fileset id="a" dir="."><include name="CIAPI/build.project.xml"/></fileset>
<fileset id="b" dir="."><include name="CIAPI.widget/build.project.xml"/></fileset>
<fileset id="c" dir=".">
    <include name="**/build.project.xml"/>
    <exclude name="CIAPI/build.project.xml" />
    <exclude name="CIAPI.widget/build.project.xml" />
</fileset>
<union id="all_build_files">
    <fileset refid="a"/>
    <fileset refid="b"/>
    <fileset refid="c"/>
</union>

Ugly, but, erm, this is ant?

like image 25
David Laing Avatar answered Oct 09 '22 11:10

David Laing