Chapter 16. Building with Maven
Abstract
Maven is an open source build system which is available from the Apache Maven project. This chapter explains some of the basic Maven concepts and describes how to set up Maven to work with Red Hat Fuse. In principle, you could use any build system to build an OSGi bundle. But Maven is strongly recommended, because it is well supported by Red Hat Fuse.
16.1. Maven Directory Structure
16.1.1. Overview
One of the most important principles of the Maven build system is that there are standard locations for all of the files in the Maven project. There are several advantages to this principle. One advantage is that Maven projects normally have an identical directory layout, making it easy to find files in a project. Another advantage is that the various tools integrated with Maven need almost no initial configuration. For example, the Java compiler knows that it should compile all of the source files under src/main/java
and put the results into target/classes
.
16.1.2. Standard directory layout
Example 16.1, “Standard Maven Directory Layout” shows the elements of the standard Maven directory layout that are relevant to building OSGi bundle projects. In addition, the standard locations for Blueprint configuration files (which are not defined by Maven) are also shown.
Example 16.1. Standard Maven Directory Layout
ProjectDir/
pom.xml
src/
main/
java/
...
resources/
META-INF/
OSGI-INF/
blueprint/
*.xml
test/
java/
resources/
target/
...
It is possible to override the standard directory layout, but this is not a recommended practice in Maven.
16.1.3. pom.xml file
The pom.xml
file is the Project Object Model (POM) for the current project, which contains a complete description of how to build the current project. A pom.xml
file can be completely self-contained, but frequently (particular for more complex Maven projects) it can import settings from a parent POM file.
After building the project, a copy of the pom.xml
file is automatically embedded at the following location in the generated JAR file:
META-INF/maven/groupId/artifactId/pom.xml
16.1.4. src and target directories
The src/
directory contains all of the code and resource files that you will work on while developing the project.
The target/
directory contains the result of the build (typically a JAR file), as well as all all of the intermediate files generated during the build. For example, after performing a build, the target/classes/
directory will contain a copy of the resource files and the compiled Java classes.
16.1.5. main and test directories
The src/main/
directory contains all of the code and resources needed for building the artifact.
The src/test/
directory contains all of the code and resources for running unit tests against the compiled artifact.
16.1.6. java directory
Each java/
sub-directory contains Java source code (*.java
files) with the standard Java directory layout (that is, where the directory pathnames mirror the Java package names, with /
in place of the .
character). The src/main/java/
directory contains the bundle source code and the src/test/java/
directory contains the unit test source code.
16.1.7. resources directory
If you have any configuration files, data files, or Java properties to include in the bundle, these should be placed under the src/main/resources/
directory. The files and directories under src/main/resources/
will be copied into the root of the JAR file that is generated by the Maven build process.
The files under src/test/resources/
are used only during the testing phase and will not be copied into the generated JAR file.
16.1.8. Blueprint container
OSGi R4.2 defines a Blueprint container. Red Hat Fuse has built-in support for the Blueprint container, which you can enable simply by including Blueprint configuration files, OSGI-INF/blueprint/*.xml
, in your project. For more details about the Blueprint container, see Chapter 10, OSGi Services.