This afternoon I attended Hermod Opstvedt's talk on Using Maven 2 to get control over your Development Process. Most of it was review for me, but I took some notes anyway. About halfway through, I quit taking notes and just listened. The most interesting part for me was seeing how the Maven Embedder works. Since Maven doesn't currently allow you to create archetypes from existing projects the embedder seems like a good workaround. I'd rather code in Java rather than XML any day.
Near the end, Hermod tried to show us how you can easily create an Ant-based Mojo (Maven plugin), but his demo bombed thanks to an invalid pom in one of the plugin snapshots. Made Maven look pretty bad IMO - but snapshots always mess things up. ;-) The room was packed. Here's my notes:
What is Maven?
It's a kind of project management tool. It focuses on standards and best practices, uses convention over configuration, shared language for build management, repeatable robust builds and acts as a central point in the Project Object Model (POM).
Maven is a result of trying to make several Apache Software Foundation (ASF) project work in the same predictable way. Offshoot of the Turbine project and started by Jason van Zyl.
Creates a common and understandable build infrastructure. If you know Maven, moving from one project to the next means you can focus on the problem at hand and not spend time trying to figure out the build system.
Common problems in a project: Traditionally, the larger the project, the more complex and build system becomes. Very often the build system is copied form a previous project that the senior developers participate in. Setting up an initial project through copy and past obviously can lead to interesting issues. Versioning of a project often becomes a nightmare if a strict regime of code management is not introduced. Documentation process is often "put off until later". Testing infrastructure is generally mediocre at best.
Maven solves these problems by introducing common build logic and uses a standard naming convention. There is also a single output from a single Maven project (often a contentious issue).
The Maven standard build lifecycle
provides a lifecycle to hook into to perform custom logic with plugins.
What is an archetype? It's a template of a project which is combined with some user input to produce a working Maven project that has been tailored to your requirements. It's used to initialize a new project of a particular kind. To use the archetype plugin, it's as simple as running:
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
For more information, see Maven's Guide to Webapps
Archetypes are very dumb for the most part - they're just a flat directory structure with files in it. However, if you want to create complex archetypes that execute
something, it can't be done using the regular way. The solution is:
- Start out by creating an archetype for your standard structure
- Then in the pom for the project, add an <scm> section that will point to where your code is stored.
- Run "mvn archetype:create".
- Change to the root of your newly created project and run "mvn scm:update".
An alternative to all this is to create an Ant-based mojo. A Mojo
is a Maven plugin that executes some code at a given lifecycle. It can run Java code, Ant scripts and more. the reason for using an Ant based mojo is that to be able to run more than one task in a mojo, we need to us what is known as the Maven Embedder.