{

How to copy files to target build directory in maven?


This is a short tutorial on how to copy property files from an input directory to the target build directory in maven.

Maven creates the [standard directory structure] (/2011/12/maven-project-structure.html) for an application. You can check my other posts on maven commands

Default resources files are located in the `src/main/resources folder. which will be copied to target/classes during build and WEB-INF/classes in war file generation

When you are packaging the project, these properties are automatically copied to the target folder.

Sometimes you have a folder called src/conf which contains configuration properties. src/conffolder is not part of the maven standard folder structure.

How do you copy the files to the target folder during the package?

maven provides a maven-resources-plugin plugin that copies files from source to target folders.

The below code copies files from src/conf folder to 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?

Suppose you want to copy configuration files to the web-inf/lib of a war file. Then how can you copy the files?

You have to use the maven-war-plugin plugin in pom.xml

The below code copies files from src/conf to target/WEB-INF/lib folder as well as war file/WEB-INF/lib folder.

Under webResources, give input directory using the directory tag and provide includes the files to add. Configure Output path using 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>
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.