Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert files to UNIX format using Maven

I have an application that is developed in a Windows environment. The application itself gets deployed to a Linux environment. Each time I deploy this application I have to convert executable files to UNIX format using dos2unix. I originally thought this was caused by the Windows CP1252 encoding, so I updated Maven to encode the files to UTF-8. This didn't solve my issue and I quickly found out that this has to do with carriage returns and line feeds by searching this site. Is there a way to have Maven convert all of the files to UNIX format during the build process? I am using Maven 2.2.1 and Java 5.

like image 538
Dan Polites Avatar asked Jan 29 '10 13:01

Dan Polites


People also ask

How to convert dos to Unix in vi?

vi. To input the ^M character, press Ctrl-v , and then press Enter or return . In vim, use :set ff=unix to convert to Unix; use :set ff=dos to convert to Windows.

What is fileset in Maven?

Defines the rules for matching and working with files in a given base directory.

How do I use dos2unix in Linux?

Option 1: Converting DOS to UNIX with dos2unix Command The simplest way to convert line breaks in a text file is to use the dos2unix tool. The command converts the file without saving it in the original format. If you want to save the original file, add the -b attribute before the file name.


2 Answers

The assembly plugin has a lineEnding option which can be used to control the line-ending of the files for a given fileSet. This parameter is precisely there to do what you want. Ultimately, you could build zip archives with with CRLF lines and tar.gz archives with LF lines.

E.g.

...
<fileSet>
    <directory>${basedir}/src/main/build/QA</directory>
    <outputDirectory>/bin</outputDirectory>
    <includes>
        <include>start.sh</include>
    </includes>
    <lineEnding>unix</lineEnding>
</fileSet>
...

Possible values at this time include:

  • "keep" - Preserve all line endings
  • "unix" - Use Unix-style line endings (i.e. "\n")
  • "lf" - Use a single line-feed line endings (i.e. "\n")
  • "dos" - Use DOS-/Windows-style line endings (i.e. "\r\n")
  • "windows" - Use DOS-/Windows-style line endings (i.e. "\r\n")
  • "crlf" - Use carriage-return, line-feed line endings (i.e. "\r\n")
like image 72
Pascal Thivent Avatar answered Oct 19 '22 07:10

Pascal Thivent


You can use the Maven antrun plugin to call the fixcrlf ant task:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ant-test</groupId>
    <artifactId>ant-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>ant-test</id>
                        <phase>package</phase>
                        <configuration>
                            <tasks>
                                <fixcrlf ... />
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
like image 17
Kevin Avatar answered Oct 19 '22 06:10

Kevin