How to copy files to target build directory in maven?

This tutorial provides a quick guide on copying property files from an input directory to the target build directory in Maven.

A Maven project adheres to the standard directory structure for an application. Additional information on maven commands can be found in my other posts, such as this one.

Default resource files are situated in the src/main/resources folder. These files are automatically copied to the target/classes folder during the build process and to the WEB-INF/classes folder during the generation of the WAR file.

When packaging the project, properties are automatically copied to the target folder. However, if you have a non-standard folder, such as src/conf containing configuration properties, you can use the maven-resources-plugin to facilitate the file transfer from the source to target folders.

The following code snippet illustrates how to copy files from the src/conf folder to the target/classes/conf folder:

You have to use regular expressions with the includes value as */.*

     <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>install</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/classes/conf/</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/src/conf/</directory>
                                <includes>
                                    <include>**/*.*</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

How to make Maven copy resource files into WEB-INF/lib directory?

If you intend to copy configuration files to the WEB-INF/lib of a WAR file, you can achieve this using the maven-war-plugin in the pom.xml.

The following code snippet demonstrates how to copy files from src/conf to both the target/WEB-INF/lib folder and the war file’s target/WEB-INF/lib folder

Here, the web resources configuration allows you to specify the input directory using the directory tag, include files using the includes tag, and set the output path using the targetPath tag.

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.1</version>
        <configuration>
          <webResources>
           <directory>/src/conf</directory>
          <includes>
            <include>**/*.conf</include>
          <includes>
         <targetPath>WEB-INF/lib</targetPath>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>

Conclusion

To summarize, Learned how to copy the files or folders from the source to the target build directory in maven using the maven-resources-plugin.