SlideShare a Scribd company logo
1 of 41
Intro to Maven
http://github.com/ostewart/maven-training
Oliver
Stewart
Contact
Oliver Stewart
oliver@trailmagic.com
@sintactic
http://github.com/ostewart
Agenda
• Why Maven?
• The Maven Way
• Maven Basics
• Multi-Module Projects
• Web Projects
• Integration Testing
• Clean/Advanced Maven
What Is Maven?
• Build tool (focused on JVM)
• Convention over configuration
• Structured project model
• Build code reuse
Why Maven?
Shanty Town Builds
• Rarely get focused attention
• Haphazard areas of focus
• Each is unique
http://dornob.com/urban-shanty-town-photo-collages-of-sao-paolo/#axzz2aADkkp00
Regular Structure
• Approachable
• Machine-readable
• Codifies good practice
• Supports collective
ownership
Photo: Russ Allison Loar
http://bit.ly/14flwzd
Good Practices
• Separate code, tests, resources
• Platform independence
• Declarative dependency management
• Share resources via dependencies
• Predictable directory structure
• Test built artifacts
Why Not Maven?
• General automation
• Legacy code with intricate build incantations
• Might be able to bridge with AntRun or
custom plugins
The Maven Way
• Don't fight Maven. You'll lose.
• Maven is a box that turns source code into
build artifacts
• First, try to see how your problem fits into
Maven's worldview
• Next, you might want to write a plugin or use
AntRun
• If you're still struggling, you may want to
consider another tool
Maven Basics
Creating a New
Project
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.maventraining</groupId>
<artifactId>simple</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Running Maven
mvn <phase or goal> [<phase or goal>...]
Maven Lifecycles
• Clean
• Default
• Site
Default Lifecycle
(Abridged)
• Validate
• Compile
• Test
• Package
• Integration-test
• Verify
• Install
• Deploy
Dependency Management
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
Coordinates: groupId, artifactId, version, and
optional scope
Dependency Scopes
• compile (default)
• provided
• runtime
• test
• system
• import
Snapshot Versions
• Good for intra-project dependencies during
development
• Will check for updates each time you build
• Will break your build eventually outside a
single source tree
• x.x-SNAPSHOT
Release versions
• Generated by the release plugin
• Assumed immutable (cached aggressively)
Directory Layout
src/main/java Application/Library sources
src/main/resources Application/Library resources
src/main/webapp Web application sources
src/test/java Test sources
src/test/resources Test resources
target Build output
Super POM
• The POM from which all
other POMs inherit
• Look here for defaults
• http://maven.apache.org/ref/
3.0.4/maven-model-
builder/super-pom.html
Image source: examiner.com
Exercise
Build a simple library that outputs "Hello, World!". Write a
HelloWorld class that outputs a String and a test that
invokes it. Build and test your project with Maven.
More Basics
Plugins
Packaging
Plugins
• Maven's main unit of modularity
• How most of Maven's functionality is
implemented and configured
Plugin Configuration
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
Built-In Plugins
• maven-compiler-plugin
• maven-surefire-plugin
• maven-dependency-plugin
• maven-release-plugin
Common Plugins
• AntRun
• Cargo
• Jasmine
• Exec
• PMD
• Cobertura
• Findbugs
Packaging
• Defines the artifact type
• Binds goals to phases
• Core values: pom, jar, maven-plugin, ejb, war,
ear, rar, par
• Default is jar
Packaging Binds Goals to Phases
Phase Goal
process-resources resources:resources
compile compiler:compile
process-test-resources resources:testResources
test-compile compiler:testCompile
test surefire:test
package jar:jar
install install:install
deploy deploy:deploy
Exercise
Add isEmpty check on a String, set
source/target compiler versions to 1.6
Multi-Module Maven
• Why?
• How?
• Dependence vs. Inheritance
• Naming
Why Multiple
Modules?
• Group related functionality
• Isolate unrelated functionality
• Organize dependencies
• Unit for sharing code
• Unit of packaging
Multi-Modules: How
• Top-level pom packaging with modules
• Optional parent in sub-modules
Modules
<modules>
<module>hello-util</module>
<module>hello-service</module>
</modules>
Parent
<parent>
<groupId>com.example.maventraining</groupId>
<artifactId>hello</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
Dependence vs.
Inheritance
• Use dependencies to share code, resources
• Use inheritance to aggregate common
settings
Naming
• "There are only two hard things in Computer
Science: naming, cache invalidation, and off-
by-one errors." - almost Phil Karlton
• All modules are top-level, referenced by
groupId-artifactId
• http://maven.apache.org/guides/mini/guide-
naming-conventions.html
Exercise: Multi-Module
Now that we have a Hello utility, we'd like to use
it to greet people on our roster of users. For
each name on our roster, output "Hello,
<name>!". While the Hello utility might be
generally useful, this greeting service is
specialized functionality, so it should go in its
own module.
Bonus
• Web modules
• Integration testing with Cargo
• Dependency management
• Properties
• Profiles
Web Modules
• war packaging
• Static resources in src/main/webapp
• web.xml in src/main/webapp/WEB-INF
Exercise: Hello Web
We’d like to expose our HelloWorld utility as a
web service. Write a simple servlet that extracts
the name from the URL and serves the hello
response as plain text.

