The Apache Maven Resources Plugin is essential for copying resources from one directory to another during the build process of your Maven project. This guide will walk you through configuring and using the Maven Resources Plugin with a complete example.
The Maven Resources Plugin handles the copying of project resources (e.g., configuration files, images, etc.) from the src/main/resources and src/test/resources directories to the target/classes and target/test-classes directories, respectively.
Latest Version
As of this writing, the latest version of the Maven Resources Plugin is 3.2.0. Using the latest version ensures access to the newest features and bug fixes.
To use the Maven Resources Plugin, you need to configure it in your project's pom.xml file. Let's go through the steps to set up a complete Maven project with the Resources Plugin.
Step 1: Create a Maven Project
Run the following command to create a new Maven project:
mvn archetype:generate -DgroupId=com.example -DartifactId=resources-plugin-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command will generate a simple Maven project with the following structure:
resources-plugin-demo |-- src | |-- main | | |-- java | | | `-- com | | | `-- example | | | `-- App.java | | `-- resources | | `-- config.properties | `-- test | |-- java | | `-- com | | `-- example | | `-- AppTest.java | `-- resources | `-- test-config.properties |-- pom.xml `-- target
Step 2: Add Maven Resources Plugin Configuration
Navigate to the project directory and open the pom.xml file. Add the Maven Resources Plugin configuration inside the <build> section:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>resources-plugin-demo</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>default-resources</id> <phase>process-resources</phase> <goals> <goal>resources</goal> </goals> </execution> <execution> <id>default-testResources</id> <phase>process-test-resources</phase> <goals> <goal>testResources</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
Explanation
Step 3: Add Resources to src/main/resources
Create a file named config.properties in the src/main/resources directory with the following content:
app.name=Resources Plugin Demo app.version=1.0-SNAPSHOT
Step 4: Add Resources to src/test/resources
Create a file named test-config.properties in the src/test/resources directory with the following content:
test.name=Resources Plugin Test test.version=1.0-SNAPSHOT
Step 5: Modify the App.java to Load Resources
Open src/main/java/com/example/App.java and modify it to load and print the properties from config.properties:
package com.example; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class App { public static void main(String[] args) { Properties properties = new Properties(); try (InputStream input = App.class.getClassLoader().getResourceAsStream("config.properties")) { if (input == null) { System.out.println("Sorry, unable to find config.properties"); return; } properties.load(input); System.out.println("App Name: " + properties.getProperty("app.name")); System.out.println("App Version: " + properties.getProperty("app.version")); } catch (IOException ex) { ex.printStackTrace(); } } }
Step 6: Build and Run the Project
Run the following command to build the project and process the resources:
mvn clean package
Execute the packaged JAR file to see the output from config.properties:
java -jar target/resources-plugin-demo-1.0-SNAPSHOT.jar
You should see the following output:
App Name: Resources Plugin Demo App Version: 1.0-SNAPSHOT
Here is the complete pom.xml file for the example project:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>resources-plugin-demo</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>default-resources</id> <phase>process-resources</phase> <goals> <goal>resources</goal> </goals> </execution> <execution> <id>default-testResources</id> <phase>process-test-resources</phase> <goals> <goal>testResources</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
The Maven Resources Plugin is a crucial tool for managing resources in your Maven projects. This guide provided a complete example of setting up and using the Maven Resources Plugin, helping you efficiently handle resource copying during the build process. By following these steps, you can easily manage resources for your Maven projects.