Best practices for copying files with Maven

I have config files and various documents that I want to copy from the dev environment to the dev-server directory using Maven2. Strangely, Maven does not seem strong at this task.


Some of the options:

  • Simple use a copy task in Maven
  • <copy file="src/main/resources/config.properties" tofile="${project.server.config}/config.properties"/>

  • Use the Ant plugin to execute copy from Ant.

  • Construct an artifact of type zip, alongside the "main" artifact of the POM which is usually of type jar, then unpack that artifact from the repository into the target directory.

  • maven-resources plugin, as mentioned below.

  • Maven Assembly plugin -- but this seems to require a lot of manual definitions, when I want to do things simply and "conventionally."

  • This page even shows how to build a plugin to do copying!

  • maven-upload plugin, as mentioned below.

  • maven-dependency-plugin with copy, as mentioned below.


  • All these seem needlessly ad hoc: Maven is supposed to excel at doing these standard tasks without fuss and bother.

    Any advice?


    Don't shy away from the Antrun plugin. Just because some people tend to think that Ant and Maven are in opposition, they are not. Use the copy task if you need to perform some unavoidable one-off customization:

    <project>
      [...]
      <build>
        <plugins>
          [...]
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <phase>deploy</phase>
                <configuration>
                  <tasks>
    
                    <!--
                      Place any Ant task here. You can add anything
                      you can add between <target> and </target> in a
                      build.xml.
                    -->
    
                  </tasks>
                </configuration>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      [...]
    </project>
    

    In answering this question, I'm focusing on the details of what you asked. How do I copy a file? The question and the variable name lead me to a larger questions like: "Is there a better way to deal with server provisioning?" Use Maven as a build system to generate deployable artifact, then perform these customizations either in separate modules or somewhere else entirely. If you shared a bit more of your build environment, there might be a better way - there are plugins to provision a number of servers. Could you attach an assembly that is unpacked in the server's root? What server are you using?

    Again, I'm sure there's a better way.


    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.3</version>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include> **/*.properties</include>
                </includes>
            </resource>
        </resources>
        ...
    </build>
    

    In order to copy a file use:

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>copy-resource-one</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
    
                        <configuration>
                            <outputDirectory>${basedir}/destination-folder</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>/source-folder</directory>
                                    <includes>
                                        <include>file.jar</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
               </executions>
            </plugin>
    

    In order to copy folder with sub-folders use next configuration:

               <configuration>
                  <outputDirectory>${basedir}/target-folder</outputDirectory>
                  <resources>          
                    <resource>
                      <directory>/source-folder</directory>
                      <filtering>true</filtering>
                    </resource>
                  </resources>              
                </configuration>  
    
    链接地址: http://www.djcxy.com/p/29672.html

    上一篇: 将外部jar(lib / *。jar)添加到IntelliJ IDEA项目的正确方法

    下一篇: 使用Maven复制文件的最佳做法