More Related Content

Similar to Intro to Maven.ppt

Lorraine JUG (1st June, 2010) - Maven
Lorraine JUG (1st June, 2010) - MavenLorraine JUG (1st June, 2010) - Maven
Lorraine JUG (1st June, 2010) - MavenArnaud Héritier
 
Introduction in Apache Maven2
Introduction in Apache Maven2Introduction in Apache Maven2
Introduction in Apache Maven2Heiko Scherrer
 
Developing Liferay Plugins with Maven
Developing Liferay Plugins with MavenDeveloping Liferay Plugins with Maven
Developing Liferay Plugins with MavenMika Koivisto
 
Apache Maven - eXo TN presentation
Apache Maven - eXo TN presentationApache Maven - eXo TN presentation
Apache Maven - eXo TN presentationArnaud Héritier
 
Introduction to Maven for beginners and DevOps
Introduction to Maven for beginners and DevOpsIntroduction to Maven for beginners and DevOps
Introduction to Maven for beginners and DevOpsSISTechnologies
 
Working in Harmony: Manchester - Optimize development and content workflows
Working in Harmony: Manchester - Optimize development and content workflowsWorking in Harmony: Manchester - Optimize development and content workflows
Working in Harmony: Manchester - Optimize development and content workflowsEdmund Turbin
 
Intelligent Projects with Maven - DevFest Istanbul
Intelligent Projects with Maven - DevFest IstanbulIntelligent Projects with Maven - DevFest Istanbul
Intelligent Projects with Maven - DevFest IstanbulMert Çalışkan
 
An Introduction to Maven
An Introduction to MavenAn Introduction to Maven
An Introduction to MavenVadym Lotar
 
Juggling Java EE with Enterprise Apache Maven
Juggling Java EE with Enterprise Apache MavenJuggling Java EE with Enterprise Apache Maven
Juggling Java EE with Enterprise Apache Mavenelliando dias
 
Embrace Maven
Embrace MavenEmbrace Maven
Embrace MavenGuy Marom
 

Similar to Intro to Maven.ppt (20)

Lorraine JUG (1st June, 2010) - Maven
Lorraine JUG (1st June, 2010) - MavenLorraine JUG (1st June, 2010) - Maven
Lorraine JUG (1st June, 2010) - Maven
 
Introduction in Apache Maven2
Introduction in Apache Maven2Introduction in Apache Maven2
Introduction in Apache Maven2
 
Developing Liferay Plugins with Maven
Developing Liferay Plugins with MavenDeveloping Liferay Plugins with Maven
Developing Liferay Plugins with Maven
 
Maven
MavenMaven
Maven
 
Apache Maven - eXo TN presentation
Apache Maven - eXo TN presentationApache Maven - eXo TN presentation
Apache Maven - eXo TN presentation
 
Introduction to Maven for beginners and DevOps
Introduction to Maven for beginners and DevOpsIntroduction to Maven for beginners and DevOps
Introduction to Maven for beginners and DevOps
 
Working in harmony
Working in harmonyWorking in harmony
Working in harmony
 
Working in Harmony: Manchester - Optimize development and content workflows
Working in Harmony: Manchester - Optimize development and content workflowsWorking in Harmony: Manchester - Optimize development and content workflows
Working in Harmony: Manchester - Optimize development and content workflows
 
Apache Maven for AT/QC
Apache Maven for AT/QCApache Maven for AT/QC
Apache Maven for AT/QC
 
Session 2
Session 2Session 2
Session 2
 
Session 2
Session 2Session 2
Session 2
 
Maven
MavenMaven
Maven
 
Intelligent Projects with Maven - DevFest Istanbul
Intelligent Projects with Maven - DevFest IstanbulIntelligent Projects with Maven - DevFest Istanbul
Intelligent Projects with Maven - DevFest Istanbul
 
An Introduction to Maven
An Introduction to MavenAn Introduction to Maven
An Introduction to Maven
 
Maven part 1
Maven part 1Maven part 1
Maven part 1
 
Maven part 1
Maven part 1Maven part 1
Maven part 1
 
Maven
Maven Maven
Maven
 
Maven part 1
Maven part 1Maven part 1
Maven part 1
 
Juggling Java EE with Enterprise Apache Maven
Juggling Java EE with Enterprise Apache MavenJuggling Java EE with Enterprise Apache Maven
Juggling Java EE with Enterprise Apache Maven
 
Embrace Maven
Embrace MavenEmbrace Maven
Embrace Maven
 

Recently uploaded

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 

Intro to Maven.ppt