在 pom.xml 中指定换行符

specifying newlines in pom.xml

我需要用项目的依赖包生成一个文件。 这可以通过 dependeny 插件来完成。 请参阅下面的 pom 片段:

    <plugin>                   
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>2.10</version>   
        <execution>            
          <id>list-dependencies</id>
          <phase>package</phase>
          <goals>              
            <goal>build-classpath</goal>
          </goals>             
          <configuration>
            <prefix></prefix>
            <outputFile>target/modules-list.txt</outputFile>
            <pathSeparator>\n</pathSeparator>                                                                                                                       
          </configuration>  
        </execution>           
      </executions>            
    </plugin>                  

但是,因为我需要每个 jar 单独一行,所以我尝试将 pathSeparator 指定为换行符。但是,这不起作用。我没有找到在 pom 中指定换行符的有用方法。

关于如何实现这一点有什么想法吗?

Maven 正在修剪您的 <pathSeparator> 值,这会导致空值,因为行分隔符被视为空格。然后,由于没有值,Maven 回退到使用默认值。我没有看到任何简单的方法来解决此问题。

还有一些其他选项。您可以考虑使用 dependency:list 目标:

pom.xml

<plugin>                   
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.10</version>
  <executions>
    <execution>
      <id>list-dependencies</id>
      <phase>package</phase>
      <goals>              
        <goal>list</goal>
      </goals>             
      <configuration>
        <outputFile>target/modules-list.txt</outputFile>
      </configuration>
    </execution>
  </executions>
</plugin>                  

target/modules-list.txt

The following files have been resolved:
   org.slf4j:slf4j-log4j12:jar:1.7.10:compile
   org.hamcrest:hamcrest-core:jar:1.3:test
   junit:junit:jar:4.11:test
   log4j:log4j:jar:1.2.17:compile
   org.slf4j:slf4j-api:jar:1.7.10:compile
   javax.ws.rs:jsr311-api:jar:1.1.1:compile
   com.sun.jersey:jersey-server:jar:1.19:compile
   com.sun.jersey:jersey-core:jar:1.19:compile

这是 Maven 工件 ID 而不是 jar 文件名。可以说,这实际上更有价值,因为输出不会在不同机器上改变 运行 构建。 (jar 文件将使用绝对路径,这在不同的机器上可能不同。)

如果它真的必须是 jar 文件名而不是 Maven 工件 ID,那么您可以通过将 non-whitespace 字符注入 <pathSeparator> 值来拼凑一些东西,这样即使在 Maven 修剪之后值,还有一些东西可以覆盖默认路径分隔符。

pom.xml

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.10</version>
  <executions>
    <execution>
      <id>list-dependencies</id>
      <phase>package</phase>
      <goals>
        <goal>build-classpath</goal>
      </goals>
      <configuration>
        <outputFile>target/modules-list.txt</outputFile>
        <pathSeparator>_${line.separator}</pathSeparator>
      </configuration>
    </execution>
  </executions>
</plugin>

target/modules-list.txt

/Users/naurc001/.m2/repository/com/sun/jersey/jersey-core/1.19/jersey-core-1.19.jar_
/Users/naurc001/.m2/repository/javax/ws/rs/jsr311-api/1.1.1/jsr311-api-1.1.1.jar_
/Users/naurc001/.m2/repository/com/sun/jersey/jersey-server/1.19/jersey-server-1.19.jar_
/Users/naurc001/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar_
/Users/naurc001/.m2/repository/org/slf4j/slf4j-api/1.7.10/slf4j-api-1.7.10.jar_
/Users/naurc001/.m2/repository/org/slf4j/slf4j-log4j12/1.7.10/slf4j-log4j12-1.7.10.jar_
/Users/naurc001/.m2/repository/junit/junit/4.11/junit-4.11.jar_
/Users/naurc001/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar

不幸的是,最后留下难看的下划线。如果你想清理它们,那么你需要另一个构建步骤来清理它们。一种方法是在 generate-resources 阶段生成此文件,然后在 process-resources 阶段使用 maven-resources-plugin 进行标记替换。

最后,如果您只是出于文档目的需要此信息,请考虑使用 maven-project-info-reports-plugin 提供的站点文档。

在 XML 中,您可以使用以下转义序列指定换行符 - &#x0A;

不幸的是,正如您已经提到的,<pathSeparator> 值被删除了所有前导和尾随空格,实际上导致空字符串值导致插件使用其默认值。

但是,您可以在同一阶段使用 Ant 任务来替换路径分隔符。例如。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>list-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>build-classpath</goal>
                    </goals>
                    <configuration>
                        <prefix></prefix>
                        <outputFile>target/modules-list.txt</outputFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- Replace path separator -->
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <configuration>
                        <target>
                            <replace file ="target/modules-list.txt" token="${path.separator}" value="${line.separator}" />
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

请注意,<pathSeparator> 不再在您的依赖项配置中定义。