How to copy files to target build directory in maven?


This is an short tutorials how how to copy property files from a input directory to 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 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 automatically copied to target folder.

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

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

maven provides maven-resources-plugin plugin which 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 expression with 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 file into WEB-INF/lib directory?

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

You have to use 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 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.