62

I am trying to find a way to copy a resource file to a new name in the target directory in a Maven build. Pretty much everything I have found while searching suggests workarounds involving multiple sub-directories in /src/main/resources and selecting among them via profiles. However, in my case, this does not solve the problem, namely that the file I want has a "magic" name.

Basically what I want to do is have a /src/main/resources/default.DS_Store file get copied to ${project.build.directory}/.DS_Store. Since the .DS_Store file has special meaning in Mac OSX, it is not desirable to have a file with that name in the source tree, and in version control. However, I do want the data in the file to be in the source tree and version control, and have it renamed to the "magic" name during the build.

I'm starting to think that ant is the only way to do this automatically. Is there any easier way?

wmorrell
  • 4,988
  • 4
  • 27
  • 37
  • http://maven.apache.org/plugins/maven-resources-plugin/ This might help you, – jmj Oct 22 '10 at 15:57
  • 10
    The resources plugin does not allow renaming. That's my problem. – wmorrell Oct 22 '10 at 16:05
  • You can also look at http://maven.apache.org/plugins/maven-assembly-plugin/ . But I am not sure you can rename files: I never used this plugin this way. – Benoit Courtine Oct 22 '10 at 16:06
  • I found [this post](http://stackoverflow.com/questions/16242759/how-can-i-create-a-maven-build-profile-to-conditionally-copy-files) helpful - I didn't need to rename the resource just select the right one based on a property. – JonnyRaa Jun 16 '14 at 09:10

5 Answers5

43

Using the antrun-maven-plugin makes it easy, but in case you are looking for a more mavenish way which is supported within eclipse m2e, then you can use the 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>

And in case you have any feedback/issues with the plugin, you can reach out at https://github.com/coderplus/copy-rename-maven-plugin/

coderplus
  • 5,793
  • 5
  • 34
  • 54
22

Example usage of the assembly plugin to copy and/or rename a file:

pom file:

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

Descriptor file:

<?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>
rob2universe
  • 7,059
  • 39
  • 54
  • 3
    Unfortunately, it seems that does not support wildcard *. I wish I could do smt like src/main/resources/*.properties${source}-suffix – CuongHuyTo Feb 02 '15 at 09:24
  • Use , etc in the assembly descriptor if you need wildcards. See http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html – Ed Randall Jan 09 '21 at 10:07
19

I had the same problem using the copy-rename-maven-plugin solved my problem

    <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>
P-A
  • 1,161
  • 12
  • 16
17

I see 2 options to solve your problem:

peterh
  • 11,875
  • 18
  • 85
  • 108
Benoit Courtine
  • 7,014
  • 31
  • 42
  • 2
    I went with the antrun plugin, with one execution in prepare-package phase to create the renamed `.DS_Store`, and another in verify phase to delete it (just in case...). Looks like it's working, thanks! – wmorrell Oct 22 '10 at 17:16
14

You can avoid the over head of Ant by using the Maven Assembly plugin and the file assembly descriptor.

jgifford25
  • 2,164
  • 1
  • 14
  • 17
  • Not supported by filesets. So you can't apply a pattern to it :( – BeepDog Sep 01 '11 at 16:34
  • File renaming can be accomplished with dependencySets in the assembly plugin. – mojoken Jun 04 '14 at 22:50
  • 10
    could you provide an example? There's a lot of documentation there! – JonnyRaa Jun 13 '14 at 15:58
  • @BeepDog: Documentation http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_file says and help renaming file, which is not supported by . See the post by RobE for example how to use and – CuongHuyTo Feb 02 '15 at 09:21
  • 5
    Answers that just contain links are not considered good answers: http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – Marco Aug 20 '15 at 12:57