13

I want to know how to copy an entire directory into another directory using Maven without using the Mmaven antrun plugin.

james.garriss
  • 12,959
  • 7
  • 83
  • 96
galme
  • 603
  • 3
  • 10
  • 24

2 Answers2

19

You can use the Maven resources plugin.

As an example taken from their documentation:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/extra-resources</outputDirectory>
              <resources>          
                <resource>
                  <directory>src/non-packaged-resources</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>

This would copy the content of the directory into the outputDirectory if I'm not mistaken.

Eugene
  • 117,005
  • 15
  • 201
  • 306
André Stannek
  • 7,773
  • 31
  • 52
  • 1
    `validate` did it for me, thank you! Now Maven runs copy-resources whenever I save any file in my project :) – Cardin Nov 06 '18 at 04:02
  • 1
    Yes, but you're basically recreating the input directory as the output directory instead of "copy an entire directory into another directory". If I wanted to include another resource, it would need a whole other execution with its own outputDirectory – caduceus Jan 25 '21 at 08:25
0

You can use https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html

<?xml version="1.0" encoding="UTF-8"?>
<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-dependency-plugin</artifactId>
         <version>3.1.2</version>
         <executions>
            <execution>
               <id>copy</id>
               <phase>package</phase>
               <goals>
                  <goal>copy</goal>
               </goals>
            </execution>
         </executions>
         <configuration>
            <artifactItems>
               <artifactItem>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>3.8.1</version>
                  <type>jar</type>
                  <overWrite>false</overWrite>
           <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
                  <destFileName>optional-new-name.jar</destFileName>
               </artifactItem>
            </artifactItems>
            <outputDirectory>${project.build.directory}/wars</outputDirectory>
            <overWriteReleases>false</overWriteReleases>
            <overWriteSnapshots>true</overWriteSnapshots>
         </configuration>
      </plugin>
   </plugins>
</build>