2010-10-22 48 views
37

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

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

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

+0

http://maven.apache.org/plugins/maven-resources-plugin/这可能对你有所帮助, – 2010-10-22 15:57:15

+5

资源插件不允许重命名。这是我的问题。 – wmorrell 2010-10-22 16:05:54

+0

你也可以看看http://maven.apache.org/plugins/maven-assembly-plugin/。但我不确定你可以重命名文件:我从来没有这样使用过这个插件。 – 2010-10-22 16:06:14

回答

15

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

+2

我使用antrun插件,在准备包阶段执行一次执行以创建重命名的'.DS_Store',另一次在验证阶段删除它(以防万一...)。看起来像在工作,谢谢! – wmorrell 2010-10-22 17:16:20

12

您可以通过使用Maven Assembly pluginfile assembly descriptor避免蚂蚁的头顶。组装插件

+0

不受文件集支持。所以你不能应用一个模式:( – BeepDog 2011-09-01 16:34:36

+0

文件重命名可以在assembly插件中用dependencySets完成。 – mojoken 2014-06-04 22:50:50

+7

你能提供一个例子吗?那里有很多文档! – 2014-06-13 15:58:57

14

实施例使用,以复制和/或重命名的文件:

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> 
+2

不幸的是,似乎不支持通配符*。我希望我能做到像 src/main/resources/*。properties $ {source} -suffix CuongHuyTo 2015-02-02 09:24:25

21

使用antrun-行家-插件使它变得简单,但是如果你正在寻找一种在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/

6

伸出我使用复制和重命名 - Maven的插件解决我的问题有同样的问题

<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> 
+0

@coderplus'之前的答案有什么区别? – 2016-12-23 22:41:41

+0

我使用复制目标来解决我的使用案例@coderplus做了重命名 – 2017-01-09 12:16:57