Maven is a Java build tool that not only compiles your project and automatically runs all it’s tests it also provides a really good directory structure and handles the retrieval of any JAR dependencies your project might have. Also as you might have guessed it builds your project in a way that it is then able to be used as a maven dependency it’s self.
This tutorial is only going to describe the very basic process of creating a new maven project. So lets begin.
Creating a maven project is as simple as running the following command.
#> mvn archetype:generate
...
365: remote -> wicket-scala-archetype (-)
366: remote -> wikbook.archetype (-)
367: remote -> circumflex-archetype (-)
368: remote -> javg-minimal-archetype (-)
Choose a number: 99:
Pro tip: If you get an error like the following saying that maven can’t find the archetype plugin this means that your repository is set to download a version of the plugin that is too new for the repository that you are using.
[INFO] Unable to find resource 'org.apache.maven.plugins:maven-archetype-plugin:maven-plugin:2.0-alpha-6' in repository central (http://repo1.maven.org/maven2)
To fix this you will need to open up the .m2/repository/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata-central.xml file and edit the <latest> and <release> tags. Place the value of one of the <version> tags within them that is a couple version down from the what is in there. It’s also a good idea to just stay away from alpha versions so maybe take the highest version that isn’t alpha. Your maven-metadata-central.xml file will then look as follows.
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<versioning>
<latest>2.0</latest>
<release>2.0</release>
<versions>
<version>1.0-alpha-3</version>
<version>1.0-alpha-4</version>
<version>1.0-alpha-7</version>
<version>2.0-alpha-1</version>
<version>2.0-alpha-2</version>
<version>2.0-alpha-3</version>
<version>2.0-alpha-4</version>
<version>2.0-alpha-5</version>
<version>2.0</version>
</versions>
<lastUpdated>20101028011818</lastUpdated>
</versioning>
</metadata>
This technique can also be used to fix any other plugins that might not download.
The mvn archetype:generate command will kick off a process to allow you to easily generate a maven project. You’ll be displayed with a list of around 300 – 400 custom maven projects that provide you with good starting points for different situations and frameworks, read through them, you might find a good starting point for your project, otherwise just select the default option (99: remote -> maven-archetype-quickstart) by pressing ENTER.
367: remote -> circumflex-archetype (-)
368: remote -> javg-minimal-archetype (-)
Choose a number: 99:
Choose version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6:
Next you will be asked which version of the template you would like to use, select the latest by pressing ENTER again.
Now you need to select how you want to name your project by giving the group id (namespace), artifactId (name), version, and package name (generally but not always the same as your given groupId) of your project. Fill these in with what ever you feel is apropriate.
5: 1.0
6: 1.1
Choose a number: 6:
Define value for property 'groupId': : org.project
Define value for property 'artifactId': : project
Define value for property 'version': 1.0-SNAPSHOT: 1.0
Define value for property 'package': org.project: #Press ENTER to select the groupId as the package name by default
Confirm properties configuration:
groupId: org.project
artifactId: project
version: 1.0
package: org.project
Y: #Press ENTER to confirm
You will now have a brand new maven project.
#> ls -R project/
project/:
pom.xml src
project/src:
main test
project/src/main:
java
project/src/main/java:
org
project/src/main/java/org:
project
project/src/main/java/org/project:
App.java
project/src/test:
java
project/src/test/java:
org
project/src/test/java/org:
project
project/src/test/java/org/project:
AppTest.java
That is it you can now begin developing within the maven project. To build and run tests use the following command.
#> mvn clean install
If the build command finishes successfully maven will place a copy of the JAR file it has built from your project into your local maven repository (the place where maven keeps all of your dependencies). This means that any classes from your project will be accessible from any other local maven projects as long as you add your project as a dependency. Your should remember though that your local build JAR’s only ever get updated after a successful run of mvn install so if you make a change in one project and want it to be seen by your other projects you will have to run a build.