在Maven中重命名资源

时间:2010-10-22 15:53:55

标签: maven-2

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

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

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

5 个答案:

答案 0 :(得分:30)

使用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/

联系

答案 1 :(得分:16)

我看到了两个解决问题的方法:

答案 2 :(得分:16)

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

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 :(得分:13)

您可以使用Maven Assembly pluginfile assembly descriptor来避免使用Ant。

答案 4 :(得分:12)

我使用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>
相关问题