java 使用 Maven 将文件转换为 UNIX 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2162275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert files to UNIX format using Maven
提问by Dan Polites
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.
我有一个在 Windows 环境中开发的应用程序。应用程序本身被部署到 Linux 环境中。每次部署此应用程序时,我都必须使用 dos2unix 将可执行文件转换为 UNIX 格式。本来以为是Windows CP1252编码造成的,于是更新了Maven,将文件编码为UTF-8。这并没有解决我的问题,我很快发现这与通过搜索该站点的回车和换行有关。有没有办法让 Maven 在构建过程中将所有文件转换为 UNIX 格式?我正在使用 Maven 2.2.1 和 Java 5。
采纳答案by Kevin
You can use the Maven antrunplugin to call the fixcrlfant task:
可以使用Maven antrun插件调用fixcrlfant任务:
<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>
回答by Pascal Thivent
The assembly plugin has a lineEndingoption 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.
程序集插件有一个lineEnding选项,可用于控制给定fileSet. 这个参数正是用来做你想做的。最终,您可以使用 CRLF 行构建 zip 档案,并使用 LF 行构建 tar.gz 档案。
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")
- "keep" - 保留所有行结尾
- "unix" - 使用 Unix 风格的行尾(即 "\n")
- "lf" - 使用单个换行符行结尾(即 "\n")
- "dos" - 使用 DOS-/Windows 风格的行尾(即 "\r\n")
- "windows" - 使用 DOS-/Windows 风格的行尾(即 "\r\n")
- "crlf" - 使用回车、换行的换行符(即 "\r\n")

