在Maven中重命名资源

我试图find一种方法将资源文件复制到Maven构build目标目录中的新名称。 在search时,几乎所有我find的东西都提示涉及/src/main/resources中多个子目录的解决方法,并通过configuration文件在其中进行select。 但是,在我的情况下,这并不能解决问题,即我想要的文件有一个“神奇”的名字。

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

我开始认为ant是唯一能自动做到这一点的方法。 有没有更简单的方法?

我看到2个选项来解决你的问题:

  • 使用Maven-Ant-Plugin ,并定义一个Ant重命名任务,它只会在构build目录的打包阶段重命名文件。
  • 使用这个专用的Maven插件(我没有testing过): http : //code.google.com/p/maven-file-rename-plugin/

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

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

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> 

你可以通过使用Maven Assembly插件和文件组件描述符来避开Ant的头顶。

我有同样的问题使用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>