-
Notifications
You must be signed in to change notification settings - Fork 6
MavenHowTo
#Introduction to Maven
TermGenie uses Maven as build tool. It can be used on the command line and in various IDEs. There are maven plugins for Eclipse, NetBeans, and IntelliJ.
What are we using maven for:
-
Dependency management
Maven allows to specify dependencies (I want this jar in this version) and provides a convenient way to deal with them without commiting every jar to the svn repository.
How does maven implements this?
Maven describes every project with a pom.xml. This descriptor contains all the information about the project, including a triple
(groupId, artifactId, version)
as unique identifier and its dependencies on other projects. These dependencies are also specified as triples(groupId, artifactId, version)
.Now, if you specify a dependency, maven looks in its repositories whether it can retrieve the jar. To make this reasonable fast, it downloads the files to a local cache and links it into your classpath. Furthermore, it also includes the transitive dependencies, specified in the pom.xml associated with the jar. Maven does this recursively, till it finds no more new dependencies.
So instead of having the jar dependencies in your svn, the dependency jars are stored in an central maven-repository (public or your own) and cached in a local folder ($HOME/.m2/repository).
-
Build tool
Maven has a nice build cycle, which includes automated testing as part of the release cycle. Furthermore, Maven has a feature called reactor build. This allows Maven to use the recently compiled classes, instead of the deployed jar for modules of a project. With the reactor build, it is possible to compile modules and find compile errors without the need to create/install intermediate jars.
-
Project modularization
Maven has build-in capabilities to handle multi-module projects including a reactor build and centralized configuration
-
Workspace resolution of projects in Eclipse: no more rebuilding and copying a jar to see the changes of a project in you own.
Of course Maven does not solve all problems. You just move the problem somewhere else. Although maven tries to solve many dependency and version issues, it is sometimes required to do this by hand (explicit excludes of transitive dependencies).
Furthermore, someone has to write the POM and there are cases, where there is no POM and you have to create your own descriptor for a jar and find the relevant dependencies.
#Usage
##Command-line Most important command:
mvn clean install
This will clean up first, then compile the project, execute the tests, and create the jar. If any of the tests fails, the jar will not be created. To skip the test execution add -Dmaven.test.skip.exec=true
to the command.
The reactor build feature can be used, in the TermGenie parent folder, where the parent pom.xml is located specifying its modules. For a quick build execute:
mvn clean compile
Another use case is to build a specific project and all required modules for it. Start by going to the TermGenie parent folder. Execute:
mvn clean install -am -pl <Module>
Whereas the <Module>
is the project/module name. This will trigger a recursive build of all the required modules.
For more details see the maven documentation.
##Eclipse
In general Maven is an additional project nature in Eclipse. Once activated, the plugin creates a container, which has all the dependencies. The nice part is that the plugin allows workspace resolution, meaning the project is the dependency instead of a fixed jar.
####Combine Eclipse and Command-Line In general Eclipse assumes: it is the only one compiling and updating resources. So if you did something on the command-line, you need to update the project (F5) and "Project > clean" in Eclipse. Otherwise, there might be some ClassNotFoundExceptions and other strange things, while using Eclipse.
#Installation
##Command-line
The recommended command-line version is 3.x, where x is the latest release. Follow the instructions on the maven page to install it (here)[http://maven.apache.org/download.html#Installation].
The goal is to run the mvn
command in any (project)-folder.
Remark: Although Eclipse does not need a command-line version, it is recommended to be able to make builds on the command-line
##Eclipse
The recommended maven plugin is m2e. This plugin for Eclipse requires Eclipse 3.6 or later. The recommended version is Eclipse 3.7 or later.
####Eclipse 3.6 (Helios)
Installation via its eclipse update site: http://download.eclipse.org/technology/m2e/releases
Please do not use the out-dated versions (i.e. m2eclipse) from sonatype or similar. The required version for m2e is 1.0.0 or later. You need to un-install all older versions.
####Eclipse 3.7 (Indigo) or later
Since Eclipse 3.7 the plugin is pre-installed with the release.