Maven is a software project management tool. Its features include –
It is also extensible thru its plugin architecture.
Archetypes
Archetypes are a good starting point for understanding maven. They are a templating system to allow you to quickly start your projects.
If you run –
mvn archetype:generate -DgroupId=com.glenware
-DartifactId=SampleWebApp
-DarchetypeArtifactId=maven-archetype-webapp
This will create the directory structure below –
SampleWebApp
|-src
|—main
|—–resources
|—–webapp
|——-index.jsp
|——-WEB-INF
|———web.xml
|-pom.xml
This gives a developer a good starting point, or if they prefer you can prepare the project to import into your favourite IDE
mvn eclipse:eclipse -Dwtpversion=2.0
The core of maven is the pom.xml –
[sourcecode language=”xml”] <project xmlns="<a href="http://maven.apache.org/POM/4.0.0">http://maven.apache.org/POM/4.0.0</a>" xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>"The pom is highly configurable, as is shown here –
[sourcecode language=”xml”] <project xmlns="<a href="http://maven.apache.org/POM/4.0.0">http://maven.apache.org/POM/4.0.0</a>" <!– The Basics –>
<groupId>…</groupId>
<artifactId>…</artifactId>
<version>…</version>
<packaging>…</packaging>
<dependencies>…</dependencies>
<parent>…</parent>
<dependencyManagement>…</dependencyManagement>
<modules>…</modules>
<properties>…</properties>
<!– Build Settings –>
<build>…</build>
<reporting>…</reporting>
<!– More Project Information –>
<name>…</name>
<description>…</description>
<url>…</url>
<inceptionYear>…</inceptionYear>
<licenses>…</licenses>
<organization>…</organization>
<developers>…</developers>
<contributors>…</contributors>
<!– Environment Settings –>
<issueManagement>…</issueManagement>
<ciManagement>…</ciManagement>
<mailingLists>…</mailingLists>
<scm>…</scm>
<prerequisites>…</prerequisites>
<repositories>…</repositories>
<pluginRepositories>…</pluginRepositories>
<distributionManagement>…</distributionManagement>
<profiles>…</profiles>
</project>
[/sourcecode]
Big projects may have multiple poms, which can be linked – for example –
[sourcecode language=”xml”] <parent>This produces an ‘effective-pom’ which can be seen –
mvn help:effective-pom
dependencies
dependencies in maven allow you to link your project to the correct versions of jar files required in your build.
For example if you need the servlet and jsp-api’s in your project you would add these dependencies –
[sourcecode language=”xml”] <dependency>To import the associated jars you run –
mvn install
Maven then has a search path –
You can add extra repositories through –
[sourcecode language=”xml”]You can also go offline with maven –
mvn dependency:go-offline
The bigger the pom means that you can have dependency conflicts.
These can be resolved thru –
mvn dependency:tree
mvn dependency:resolve
mvn dependency:resolve-plugins
mvn dependency:analyze – unused/undeclared dependencies
plug-ins
Useful plugins –
Execution Groups
Lifecycle – well recognised flow steps – mvn deploy
Phase – phase within a lifecycle
Plugin – Logical Grouping and distribution
Goal – Lowest unit – eg – mvn compile:compile jar:jar
Built-in Maven Lifecycles
mvn clean –
pre-clean
clean – Remove all generated/compiled attributes prior to build
post-clean
default –
validate – Cross check all elements necessary for build are correct and present
compile – Primary or mixed language resources
test – Execute unit tests
package – Bundle module as JAR, WAR or EAR
verify – Inspect distribution packages
install – Install to local repository
deploy – Upload to remote repository
site –
pre-site – Similar to validate
site – Generate site
site-deploy – deploy to remote site
mvn install -Dmaven.test.skip=true – skip tests
Eclipse –
mvn eclipse:eclipse
Default Goal
[sourcecode language=”xml”] <project> Profiles
Profiles allow you to create environment specific configurations – eg a test server vs a live server
mvn <yourgoal> -P YourProfile
mvn -Prelease-profile clean assembly:assembly
mvn -Pdemo clean assembly:assembly
References
http://www.mkyong.com/maven/how-to-create-a-web-application-project-with-maven/
http://books.sonatype.com/mvnex-book
http://books.sonatype.com/mvnex-book/reference/appendix-license.html