【问题标题】:Renaming resources in Maven在 Maven 中重命名资源
【发布时间】:2011-04-29 06:07:27
【问题描述】:

我正在尝试找到一种将资源文件复制到 Maven 构建中目标目录中的新名称的方法。我在搜索时发现的几乎所有内容都建议了涉及/src/main/resources 中的多个子目录并通过配置文件在其中进行选择的解决方法。但是,就我而言,这并不能解决问题,即我想要的文件有一个“神奇”的名称。

基本上我想做的是将/src/main/resources/default.DS_Store 文件复制到${project.build.directory}/.DS_Store。由于.DS_Store 文件在 Mac OSX 中具有特殊含义,因此在源代码树和版本控制中不希望有一个具有该名称的文件。但是,我确实希望文件中的数据位于源代码树和版本控制中,并在构建期间将其重命名为“魔术”名称。

我开始认为 ant 是自动执行此操作的唯一方法。有没有更简单的方法?

【问题讨论】:

标签: maven-2


【解决方案1】:

使用 antrun-maven-plugin 很容易,但如果您正在寻找 eclipse m2e 支持的更专业的方式,那么您可以使用 copy-rename-maven-plugin

  <plugin>
    <groupId>com.coderplus.maven.plugins</groupId>
    <artifactId>copy-rename-maven-plugin</artifactId>
    <version>1.0.1</version>
    <executions>
      <execution>
        <id>rename-file</id>
        <phase>compile</phase>
        <goals>
          <goal>rename</goal>
        </goals>
        <configuration>
          <sourceFile>${project.build.outputDirectory}/default.DS_Store</sourceFile>
          <destinationFile>${project.build.outputDirectory}/.DS_Store</destinationFile>
        </configuration>
      </execution>
    </executions>
  </plugin>

如果您对该插件有任何反馈/问题,您可以通过https://github.com/coderplus/copy-rename-maven-plugin/联系我们

【讨论】:

    【解决方案2】:

    使用程序集插件复制和/或重命名文件的示例:

    pom 文件:

    <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/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>test</groupId>
      <artifactId>test</artifactId>
      <version>0.0.1-SNAPSHOT</version>
       <build>
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2.1</version>
            <configuration>
              <descriptors>
                <descriptor>src/main/descriptors/example.xml</descriptor>
              </descriptors>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    

    描述符文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <assembly>
           <id>example</id>
           <formats>
                  <format>dir</format>
           </formats>
           <files>
                  <file>
                         <source>src/main/resources/something.properties</source>
                         <outputDirectory>/</outputDirectory>
                         <destName>something.properties</destName>
                  </file>
                  <file>
                         <source>src/main/resources/something.properties</source>
                         <outputDirectory>/</outputDirectory>
                         <destName>something_en.properties</destName>
                  </file>
           </files>
    </assembly>
    

    【讨论】:

    【解决方案3】:

    我遇到了同样的问题,使用 copy-rename-maven-plugin 解决了我的问题

        <project>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>com.coderplus.maven.plugins</groupId>
            <artifactId>copy-rename-maven-plugin</artifactId>
            <version>1.0</version>
            <executions>
              <execution>
                <id>copy-file</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>copy</goal>
                </goals>
                <configuration>
                  <sourceFile>src/someDirectory/test.environment.properties</sourceFile>
                  <destinationFile>target/someDir/environment.properties</destinationFile>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    

    【讨论】:

    • 与@coderplus 之前的回答有什么不同?
    • 我使用复制目标来解决我的用例@coderplus 进行了重命名
    【解决方案4】:

    我看到 2 个选项可以解决您的问题:

    【讨论】:

    • 我使用了 antrun 插件,在准备包阶段执行一次以创建重命名的.DS_Store,另一次在验证阶段将其删除(以防万一……)。看起来它正在工作,谢谢!
    【解决方案5】:

    您可以通过使用Maven Assembly pluginfile assembly descriptor 来避免Ant 的开销。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 2015-03-04
    • 2019-07-01
    相关资源
    最近更新 更多