1. Introduction

Maven is a build automation tool that allows Java developers to manage a project's build, reporting, and documentation from a centralized location – the POM (Project Object Model).

When we build a Java project, we often require arbitrary project resources to be copied to a specific location in the output build – we can achieve this with Maven through the use of several different plugins.

In this tutorial, we'll build a Java project and copy a specific file to a destination in the build output, using:

2. Using the Maven Resources Plugin

The maven-resources-plugin handles the copying of project resources to an output directory.

Let's start by adding the plugin to our pom.xml:

<project>
    ...
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                ...
            </plugins>
        </pluginManagement>
        <!-- To use the plugin goals in your POM or parent POM -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
            </plugin>
            ...
        </plugins>
    </build>
    ...
</project>

Then, let's create a folder in the project root called source-files. It'll contain a text file that we want to copy: foo.txt. We'll then add a configuration element to the maven-resources-plugin to copy this file to target/destination-folder:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
        <execution>
            <id>copy-resource-one</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/target/destination-folder</outputDirectory>
                <resources>
                    <resource>
                        <directory>source-files</directory>
                        <includes>
                            <include>foo.txt</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

After building the project, we can find the foo.txt in the target/destination-folder.

3. Using the Maven Antrun Plugin

The maven-antrun-plugin provides the ability to run Ant tasks from within Maven. We'll use it here to specify an Ant task that copies a source file to a destination.

The plugin is defined in the pom.xml as follows:

<project>
    [...]
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>
                            <phase>generate-sources</phase>
                        </phase>
                        <configuration>
                            <target>
                                <!-- Place any Ant task here. -->
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

We'll perform the same example as above: copying source-files/foo.txt to target/destination-folder/foo.txt – we'll achieve this by defining an Ant task to perform the copy:

<configuration>
    <target>
        <mkdir dir="${basedir}/target/destination-folder" />
        <copy todir="${basedir}/target/destination-folder">
            <fileset dir="${basedir}/source-files" includes="foo.txt" />
        </copy>
    </target>
</configuration>

After building the project we'll find foo.txt in target/destination-folder.

4. Using the Copy Rename Maven Plugin

The copy-rename-maven-plugin helps copying files or renaming files/directories during the Maven build lifecycle.

The plugin can be installed by adding the following entry to our pom.xml:

<project>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>com.coderplus.maven.plugins</groupId>
                <artifactId>copy-rename-maven-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <id>copy-file</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <!-- Place config here -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

We'll now add some config to perform the copy: source-files/foo.txt to target/destination-folder/foo.txt:

<configuration>
    <sourceFile>source-files/foo.txt</sourceFile>
    <destinationFile>target/destination-folder/foo.txt</destinationFile>
</configuration>

Upon building the project, we'll see foo.txt in the target/destination-folder.

5. Conclusion

We've successfully copied a source file to a destination using three different Maven plugins. Each one operates slightly differently, and while we've covered copying a single file here, the plugins are capable of copying multiple files and in some instances, entire directories.

Our other articles plus the official documentation for each of the plugins go into further detail about how to perform more complicated operations.

The source code for these examples can be found over on GitHub.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
guest
0 Comments
Inline Feedbacks
View all comments