util Project
The first subproject discussed here is the util project because all the others depend on it. In this example, the util project contains only one class and is stored inside a jar file to be referenced by the other subprojects.
Under the src/java directory structure, I added the package structure of com/zarrastudios/util. Inside that package, I added the following class file:
package com.zarrastudios.util; public class UtilExample { public static void utilMethod() { //this is a util method. } }
Although this class does not do anything, it demonstrates how a utility project is utilized in a multiproject Maven structure. The project.xml file for the util project is very straightforward:
<project> <extend>../common/project.xml</extend> <name>Utility classes for project</name> <artifactId>util</artifactId> <shortDescription>Utility package for project</shortDescription> </project>
All the subprojects must extend from the common project.xml file to gain the benefits of its settings. The second setting is the name of this subproject, which is the name displayed by Maven during all of the builds and allows for easier debugging. The artifactId is the setting used to determine the filename for the jar of this subproject. Maven uses the shortDescription setting during report generation.
In addition to the project.xml file, there is also a maven.xml file for this subproject. Because each subproject needs to be built separately, I need to set up a common goal that is defined in each subproject, which will be called by the parent Maven build detailed below. The maven.xml file for the util subproject is as follows:
<project default="example:build"> <goal name="example:build" prereqs="jar:install"/> </project>
Again, because this is a very simple subproject, the maven.xml is equally simple. In it, I defined a default goal and set up a prerequisite for that goal. In this case, the prerequisite is jar:install, which will compile all the source files, place them in a jar, and install that jar in the local repository.
Because this subproject is self-contained, it can be built now with a command line call to Maven from the subproject's directory. If everything is set correctly, Maven will compile and install the jar file named util-1.0.jar.