This post demonstrates how to add directories that contain resources needed by an application using build-helper-maven-plugin via Maven.
Simple Cases
In simple cases, resources like .properties and XSDs go with project codes. Consider the following Maven project that builds a codebase into an executable jar file.
When we mvn clean package our application, we’ll have the following contents of the jar file. Those clients-config-* directories become readily available to codes that come along with the jar file.
External Resource Directories
In this section, we try to externalize the clients-config-* resource directories. First, we move them to some external directory or directory that will not be read from during the build process.
If we mvn clean package our application without changing anything else, the content of our jar file is as follows.
1 | mvn clean package |
Using build-helper-maven-plugin plugin
To include all clients-config-* directory from somewhere-external, we need to use a Maven plugin – build-helper-maven-plugin. This requires modifying our pom.xml to
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <executions> <execution> <id>addExternalAllClientsConfigDirectories</id> <phase>generate-resources</phase> <goals> <goal>add-resource</goal> </goals> <configuration> <resources> <resource> <directory>${project.basedir}/somewhere-external</directory> </resource> </resources> </configuration> </execution> </executions> </plugin> |
Package again using
1 | mvn clean package |
and we will get the following content for the jar file.
Download the codes
https://github.com/Turreta/Add-external-resource-directories-to-jar-using-build-helper-maven-plugin
References
http://www.mojohaus.org/build-helper-maven-plugin/plugin-info.html