Renaming resources in Maven

后端 未结 5 1489
谎友^
谎友^ 2020-12-02 19:43

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 workar

相关标签:
5条回答
  • 2020-12-02 20:15

    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>
    
    0 讨论(0)
  • 2020-12-02 20:21

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

    0 讨论(0)
  • 2020-12-02 20:26

    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/

    0 讨论(0)
  • 2020-12-02 20:40

    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>
    
    0 讨论(0)
  • 2020-12-02 20:42

    I see 2 options to solve your problem:

    • Use the Maven-Ant-Plugin, and define an Ant rename task that will rename your file only at the packaging phase, in the build directory.
    • Use this dedicated Maven plugin (I didn't test it): http://code.google.com/p/maven-file-rename-plugin/
    0 讨论(0)
提交回复
热议问题