18

我有一个在 Windows 环境中开发的应用程序。应用程序本身被部署到 Linux 环境中。每次部署此应用程序时,我都必须使用 dos2unix 将可执行文件转换为 UNIX 格式。我最初认为这是由 Windows CP1252 编码引起的,所以我更新了 Maven 以将文件编码为 UTF-8。这并没有解决我的问题,我通过搜索这个站点很快发现这与回车和换行有关。有没有办法让 Maven 在构建过程中将所有文件转换为 UNIX 格式?我正在使用 Maven 2.2.1 和 Java 5。

4

2 回答 2

45

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")
于 2010-01-29T14:52:44.520 回答
17

可以使用 Maven antrun插件调用fixcrlf ant 任务:

<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>
于 2010-01-29T14:04:42.747 回答