In many multi-module Maven projects, we have noticed that there are certain resources that need to be shared across different modules. These can be images, database scripts, JavaScript files, style sheets, or any other resources. Developers follow different approaches in sharing resources. Some of them are listed here:
svn externals
. Here you only maintain resources in a single place, but all the modules need them, will get a copy when doing an svn up
.Of all the three, the use of the remote resource plugin is the best, as there is no resource duplication. With the remote resource plugin, first you need to create a Maven module, which includes all the resources that need to be shared. The following POM file defines the Maven module for all the shared resources:
<project> <groupId>com.packt</groupId> <artifactId>resources</artifactId> <build> <plugins> <plugin> <artifactId>maven-remote-resources-plugin</artifactId> <version>1.5</version> <executions> <execution> <goals> <goal>bundle</goal> </goals> </execution> </executions> <configuration> <includes> <include>**/*.png</include> <include>**/*.sql</include> <include>**/*.css</include> <include>**/*.js</include> </includes> </configuration> </plugin> </plugins> </build> </project>
The directory structure of the shared resources project will look like following. When you build the project, Maven will bundle all the resources into a JAR file during the generate-resources
phase of the Maven default lifecycle:
resources |-pom.xml |-src/main/resources/sql/*.sql |-src/main/resources/images/*.png |-src/main/resources/css/*.css |-src/main/resources/js/*.js
For any other Maven module to consume these resources, they have to take a dependency on the artifact produced by the preceding project and associate maven-remote-resources-plugin
with the build.
The following POM file defines the Maven module to consume shared resources:
<project> <groupId>com.packt</groupId> <artifactId>consumer</artifactId> <dependencies> <dependency> <groupId>${project.groupId}</groupId> <artifactId>resources</artifactId> <version>${project.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-remote-resources-plugin</artifactId> <version>1.5</version> <configuration> <resourceBundles> <resourceBundle> com.packt:resources:${project.version} </resourceBundle> </resourceBundles> </configuration> <executions> <execution> <goals> <goal>process</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>sql-maven-plugin</artifactId> <version>1.4</version> <executions> <execution> <id>create-schema</id> <phase>process-test-resources</phase> <goals> <goal>execute</goal> </goals> <configuration> <autocommit>true</autocommit> <srcFiles> <srcFile>${project.build.directory}/maven-shared-archive-resources/packt.sql </srcFile> </srcFiles> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
The maven-remote-resources-plugin
will read the resources from the com.packt:resources
artifact and copy them into the ${project.build.directory}/maven-shared-archive-resources
directory. As shown in the previous sample configuration, any other plugin can consume these resources, such as the sql-maven-plugin
.
3.22.77.63