Search code examples
groovyioconventionsline-endings

Groovy: copy file, while converting line endings


I am using Groovy to customize Maven assembly plugin and make my life a bit easier. Basically I would like to mimicry Maven Assembly plugin functionallity and one is to copy files while converting line endings.

What is the simplest way to copy file from A to B while converting line-endings to Unix-style?


Solution

  • You could do this (assuming the file is not too large, as it loads it all into memory)

    void convertCRLF( File input, File output ) {
        output << input.text.replaceAll( '\r\n', '\n' )
    }
    
    convertCRLF( new File( '/tmp/test.txt' ), new File( '/tmp/test.fix.txt' ) )
    

    Or maybe you could try AntBuilder with the FixCRLF task

    An example of this would be:

    new AntBuilder().fixcrlf( srcDir:'/tmp/inputs', eol:'lf' )
    

    Which will convert all files in /tmp/inputs to use lf as the line-ending