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 */.*


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.



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.