How to keep file order in Ant concat?

How to keep file order in Ant concat?

A simple concat with a set of files and an includefile produces a rather "random" order, since the order is not guaranteed:

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

I need concatenation in a certain order that the files are listed in the include file.

So far, I have found a resourcelist that should keep order, but I seem to be unable to create 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> 

In addition, the resourcelist cannot process strings such as

 LibraryX/A/Stuff/Morestuff/* 

Instead of a line, it simply creates "... / Seaworld / * does not exist." -error

Includes a list of relative paths in its file:

 LibraryX/A/Stuff/FileA.txt LibraryX/A/Stuff/FileB.txt LibraryX/A/Stuff/FileC.txt LibraryX/A/Stuff/FileY.txt 
+6
source share
5 answers

I managed to get a file that works quite 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 this helps!

+13
source

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 sorting documentation here.

+6
source

[In Ant 1.8.2+] You can also transfer a set of files by sorting and sorting the file name, as shown 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> 

A couple of things to look out for:

  • Directories are sorted before files.
  • Capitals go to the lower case.

UPDATE: Another alternative if you need to manually specify the 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> 

Terrible, but, erm, is it ant?

+2
source

try this put in alphabetical order

 <project name="concatPath" default="full"> <target name="full"> <fileset id="fs" dir="./files" /> <pathconvert refid="fs" property="concatList" pathsep=";" targetos="unix"/> <echo>${concatList}</echo> </target> </project> 

this can be used with a hierarchical directory structure and the order will be set by David.

0
source

Remember that XML is, by definition, independent of order.

To combine files in sorted order, use <replace> instead.

Create an order file that defines the order. Then in the build file:

  • Copy the order file to the destination file using <copy>
  • Merge your files into a temporary file using <concat>
  • Upload files to properties using <loadfile>
  • Paste text from these files into the destination file using <replace>

Example order_file.txt file:

  FILE_A_HERE CONCAT_FILES_HERE 

Example ant build file build.xml:

  <copy file="order_file.txt" tofile="destination.txt" overwrite="yes"> <concat destfile="tempfile.txt"> <fileset dir="includes/"> <include name="*.txt"> <exclude name="fileA.txt"> </fileset> </concat> <loadfile property="fileA" srcFile="includes/fileA.txt" /> <loadfile property="concatFile" srcFile="tempfile.txt" /> <replace file="destination.txt" token="FILE_A_HERE" value="fileA" /> <replace file="destination.txt" token="CONCAT_FILES_HERE" value="concatFile" /> 
0
source

Source: https://habr.com/ru/post/887115/


All Articles