Using ANT to fix end of line problem in scripts or text files

One of the ant tasks which has been overlooked is the one related to files. ANT can do some really useful things for us. When development environment is windows and deployment environment is *nix we always face the problem of ^M characters. The text files edited and saved in windows will have special characters (*nix) when moved to *nix

ANT can be used to fix this problem before moving into deployment environment. ANT task FixCRLF can be used for this.

Important parameters of this task
srcDir or file : Where to find the files to be fixed up.

destDir : Where to place the corrected files. Defaults to srcDir (replacing the original file).

eol : Specifies how end-of-line (EOL) characters are to be handled. The EOL characters are CR, LF and the pair CRLF. Valid values for this property are:

* asis: leave EOL characters alone
* cr: convert all EOLs to a single CR
* lf: convert all EOLs to a single LF
* crlf: convert all EOLs to the pair CRLF
* mac: convert all EOLs to a single CR
* unix: convert all EOLs to a single LF
* dos: convert all EOLs to the pair CRLF

Default is based on the platform on which you are running this task. For Unix platforms (including Mac OS X), the default is “lf”. For DOS-based systems (including Windows), the default is “crlf”. For Mac environments other than OS X, the default is “cr”.
This is the preferred method for specifying EOL. The “cr” attribute (see below) is now deprecated. N.B.: One special case is recognized. The three characters CR-CR-LF are regarded as a single EOL. Unless this property is specified as “asis”, this sequence will be converted into the specified EOL type.
This task forms an implicit FileSet and supports all attributes of (dir becomes srcdir) as well as the nested , and elements.
The output file is only written if it is a new file, or if it differs from the existing file. This prevents spurious rebuilds based on unchanged files which have been regenerated by this task. This task can also be used to fix the tab related issues.

Examples
Replaces EOLs with LF characters and removes eof characters from the shell scripts. Tabs and spaces are left as is.

<fixcrlf srcdir="${src}" includes="**/*.sh" eol="lf" eof="remove" />

Replaces all eol by windows specific crlf

<fixcrlf srcdir="${src}" includes="**/*.bat" eol="crlf" />

You can get more information about other ANT taksks from their manual.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *