Professional Documents
Culture Documents
rc055 010d Apachemaven 1 PDF
rc055 010d Apachemaven 1 PDF
rc055 010d Apachemaven 1 PDF
com
CONTENTS INCLUDE:
The MVN Command
n
Apache Maven 2
Project Object Model
n
Dependencies
n
Plugins
n
Debugging
n
By Matthew McCullough
<project>
ABOUT APACHE MAVEN <modelVersion>4.0.0</modelVersion>
<groupId>com.ambientideas</groupId>
<artifactId>barestbones</artifactId>
Maven is a comprehensive project information tool, whose <version>1.0-SNAPSHOT</version>
</project>
most common application is building Java code. Maven is
often considered an alternative to Ant, but as you’ll see in this Super POM
Refcard, it offers unparalleled software lifecycle management, The Super POM is a virtual pom.xml file that ships inside the
providing a cohesive suite of verification, compilation, testing, core Maven JARs, and provides numerous default settings. All
packaging, reporting, and deployment plugins. projects automatically inherit from the Super POM, much like
Maven is receiving renewed recognition in the emerging the Object super class in Java. Its contents can be viewed in
development space for its convention over configuration one of two ways:
approach to builds. This Refcard aims to give JVM platform View Super POM via SVN
developers a range of basic to advanced execution commands, Open the following SVN viewing URL in your web browser:
tips for debugging Mavenized builds, and a clear introduction http://svn.apache.org/repos/asf/maven/components/branches/maven-
2.1.x/pom.xml
to the “Maven vocabulary”.
View Super POM via effective-pom
Interoperability and Extensibility Run the following command in a directory that contains the
New Maven users are pleasantly surprised to find that Maven most minimal Maven project pom.xml, listed above.
offers easy-to-write custom build-supplementing plugins,
reuses any desired aspect of Ant, and can compile native mvn help:effective-pom
C, C++, and .NET code in addition to its strong support for Multi-module Projects
Java and JVM languages and platforms, such as Scala, JRuby,
w ww.dzone.com
The world of Maven revolves around metadata files named n Bonus content online
pom.xml. A file of this name exists at the root of every Maven n New issue every 1-2 weeks
Artifact Vector Conversely, if you ask Maven to execute a phase, all phases and
Each Maven project produces an element, such as a JAR, WAR bound plugin goals up to that point in the lifecycle are also
or EAR, uniquely identified by a composite of fields known as executed. This example requests the deploy lifecycle phase,
groupId, artifactId, packaging, version and scope. This vector which will also execute the verification, compilation, testing
of fields uniquely distinguishes a Maven artifact from all others. and packaging phases.
Many Maven reports and plugins print the details of a specific mvn deploy
artifact in this colon separated fashion:
Online and Offline
groupid:artifactid:packaging:version:scope
During a build, Maven attempts to download any uncached
An example of this output for the core Spring JAR would be: referenced artifacts and proceeds to cache them in the ~/.m2/
repository directory on Unix, or in the %USERPROFILE%/.m2/
org.springframework:spring:jar:2.5.6:compile
repository directory on Windows.
Clean Lifecycle
Lifecycle Phase Purpose
Lifecycles represent a well-recognized flow of steps (Phases) pre-clean
used in software assembly. clean Remove all generated and compiled artifacts in preperation
for a fresh build.
Each step in a lifecycle flow is called a phase. Zero or more
post-clean
plugin goals are bound to a phase.
The default lifecycle defines the most commonly used phases
A plugin is a logical grouping and distribution (often a single
for building an application, ranging from compilation of the
JAR) of related goals, such as JARing.
code to installation of the completed artifacts, such as a JAR,
A goal, the most granular step in Maven, is a single executable into a remote Maven repository.
task within a plugin. For example, discrete goals in the jar
Default Lifecycle
plugin include packaging the jar (jar:jar), signing the jar
Lifecycle Phase Purpose
(jar:sign), and verifying the signature (jar:sign-verify).
validate Cross check that all elements necessary for the build are
correct and present.
Executing a Phase or Goal
At the command prompt, either a phase or a plugin goal can initialize Set up and bootstrap the build process.
be requested. Multiple phases or goals can be specified and generate-sources Generate dynamic source code
are separated by spaces. process-sources Filter, sed and copy source code
mvn compile:compile jar:jar generate-test-sources Generate dynamic unit test source code.
Scope Description
Site Lifecycle
compile Needed for compilation, included in packages.
Lifecycle Phase Purpose
test Needed for unit tests, not included in packages.
pre-site Cross check that all elements necessary for the build are
correct and present. provided Needed for compilation, but provided at runtime by the runtime container.
site Generate an HTML web site containing project information system Needed for compilation, given as absolute path on disk, and not included in
and reports. packages.
post-site import An inline inclusion of a POM-type artifact facilitating dependency-declaring
POM snippets.
site-deploy Upload the generated website to a web server
assembly Creates ZIPs and other distribution packages of apps and their
transitive dependency JARs.
DEPENDENCIES
Declaring a Dependency Hot The full catalog of plugins can be found at:
To express your project’s reliance on a particular artifact, you Tip http://maven.apache.org/plugins/index.html
declare a dependency in the project’s pom.xml.
Define a Property
Analyze dependencies and list any that are unused, or
You can define a new custom property in your pom.xml like so:
undeclared.
<project>
mvn dependency:analyze [...]
<properties>
<my.somevar>My Value</my.somevar>
</properties>
REPOSITORIES [...]
</project>
mvnDebug <yourgoal>
Hot A near complete list of repositores can be found at Preparing to Execute Maven in Debug Mode
Tip http://www.mvnbrowser.com/repositories.html Listening for transport dt_socket at address: 8000
mvn scm:update-subprojects
CUTTING A RELEASE
makers. DZone offers something for everyone, including news, Sponsorship Opportunities
tutorials, cheatsheets, blogs, feature articles, source code and more. sales@dzone.com 9 781934 238578
“DZone is a developer’s dream,” says PC Magazine.
Copyright © 2009 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, Version 1.0
photocopying, or otherwise, without prior written permission of the publisher. Reference: