Title: The Freedom to Achieve
1The Freedom to Achieve
- Java development with Maven 2
- More than just a build tool
2Mavens Origins
- Apache Software Foundation project
- Born in an attempt to make several projects at
- the ASF work in the same way
-
- ASF developers worked on several ASF projects
- and hated learning the build details for each
- project
3What exactly is Maven ?
- A Project Management Framework whose goal is
to provide a comprehensive approach to managing
software projects. - An attempt to apply patterns to a project's
build infrastructure in order to promote
comprehension and productivity by providing a
clear path in the use of best practices
4What exactly is Maven? II
- Maven is essentially a project management and
comprehension tool and as such provides a way to
help with managing - Builds
- Testing
- Continuous Integration
- Documentation
- Reporting
- Dependencies
- SCMs
- Team Collaboration
- Distribution
5How does Maven make my life easier ?
- Employs standard conventions and practices to
accelerate your development cycle. - Build reuse (pom.xml inheritance)
- Dependency management
- Easy project comprehension
- Project generation
- IDE Integration
- Project site generation
6Maven fundamentals
- Single project produces a single artifact
(output) - Repositories for access storage of 3rd party
and internally produced artifacts - Remote Repositories
- Public Maven repositories
- Company owned/managed repositories
- Local Repository
- C\Documents and Settings\mchacon\.m2\repository
- Default path but can be configured via
settings.xml - Repositories are broken down into groups,
artifacts - and versions
7Maven Fundamentals II
- Build lifecycle made up of phases and each phase
is broken down into goals - Each phase is handled by a Maven plugin
- pom.xml is declarative and describes all aspects
of your project dependencies, build, site
generation etc
8Typical Maven build phases
- validate - validate the project is correct and
all necessary information is available - compile - compile the source code of the project
- test - test the compiled source code using a
suitable unit testing framework. These tests
should not require the code be packaged or
deployed - package - take the compiled code and package it
in its distributable format, such as a JAR. - integration-test - process and deploy the package
if necessary into an environment where
integration tests can be run - verify - run any checks to verify the package is
valid and meets quality criteria - install - install the package into the local
repository, for use as a dependency in other
projects locally - deploy - done in an integration or release
environment, copies the final package to the
remote repository for sharing with other
developers and projects.
9Maven Architecture
- Maven is Eclipse-like in that it is very
pluggable. - Maven is the engine that will execute its
various plugins during the build lifecycle - When you configure/customize a projects build
you are actually just configuring a Maven plugin - All this is done in your pom.xml file
10Maven 2 Resources
- Above average documentation and tutorials on
Maven home page - http//maven.apache.org/
- http//maven.apache.org/guides/getting-started/mav
en-in-five-minutes.html - Active forums for Maven and related projects
- http//www.nabble.com/Maven-f177.html
- Free E-Book from Mergere Consulting
- http//www.mergere.com/m2book_download.jsp
11Maven 2 Adoption
- Currently several open source projects are built
and managed using Maven 2. - Jetty 6
- Struts
- Tapestry
- MyFaces
- Geronimo
- ActiveMQ
- Celtix (Open Source ESB)
-
12Getting Started With Maven 2
- Download Maven 2.0.6 and extract zip file to the
directory of your choice - Set up M2_HOME environment variable
- Add M2_HOME\bin to your PATH
- Make sure you have a JAVA_HOME that points
- to a JDK
- Run a basic Maven test
- Open a command line and type this
- command
- mvn --version
13Maven installation test
- The mvn command should spit out the version of
Maven you are running.
14Mavenize Eclipse
- Use the standard Eclipse Software Update feature
to install the Codehaus Maven 2 Eclipse plugin - Help ? Software Updates ? Find and Install
- Use the New Remote Site option and use the
following URL to download the plugin - http//m2eclipse.codehaus.org/update/
15Building a Jar project Importing into Eclipse
- Step 1 Use Maven 2 archetype to create project
via command line. - mvn archetypecreate -DarchetypeGroupIdorg.apache
.maven.archetypes -DarchetypeArtifactIdmaven-arch
etype-quickstart -DgroupIdcom.platinum.maven.demo
-DartifactIddemo-maven2-jar -DpackageNamecom.pl
atinum.maven2.app - Step 2 Use Maven 2 Eclipse plugin to generate
the .project and .classpath files that Eclipse
needs to recognize the java project - Step 2-A Change directory into the top level of
the newly created project - Make sure you are in the directory where the
pom.xml file is located - Step 2-B Execute the Maven 2 Eclipse plugin
- mvn eclipseeclipse
- Step 3 Use Eclipse to import the newly created
project - File ? Import ? Existing Projects Into Workspace
16Building a War project Importing into Eclipse
- Step 1 Use Maven 2 archetype to create project
via command line. - mvn archetypecreate -DarchetypeGroupIdorg.apache
.maven.archetypes -DarchetypeArtifactIdmaven-arch
etype-webapp -DgroupIdcom.platinum.maven.demo
-DartifactIddemo-maven2-war - Step 2 Use Maven 2 Eclipse plugin to generate
the .project and .classpath files that Eclipse
needs to recognize the java project - Step 2-A Change directory into the top level of
the newly created project - Make sure you are in the directory where the
pom.xml file is located - Step 2-B Execute the Maven 2 Eclipse plugin
- mvn eclipseeclipse
- Step 3 Use Eclipse to import the newly created
project - File ? Import ? Existing Projects Into Workspace
17Examining the pom.xml
lt?xml version"1.0" encoding"UTF-8"?gt ltproject
xmlns"http//maven.apache.org/POM/4.0.0"
xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
ce" xsischemaLocation"http//maven.apache.org/PO
M/4.0.0 http//maven.apache.org/maven-v4_0_0.xsd"gt
ltmodelVersiongt4.0.0lt/modelVersiongt ltgroupIdgtcom.p
latinumsolutions.maven.demolt/groupIdgt ltartifactIdgt
demo-jarlt/artifactIdgt ltpackaginggtjarlt/packaginggt lt
namegtdemo-jarlt/namegt ltversiongt1.0-SNAPSHOTlt/versio
ngt ltdescriptiongtMaven Jar Project
Demolt/descriptiongt lturlgthttp//www.platinumsolutio
ns.comlt/urlgt ltdevelopersgt ltdevelopergt ltidgtarossilt/
idgt ltnamegtAdam Rossilt/namegt ltemailgtarossi_at_platinum
solutions.comlt/emailgt ltorganizationgtPlatinum
Solutionslt/organizationgt ltorganizationUrlgthttp//w
ww.platinumsolutions.com/lt/organizationUrlgt ltroles
gt ltrolegtarchitectlt/rolegt ltrolegtdeveloperlt/rolegt lt/
rolesgt lt/developergt lt/developersgt
- Basic project information
18Examining the pom.xml II
ltdependenciesgt ltdependencygt ltgroupIdgtjunitlt/groupI
dgt ltartifactIdgtjunitlt/artifactIdgt ltversiongt3.8.1lt/
versiongt ltscopegttestlt/scopegt lt/dependencygt ltdepend
encygt ltgroupIdgtcommons-langlt/groupIdgt ltartifactIdgt
commons-langlt/artifactIdgt ltversiongt2.2lt/versiongt lt
scopegtcompilelt/scopegt lt/dependencygt ltdependencygt lt
groupIdgtorg.springframeworklt/groupIdgt ltartifactIdgt
spring-corelt/artifactIdgt ltversiongt2.0.2lt/versiongt
ltscopegtcompilelt/scopegt lt/dependencygt lt/dependencie
sgt
- Example dependencies
- Dependency scopes
- compile - this is the default scope, used if none
is specified. Compile dependencies are available
in all classpaths. - provided - this is much like compile, but
indicates you expect the JDK or a container to
provide it. It is only available on the
compilation classpath, and is not transitive. - runtime - this scope indicates that the
dependency is not required for compilation, but
is for execution. It is in the runtime and test
classpaths, but not the compile classpath. - test - this scope indicates that the dependency
is not required for normal use of the
application, and is only available for the test
compilation and execution phases. - system - this scope is similar to provided except
that you have to provide the JAR which contains
it explicitly. The artifact is always available
and is not looked up in a repository.
19Examining the pom.xml III
ltbuildgt lt!-- Build configuration not necessary if
using standard Maven project layout
--gt ltsourceDirectorygtbasedir/src/main/javalt/sou
rceDirectorygt ltscriptSourceDirectorygtbasedir/sr
c/main/scriptslt/scriptSourceDirectorygt lttestSource
Directorygtbasedir/src/test/javalt/testSourceDire
ctorygt ltoutputDirectorygtbasedir/target/classeslt
/outputDirectorygt lttestOutputDirectorygtbasedir/
target/test-classeslt/testOutputDirectorygt ltplugins
gt ltplugingt ltartifactIdgtmaven-compiler-pluginlt/arti
factIdgt ltconfigurationgt ltsourcegt1.5lt/sourcegt lttarg
etgt1.5lt/targetgt ltexcludesgt ltexcludegt/Point.jav
alt/excludegt lt/excludesgt lt/configurationgt lt/plugingt
lt/pluginsgt lt/buildgt
- Build/plugin configuration
- Configure compiler plugin
20Examining the pom.xml IV
. ltplugingt ltartifactIdgtmaven-jar-pluginlt/artifac
tIdgt ltconfigurationgt ltarchivegt ltmanifestgt ltmainCla
ssgtpom.groupId.Applt/mainClassgt ltpackageNamegtp
om.groupIdlt/packageNamegt lt/manifestgt ltmanifestEnt
riesgt ltmodegtdevelopmentlt/modegt lturlgtpom.urllt/ur
lgt lt/manifestEntriesgt lt/archivegt lt/configurationgt
lt/plugingt .
- Build/plugin configuration
- Configure jar plugin
21Examining the pom.xml V
ltbuildgt ltsourceDirectorygtbasedir/src/main/javalt
/sourceDirectorygt ltfinalNamegtdemo-war-pom.versio
nlt/finalNamegt ltpluginsgt ltplugingt ltgroupIdgtorg.apa
che.maven.pluginslt/groupIdgt ltartifactIdgtmaven-war-
pluginlt/artifactIdgt ltversiongt2.0.2lt/versiongt lt!--
These configurations are not necessary because we
are using the default project layout that Maven
2 prescribes. They are shown as examples on how
to configure the Maven 2 war plugin to build a
project that is not in the standard directory
layout. --gt ltconfigurationgt ltwarSourceDirectorygt
basedir/src/main/webapplt/warSourceDirectorygt ltweb
Xmlgtbasedir/src/main/webapp/WEB-INF/web.xmllt/we
bXmlgt lt/configurationgt lt/plugingt lt/pluginsgt lt/buil
dgt
- Build/plugin configuration
- Configure war plugin
22Eclipses Maven Plugin
23Setting up Maven as Eclipse External Tool
24Maven Site Generation
- To generate project site just run the command
- mvn site
- Sample Maven projects are on the P Drive
- P\Maven 2
25Example Maven Site