Maven is a well known build management tool to compile JVM based project (Scala, Groovy, Java…).
It’s purpose is to simplify your dependency management, downloading your needed jar from remote repository, executing maven plugin during different build phase (build-time weave your code, generate resources, unit or integration testing your project, generate Javadoc, licence, deploy to any server, etc…).
Maven uses the concept of Archetypes to generate your project skeleton, to keep you on rails and give you a standard project structure that is well done and fit your needs in the most common cases.
I personally use Maven in all my non too over engineered project (in that case, I’ll prefer Gradle as it is more permissive and allows build files testing) and made this tutorial as my future tutorials will be mostly based on it.
Dependency management section of a pom allows you to specify at one location the version of the dependencies that will be used on children pom.
The dependency tree of a jar is resolved transitively scanning the dependency pom to find its dependencies.
For example, when you specify the dependency to spring security web jar, you’ll also have a dependency to spring security core and another to spring beans (as ssc depends on it).
All these jar will be downloaded from remote to your local maven repo and will be used during each project build
Phases and plugins
Maven declares multiple build phases (the build lifecycle): validate, initialize, process-sources, process-resources, compile, test, package… Each of these phase represent a step on your project construction and these steps are executed sequentially (for example, when you run mvn process-sources, the phases validate, initialize and process-sources will be executed).
Maven is constituted of several plugins that have multiple goal that are binded to a (or some) phases: the maven-compiler-plugin have the goal ‘compile’ executed by default during the ‘compile’ maven phase
Maven generates standard structure for your project: a src/main/java folder where you put your java source files, a src/main/resource where you put your resources (config, langage and other spring beans files), a src/test/java for your tests, src/test/resources, src/main/webapp for web related files (war projects)…
This structure is generated from the stadard jar (or war) archetype but there are many other ones (check mvn archetype:list to have a list of all available ones).
As a prerequisite, you must have Maven install and M2E enabled on your Eclipse (it will simplify the steps to build the sample).
So now that we’re done, let’s check the content:
You’ll notice that the dependency Junit 3 is imported in our project, maybe because of the default unit test.
Notice that not plugin have been defined, that’s because compiler and junit plugins are imported by default.
Let’s add the Licence plugin that will generate our licence headers during the process-sources phase:
<inceptionYear>2013</inceptionYear> <organization> <name>Osgiliath</name> </organization> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>license-maven-plugin</artifactId> <version>1.5</version> <configuration> <licenseName>apache_v2</licenseName> </configuration> <executions> <execution> <id>first</id> <goals> <goal>update-file-header</goal> </goals> <phase>process-sources</phase> </execution> <execution> <id>second</id> <goals> <goal>update-project-license</goal> </goals> <phase>process-sources</phase> </execution> </executions> </plugin> </plugins> </build>
After running mvn install (or run as->maven install) we can now see our headers generated on top of the src/main/java/*/App.java file, and also a LICENCE file appearing on the root of our project.
You can now add more dependencies and other plugins to make a real project!
Standard plugin list (non exhaustive)
Maven central repository