Convert files to UNIX format using Maven
Asked Answered
D

2

20

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.

Drysalter answered 29/1, 2010 at 13:45 Comment(0)
A
17

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>
Addicted answered 29/1, 2010 at 14:4 Comment(6)
I tried <fixcrlf srcdir="${basedir}" eol="unix"/>, which seems to be working. I am also using the maven assembly plugin and it is now having issues reading the jar files due to this conversion. Any ideas on how I can get this to build on a Windows machine?Drysalter
You probably want to add an includes or excludes attribute so you're only changing *.txt or whatever resources you're trying to modifyAddicted
Thanks, the includes/excludes worked. It modifies the files in my source directories though, which will be OK if need be, but is there any way to have the Maven assembly plug-in run this ant task as it is building so that I won't have to revert or check in my files?Drysalter
What resources are you trying to modify? See the documentation for the antrun plugin, you can use maven expressions to reference build paths and other related paths.Addicted
Example on actual usage: <fixcrlf srcdir="${project.build.outputDirectory}" eol="unix" includes="**/*.sh" /> - I'd also bind it to process-resources phase, not to package phase.Maggiemaggio
I find it interesting that this says maven-resources-plugin should be able to replace antrun plugin for fixcrlf task, but haven't seen any indication it would be able toMaggiemaggio
H
48

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")
Hooray answered 29/1, 2010 at 14:52 Comment(0)
A
17

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>
Addicted answered 29/1, 2010 at 14:4 Comment(6)
I tried <fixcrlf srcdir="${basedir}" eol="unix"/>, which seems to be working. I am also using the maven assembly plugin and it is now having issues reading the jar files due to this conversion. Any ideas on how I can get this to build on a Windows machine?Drysalter
You probably want to add an includes or excludes attribute so you're only changing *.txt or whatever resources you're trying to modifyAddicted
Thanks, the includes/excludes worked. It modifies the files in my source directories though, which will be OK if need be, but is there any way to have the Maven assembly plug-in run this ant task as it is building so that I won't have to revert or check in my files?Drysalter
What resources are you trying to modify? See the documentation for the antrun plugin, you can use maven expressions to reference build paths and other related paths.Addicted
Example on actual usage: <fixcrlf srcdir="${project.build.outputDirectory}" eol="unix" includes="**/*.sh" /> - I'd also bind it to process-resources phase, not to package phase.Maggiemaggio
I find it interesting that this says maven-resources-plugin should be able to replace antrun plugin for fixcrlf task, but haven't seen any indication it would be able toMaggiemaggio

© 2022 - 2024 — McMap. All rights reserved.