Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.
Ask ten developers about maven and you’ll probably get eleven opinions. The more experienced developers are apt to be on their 3rd or 4th opinion. My opinion of maven is similar to Churchill’s opinion of democracy:
democracy is the worst form of Government except for all those other forms that have been tried …
Maven does everything – it will generate a template, download dependencies, build the project, run the tests and deploy the project. Let’s see how easy it is to build a jersey web application with maven.
Start by downloading Apache Maven and installing. Be forewarned that maven will download all dependencies to a local repository below your home directory (${user.home}/.m2/repository). I don’t want these mixed into the standard backup of my home directory, so I change the location by specifying an alternate location in my maven settings file. 1
2
3
4$ cat ~/.m2/settings.xml
<settings>
<localRepository>/MyApps/Developer/MavenRepo</localRepository>
</settings>
I like to start all projects by building the simplest possible example, the proverbial hello, world. This lets me know that I’ve got all my pieces in place. In maven, this simple project is known as an archetype (“model from which all things of the same kind are based”). Let’s get started with the archetype-generate command. 1
2
3
4
5$ mvn archetype:generate
...
1158: remote -> tr.com.lucidcode:kite-archetype (A Maven Archetype that allows users to create a Fresh Kite project)
1159: remote -> uk.ac.rdg.resc:edal-ncwms-based-webapp (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 475:
Maven responds with its full set of known archetypes. Enter a keyword at this point to filter the full set to a smaller set. In this case, enter jersey to filter our choices down. 1
2
3
4
5
6Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 475: jersey
1: remote -> com.pampanet:jersey-guice-webapp-archetype (Archetype for building RESTful Web Services with Jersey 1.18.1 and Google Guice 3.0)
...
11: remote -> org.glassfish.jersey.archetypes:jersey-quickstart-webapp (An archetype which contains a quick start Jersey-based web application project.)
...
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): :
Enter 11 for a jersey-quickstart-webapp. 1
2
3
4
5
6
7Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 11
Choose org.glassfish.jersey.archetypes:jersey-quickstart-webapp version:
1: 2.0-m03
...
44: 2.11
45: 2.12
Choose a number: 45:
And then 45 to select jersey version 2.12. Next maven will query for your project properties. The first three properties specify how your project will be defined within maven (in case it becomes a dependency for another project). The last property defines the java project path that you’d like to start with. 1
2
3
4
5
6
7
8
9
10
11Choose a number: 45: 45
Define value for property 'groupId': : com.ideoplex.tutorial
Define value for property 'artifactId': : jersey-gson
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': com.ideoplex.tutorial: :
Confirm properties configuration:
groupId: com.ideoplex.tutorial
artifactId: jersey-gson
version: 1.0-SNAPSHOT
package: com.ideoplex.tutorial
Y: :
Confirm your property values and maven will generate your starting point.
1
2
3
4
5
6
7
8
9
10
11
12
13Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: jersey-quickstart-webapp:2.12
[INFO] ----------------------------------------------------------------------------
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:21 min
[INFO] Finished at: 2014-09-20T17:27:53-04:00
[INFO] Final Memory: 13M/245M
[INFO] ------------------------------------------------------------------------
$
A quick look reveals that maven has generated the project pom, web.xml, java class and jsp page in the maven standard directory layout. 1
2
3$ ls **/*.java **/*.jsp **/*.xml
pom.xml src/main/webapp/WEB-INF/web.xml
src/main/java/com/ideoplex/tutorial/MyResource.java src/main/webapp/index.jsp
At this point, let maven build your project. Package is a single stage from the maven lifecycle that takes the compiled code and packages it into a distributable format (jar, war, …). 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building jersey-gson 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...
------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.156 s
[INFO] Finished at: 2014-09-20T17:31:25-04:00
[INFO] Final Memory: 15M/245M
[INFO] ------------------------------------------------------------------------
$ ls target
classes/ generated-sources/ jersey-gson/ jersey-gson.war maven-archiver/