代码之家  ›  专栏  ›  技术社区  ›  Dan Polites

使用Maven将文件转换为UNIX格式

  •  15
  • Dan Polites  · 技术社区  · 16 年前

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

    2 回复  |  直到 16 年前
        1
  •  17
  •   martin clayton egrunin    15 年前

    你可以使用Maven antrun 调用 fixcrlf 蚂蚁任务:

    <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>
    
        2
  •  45
  •   jk.    12 年前

    程序集插件有一个 lineEnding 选项,可用于控制给定文件的行结束 fileSet . 这个参数就是用来做你想做的事情的。最终,您可以使用CRLF行构建zip归档,使用LF行构建tar.gz归档。

    例如。

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

    此时可能的值包括:

    • “保留”-保留所有行尾
    • “unix”-使用unix样式的行尾(即“\n”)
    • “lf”-使用单行进给线末端(即“\n”)
    • “dos”-使用dos-/Windows样式的行尾(即“\r\n”)
    • “windows”-使用DOS-/windows样式的行尾(即“\r\n”)
    • “crlf”-使用回车符、换行符和换行符结尾(即“\r\n”)