This tutorial uses the Equinox example included with Exist Global Maestro to illustrate how to compile, test and package an application. Specifically, it explains about some of the features of the Maven Project Object Model (POM) file, as well as how to:
Read more about Maestro here.
This section explains several things you need to know before building the Equinox example application, including the Project Object Model (POM), the Super POM, how to use Plugins, and the Equinox POM. After that are sections that demonstrate how to compile, test sources, and run unit tests for the Equinox example.
A Project Object Model (POM) is Maven's description of a single, specific project. POMs are in the form of XML files with a combination of required and optional elements that describe the project. The required elements must be in every POM, and they represent the basic information needed to compile and test a project, as well as generate basic documentation. The optional elements let you include more detail about a project as you see fit. Below is an example POM file that contains only the required elements:
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.mydepartment</groupId> <artifactId>my-app</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
The following table describes the required elements that appear in the sample POM file:
POM File Required Elements
|project||The top-level element in all Maven pom.xml files.|
|modelVersion||Required element that indicates which version of the object model this POM uses.
The model version does not change often, but including this element ensures
stability when Maven introduces new features or model changes.
|groupId||Indicates the unique identifier for the organization or group that created
the project. This is one of the key identifiers of a project and is typically
based on the fully-qualified domain name of the organization. For example,
org.apache.maven.plugins is the designated groupId for all Maven plugins.
|artifactId||Indicates the unique base name of the primary artifact being generated by this
project. A typical artifact produced by Maven would have the form
<artifactId>-<version>.<extension> (for example, myapp-1.0.jar ).
Additional artifacts such as source bundles also use the artifactId as part
of their file name.
|packaging||Indicates the package type to be used by this artifact (JAR, WAR, EAR, etc.).
This not only means that the artifact produced is a JAR, WAR, or EAR, but also
indicates a specific lifecycle to use as part of the build process. The lifecycle
is a topic dealt with later in the chapter. For now, just keep in mind that the
selected packaging of a project plays a part in customizing the build lifecycle.
The default value for the packaging element is jar so you do not have to specify
this in most cases.
|version||This element indicates the version of the artifact generated by the project.
Maven goes a long way to help you with version management and you will often see
the SNAPSHOT designator in a version, which indicates that a project is in a
state of development.
|name||This element indicates the display name used for the project. This is often used
in Maven's generated documentation, and during the build process for your project,
or other projects that use it as a dependency.
|url||This element indicates where the project's site can be found.|
|description||This element provides a basic description of your project.|
Note: For a complete reference for both required and optional elements available for use in the POM, please refer to the POM reference at http://maven.apache.org/maven-model/maven.html.
The small, 15-line POM example can compile, test and document a project because Maven uses something called a "Super POM."
The Super POM contains all of the default conventions (patterns and best practices) that Maven encourages, and is analogous to the Java language's java.lang.Object class. Just like in Java, where all objects implicitly have the java.lang.Object class as a parent, all POMs in Maven implicitly have the Super POM as a parent. This means you do not have to repeat that information in every POM you build, which greatly simplifies the task (as shown in the previous small example).
Note: For a detailed explanation of what the Super POM contains, refer to Appendix B of Better Builds with Maven.
To customize the build for a Maven project, you must do one of the following:
For example, you may want to configure the Java compiler to allow JDK 5.0 sources. This is as simple as adding this following to your POM:
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> </project>
All plugins in Maven look very similar to a dependency, and in some ways, they actually are like dependencies. For example, if the plugin shown above is not already on your local system, it would be downloaded and installed automatically, very much like a dependency would be. To illustrate the similarities between plugins and dependencies, the sample includes the groupId and version elements. However, those are not required elements for plugins, and the following table explains their default values:
Default Values for groupID and version
|If you do not specify a value for this plugin element:||then:|
|groupID||Maven automatically attempts to locate the plugin within the org.apache.maven.plugins group.|
|version||Maven attempts to use the latest released version of the plugin. Often, accepting the default value
is the easiest way to use the plugin, because most plugin developers take care to ensure that new
versions are backwards-compatible. However, if you find something has changed, you can specify a
The configuration element applies the given parameters to every goal from the compiler plugin. In the sample, the compiler plugin is already used as part of the build process and this just changes its configuration.
If you want to find out what the plugin's configuration options are, use the mvn help:describe command. If you want to see the options for the maven-compiler-plugin shown previously, use the following command:
mvn help:describe -DgroupId=org.apache.maven.plugins -DartifactId=maven-compiler-plugin -Dfull=true