Taking the Maven 2 Plunge, Page 2
Lifecycles are groupings of goals that define a process for building a project. Goals are bound to lifecycles to define a sequence that must be accomplished to produce results. Lifecycles ensure that users building projects with Maven only need to learn a small set of commands. Whereas Maven defines a default set of goals for each typical lifecycle, developers can bind additional goals to lifecycles to transparently add functionality to a build.
Typical lifecycles include:
- compile—compile the source code
- test—unit test the compiled source.
- package—bundle the compile and tested source code into a distributable package, such as a jar or war.
- integration-test—employ the package as necessary and run integration tests.
- install—install the package into the local repository. This allows the package to be utilized as a dependency of other projects.
- deploy—copies the final package into the remote repositories for sharing and use with other projects.
Note: Unit tests should not require the classes to be packaged or deployed.
As you can see, each lifecycle depends upon and builds upon the next. As a result, when a goal is bound to the compile lifecycle, all lifecycles will ensure its execution.
Lifecycles can be executed in conjunction with standalone goals. For instance, commonly the clean:clean goal is executed before the install goal. This is done by invoking:
m2 clean:clean install
Native Multiproject Support
One of the best practices that Maven strongly encourages is the idea that a single project should result in a single artifact, or package, being created. This best practice results in simplified builds and organized project structures. By natively supporting a hierarchical structure of projects, Maven now makes developing enterprise projects which implement this approach easier.
Maven 1 included a plugin for working with related of projects—or multiprojects. Maven 2 takes multiproject builds a step further by including a specialized parent descriptor and native multiproject execution support. As a result, any goals or lifecycles invoked upon a multiproject POM will result in each subproject goal being achieved.
Several other changes are included in Maven 2. Maven has been rewritten to be smaller and faster. The architectural changes make embedding Maven into other tools easier and allows for faster command line execution. Maven 2 depends upon fewer dependencies, resulting in a smaller distribution.
The way extensions are made to Maven has been changed in Maven 2. Instead, developers are encouraged to utilize JavaBeans for enhancements. Whereas scripting is allowed (through marmalade support—which includes a jelly facade), it is no longer the tool of choice. All extensions are made through the development of plugins (projects can no longer be customized through scripting in the maven.xml).
The introduction of a settings.xml file replaces the need for properties files. The settings file can be defined at a system, user, or project level. Settings are used to define private configuration information, including usernames and passwords. Project properties (including plugin configuration) are now all defined in the pom.xml.
Take the Plunge
Understanding Maven 2 Project Types
The first step to creating a maven project is determining which project template, or archetype, your Maven project should be configured as. Archetypes define which type of artifact will be produced by a project. Several archetype templates exist. The standard archetype will produce a standard library jar file. Templates also exist for webapps/wars, Maven plugin projects, documentation Web sites, and more.
Creating Project Descriptors
The archetype:create goal can be utilized to create a basic pom.xml for you project. This basic POM will allow you to execute all of the lifecycles and goals associated with any maven project. The archetype:create goal should be executed as follows:
m2 archetype:create -DgroupdId=com.daviddewolf.maven -DartifactId=example
This simple command creates the basic infrastructure needed for a project. It creates the pom.xml as well as the basic directory structure of for both source and test code. The following structure indicates the directories and files produced by issuing the archetype:goal command as listed above.
+ root - pom.xml + src + main + java + com + daviddewolf + maven + example - App.java + test + java + com + daviddewolf + maven + example - TestApp.java
This simple structure that is created is enough to get started with Maven. All of the lifecycles now can be utilized. A simple invocation of the install lifecycle will compile, test, and package the application and then deploy it to the local system repository for use by other projects.
Once created, the POM can be customized to meet the requirements of the specific project. Documentation on the Maven POM can be found on the Maven2 Web site.
Goals You Should Know About
The introduction of lifecycles in Maven 2 has greatly reduced the number of goals that a developer must be aware of. Still, the following goals will undoubtedly be found useful (and can be utilized as soon as the basic descriptor has been generated).
- clean:clean Clean all artifacts and intermediary files created by Maven
- idea:idea Generate project files for the IntelliJ IDEA IDE
- eclipse:eclipse Generate project files for the Eclipse IDE
- javadoc:javadoc Generate the javadocs for the project
- antrun:run Run a specified ant target
- clover:clover Generate a coverage report for the project
- checkstyle:checkstyle Generate a checkstyle report for the project
- site:site Generate a documentation Web site for the project. This site will include many information reports concerning the project.
For more information concerning these and other goals, see the Maven2 Web site.
Learning Maven is not difficult; it simply requires a willingness to accept a new philosophy for building applications. Maven utilizes a a centralized project descriptor to intelligently build applications with prebuilt build tools. This differs greatly from ant and other build tools in that project developers are no longer required to write build systems by using a comprehensive set of utilities. This change will save development teams significant time and has started a revolution in build tools.
It is important to remember that Maven2 is still currently only released as Beta Software. Although it is mature enough to utilize in most projects, all of the Maven 1 plugins have not yet been migrated to Maven 2. With time, Maven 2 will become more widely accepted and the number of available plugins will grow beyond what is even available in Maven 1.
About the Author
David DeWolf is a Senior Architect and Agile Coach at Digital Focus where he mentors agile practices such as automated builds, continuous integration, and test first development. Through his experience, he has found Maven to be the most productive project management (build) tool for achieving these practices. David is a member of the Apache Portals Project Management Committee and an active committer to Apache Pluto.