Apache Ant – Concatenate text files

This is an example showing how to concatenates files using Ant script.

1. Assume you have the following files in your Ant project folder.

ant-for-example
  | - build.xml
  | - Text-1.txt
  | - Text-2.txt
  | - Text-3.txt

 

2. Each of the .txt files has similar content as follow. Pay attention to the newline characters at the end.

This is Test-1.txt


<EOF>

 

3. Here is the build.xml.

<project name="ant-concat-example" default="run" basedir=".">
  <target name="run">
    <concat destfile="${basedir}/Full.txt">
      <sort>
        <fileset dir="${basedir}">
          <include name="Test-*.txt"/>
        </fileset>
      </sort>
    </concat>
  </target>
</project>

 

4. Run the Ant script you will get the Full.txt as below.

This is Test-1.txt

This is Test-2.txt

This is Test-3.txt


<EOF>

 

5. See the newline characters in the result text file above. To insert a newline for every included file, you can add the fixlastline attribute.

<project name="ant-concat-example" default="run" basedir=".">
  <target name="run">
    <concat destfile="${basedir}/Full.txt" fixlastline="true">
      <sort>
        <fileset dir="${basedir}">
          <include name="Test-*.txt"/>
        </fileset>
      </sort>
    </concat>
  </target>
</project>

 

Done =)

Reference:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s