SlideShare ist ein Scribd-Unternehmen logo
1 von 35
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Automated build management.
Maven
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Manual build management (life before maven)
● Download the dependencies to compile the source code.
● Run the javac command to compile the source code.
● Run the java commands to test the source code.
● Run the jar command to package the source code.
● Run the javadoc command to create the documentation.
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Hey, but..
Anything, which is not the
part of business logic,
must be automated!
That’s correct! Maven is at our rescue.
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
What is Maven?
● At its simplest, build automation tool
○ Produces deployable artifact (jar, war etc) from source code.
○ Helps us manage the dependencies.
○ Has built in build lifecycle.
● Can also be used as a project management tool
○ Can produce the project documentation
○ Supports plugins to provide various report.
○ Supports versioning and releasing the project.
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Maven in action!
Lets create a simple maven based project.
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Creating a simple java application
● mvn archetype:generate
○ Creates an application in interactive mode; will prompt for
■ archetype - default 414 (maven-archetype-quickstart)
■ archetype version - default 1.1
■ groupId - com.hashedin.hu
■ artifactId - HelloWorldMaven
■ version - default 1.0-SNAPSHOT
■ package - default groupId
● mvn archetype:generate -DgroupId=com.hashedin.hu -
DartifactId=HelloWorldMaven -DinteractiveMode=false -
DarchetypeArtifactId=maven-archetype-quickstart
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Directory Structure
● src/main/java
○ The source code directory.
○ com.hashedin.hu package
created automatically by
archetype.
● src/test/java
○ The test directory.
○ The test cases are also placed
inside the same default
package.
● pom.xml
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Contents of pom.xml
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Goals - Automated build commands
● mvn compile
Compiles the source code, generates any files, copies resources to our
target directory.
● mvn clean
Deletes the target directory and any generated resources.
● mvn package
Runs the compile command first, run test cases and packages the app
based on its packaging type.
● mvn install
Runs the package command then installs it in local repo.
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
The ‘target’ directory
● Automatically created by maven.
● This is where everything gets compiled to.
● This is also the place where test cases run from.
● Contents in this directory gets packaged into jar, war
etc.
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
The pom.xml
● Holds the meta information about the project.
● Project Information
○ groupId
○ artifactId
○ version
○ packaging
● Dependencies - Direct dependencies used in our application.
● Build - Plugins, Directory Structure etc.
● Repositories - Holds all the dependencies and artifacts.
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Creating a web application
● src/main/webapp
○ The base directory containing
all the assets like JSP, Images
etc.
● src/webapp/WEB-INF
○ web.xml - Deployment Desc.
● src/main/java
○ Standard java classes: Servlets,
POJOs etc.
● pom.xml
mvn archetype:generate -DgroupId=com.hashedin
-DartifactId=hu-webapp
-DarchetypeArtifactId=maven-archetype-webapp
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Dependencies
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Dependencies
● Resources that our project relies upon to function.
● To include, just list the dependency in pom.xml
○ Transitive dependencies will be pulled in by maven.
● Dependencies are identified by their coordinates
○ groupId
○ artifactId
○ version
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version>
</dependency>
Example:
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Transitive Dependencies
● Our project dependencies’s dependencies.
○ Eg: Hibernate has dependency on 8 other resources:
■ hibernate-core
■ hibernate-commons-annotations
■ jboss-logging etc
● All the transitive dependencies are automatically
downloaded and added by maven.
● The main reason people start off with maven.
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
● Not all resources are needed at all times!
● There are 6 available scopes:
○ compile - default scope, artifacts available everywhere.
○ provided - like compile, but not packaged into an artifact. It will be
available where artifact is deployed. (servlets-api)
○ runtime - needed only for execution not for compiling.
○ test - available for test-compilation and execution phase.
○ system - provided, you specify the path to the jar on file system.
○ import - deals with dependencyManagement and multi modules.
● Lets add few dependencies to our simple application.
Dependencies Scope
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Repositories
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Local Repository
● Where maven caches everything it downloads
○ /home/<username>/.m2/repository.
● When maven needs to resolve a dependency, it first looks into the local
repository and then goes about downloading it.
● Stores the artifacts using the information we provide for artifactId, groupId
and version.
○ ~/.m2/repository/com.hashedin/HelloWorldMaven/1.0-SNAPHOST/HelloWorldMaven-1.0-
SNAPSHOT.jar
● No need to copy the artifact into every project and store it in the SCM (git
etc)
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Remote Repository
● Just a network accessible location that maven downloads
dependencies from.
● Default location: http://repo.maven.apache.org/maven2/
● We can add multiple repositories.
● All the artifacts that remote repository contains are open
source. Dont add any private artifact of the organization.
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Adding a new remote repository
We can add a remote repository to search for dependencies if
these dependencies aren’t available on default location.
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Build lifecycle and plugins
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Build Lifecycle
● Software projects undertake various steps before they are actually
distributed. (eg. compile, test, package etc).
● Build lifecycle defines the process of building and distributing the
artifacts.
● There are three built‐in build lifecycles:
○ Default/Build - Handles project building and deployment.
○ Clean - Handles project cleaning
○ Site - Handles project’s site generation.
● Each build lifecycle is made up of phases.
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Build / Default lifecycle
● You can invoke a specific phase on the lifecycle
○ Example: mvn test
● When you invoke a specific phase, every previous phase
run, including the one you specified. If we run, mvn test
Refer: All lifecycle phases
Validate DeployCompile
Tes
t
Package Install
Validate Compile
Tes
t
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Clean and Site lifecycle phases
● Clean
○ Handles project cleaning.
○ Deletes target directory and any generated files.
● Site
○ Generates project documentation.
○ Used to generate various kind of reports.
Pre - Clean Clean Post - Clean
Pre - Site Site Post - Site Site - Deploy
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Maven Architecture
Maven - Core Engine
Remote
Repositories
Plugin 1 Plugin 2 Plugin 3 Plugin N
Remote
RepositoriesRemote
Repositories
Local
Repository
Fig: Maven’s plugin based architecture.
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Maven Architecture - Core Engine
● Project processing
○ Read the project configuration file and configure the
project accordingly.
● Build lifecycle management
○ Run a series of phases (as seen before).
● Framework for plug‐ins
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Maven Architecture - Plugins
● Provides the core operations to build your project.
○ For example, to create a jar the maven jar plugin will do the job, not
maven itself.
● Plug‐ins provides one or more “Goals”
● A Goal perform some operation on the project.
○ Ex: compile, create a Jar, deploy to Jboss, etc.
● Goals can be bound to build lifecycle phases
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Maven lifecycle, plugins, and goals
● Goals can be bound to build‐lifecycle phases
○ Eg: $ mvn test
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Maven lifecycle, plugins, and goals
We can bind a plugin’s goal to a lifecycle phase
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Maven lifecycle, plugins, and goals
● Putting lifecycle phases plug‐ins and goals together
○ A lifecycle is a series of phases
○ A phase is made of goals
○ Goals are provided by plug ins
● Each phase have default bindings
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Jetty
The web interface for Maven.
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Jetty - The Web Container
We can use the jetty plugin to run our web application
For jetty to function without any configuration, keep:
● resources in ${basedir}/src/main/webapp
● classes in ${project.build.outputDirectory}
● web.xml in ${basedir}/src/main/webapp/WEB-INF/
Plugin configuration:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-
plugin</artifactId></plugin>
Usage:
Start: $mvn jetty:run
Stop: <ctrl-c>, $mvn jetty:stop
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Summary - Benefits of maven
● Declarative way of defining the dependency.
● Transitive dependency management.
● Keeps the code organized.
● Various plugins available for almost any task we would need
to undertake.
● Reduces build size by eliminating the need to check in jar
files into source code repository.
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
References
Apache Maven Guides
Maven by example
Apache Maven tutorial
Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
That’s It!
Questions, Suggestions Or Feedback?

Weitere Àhnliche Inhalte

Was ist angesagt?

Gradle Introduction
Gradle IntroductionGradle Introduction
Gradle Introduction
Dmitry Buzdin
 

Was ist angesagt? (20)

Build Automation using Maven
Build Automation using Maven Build Automation using Maven
Build Automation using Maven
 
Maven ppt
Maven pptMaven ppt
Maven ppt
 
An introduction to Maven
An introduction to MavenAn introduction to Maven
An introduction to Maven
 
Introduction to Apache Maven
Introduction to Apache MavenIntroduction to Apache Maven
Introduction to Apache Maven
 
Maven
MavenMaven
Maven
 
Continuous delivery-with-maven
Continuous delivery-with-mavenContinuous delivery-with-maven
Continuous delivery-with-maven
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Apache maven 2 overview
Apache maven 2 overviewApache maven 2 overview
Apache maven 2 overview
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
 
Xke spring boot
Xke spring bootXke spring boot
Xke spring boot
 
Maven
MavenMaven
Maven
 
Maven
MavenMaven
Maven
 
Maven
MavenMaven
Maven
 
Gradle Introduction
Gradle IntroductionGradle Introduction
Gradle Introduction
 
Jenkins
JenkinsJenkins
Jenkins
 
Introduction to gradle
Introduction to gradleIntroduction to gradle
Introduction to gradle
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
 
Spring boot
Spring bootSpring boot
Spring boot
 
Java spring ppt
Java spring pptJava spring ppt
Java spring ppt
 

Ähnlich wie Apache Maven

Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
mfrancis
 
Gradle and Your Android Wearable Projects
Gradle and Your Android Wearable ProjectsGradle and Your Android Wearable Projects
Gradle and Your Android Wearable Projects
CommonsWare
 

Ähnlich wie Apache Maven (20)

An Introduction to Maven Part 1
An Introduction to Maven Part 1An Introduction to Maven Part 1
An Introduction to Maven Part 1
 
Extending Build to the Client: A Maven User's Guide to Grunt.js
Extending Build to the Client: A Maven User's Guide to Grunt.jsExtending Build to the Client: A Maven User's Guide to Grunt.js
Extending Build to the Client: A Maven User's Guide to Grunt.js
 
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
 
Continuous Integration Fundamentals: Maven - OFM Canberra July 2014
Continuous Integration Fundamentals: Maven - OFM Canberra July 2014Continuous Integration Fundamentals: Maven - OFM Canberra July 2014
Continuous Integration Fundamentals: Maven - OFM Canberra July 2014
 
BMO - Intelligent Projects with Maven
BMO - Intelligent Projects with MavenBMO - Intelligent Projects with Maven
BMO - Intelligent Projects with Maven
 
A-Z_Maven.pdf
A-Z_Maven.pdfA-Z_Maven.pdf
A-Z_Maven.pdf
 
Intelligent Projects with Maven - DevFest Istanbul
Intelligent Projects with Maven - DevFest IstanbulIntelligent Projects with Maven - DevFest Istanbul
Intelligent Projects with Maven - DevFest Istanbul
 
Apigee deploy grunt plugin.1.0
Apigee deploy grunt plugin.1.0Apigee deploy grunt plugin.1.0
Apigee deploy grunt plugin.1.0
 
Pipeline as code for your infrastructure as Code
Pipeline as code for your infrastructure as CodePipeline as code for your infrastructure as Code
Pipeline as code for your infrastructure as Code
 
Gradle and Your Android Wearable Projects
Gradle and Your Android Wearable ProjectsGradle and Your Android Wearable Projects
Gradle and Your Android Wearable Projects
 
Jenkins advance topic
Jenkins advance topicJenkins advance topic
Jenkins advance topic
 
Wonderful World of Maven
Wonderful World of MavenWonderful World of Maven
Wonderful World of Maven
 
Alpes Jug (29th March, 2010) - Apache Maven
Alpes Jug (29th March, 2010) - Apache MavenAlpes Jug (29th March, 2010) - Apache Maven
Alpes Jug (29th March, 2010) - Apache Maven
 
Lausanne Jug (08th April, 2010) - Maven
Lausanne Jug (08th April, 2010) - MavenLausanne Jug (08th April, 2010) - Maven
Lausanne Jug (08th April, 2010) - Maven
 
Prizm Installation Guide
Prizm Installation GuidePrizm Installation Guide
Prizm Installation Guide
 
Mavennotes.pdf
Mavennotes.pdfMavennotes.pdf
Mavennotes.pdf
 
Devops
DevopsDevops
Devops
 
Riviera JUG (20th April, 2010) - Maven
Riviera JUG (20th April, 2010) - MavenRiviera JUG (20th April, 2010) - Maven
Riviera JUG (20th April, 2010) - Maven
 
Build server
Build serverBuild server
Build server
 
Devoops: DoJ Annual Cybersecurity Training Symposium Edition 2015
Devoops: DoJ Annual Cybersecurity Training Symposium Edition 2015Devoops: DoJ Annual Cybersecurity Training Symposium Edition 2015
Devoops: DoJ Annual Cybersecurity Training Symposium Edition 2015
 

KĂŒrzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

KĂŒrzlich hochgeladen (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 

Apache Maven

  • 1. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Automated build management. Maven
  • 2. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Manual build management (life before maven) ● Download the dependencies to compile the source code. ● Run the javac command to compile the source code. ● Run the java commands to test the source code. ● Run the jar command to package the source code. ● Run the javadoc command to create the documentation.
  • 3. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Hey, but.. Anything, which is not the part of business logic, must be automated! That’s correct! Maven is at our rescue.
  • 4. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL What is Maven? ● At its simplest, build automation tool ○ Produces deployable artifact (jar, war etc) from source code. ○ Helps us manage the dependencies. ○ Has built in build lifecycle. ● Can also be used as a project management tool ○ Can produce the project documentation ○ Supports plugins to provide various report. ○ Supports versioning and releasing the project.
  • 5. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Maven in action! Lets create a simple maven based project.
  • 6. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Creating a simple java application ● mvn archetype:generate ○ Creates an application in interactive mode; will prompt for ■ archetype - default 414 (maven-archetype-quickstart) ■ archetype version - default 1.1 ■ groupId - com.hashedin.hu ■ artifactId - HelloWorldMaven ■ version - default 1.0-SNAPSHOT ■ package - default groupId ● mvn archetype:generate -DgroupId=com.hashedin.hu - DartifactId=HelloWorldMaven -DinteractiveMode=false - DarchetypeArtifactId=maven-archetype-quickstart
  • 7. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Directory Structure ● src/main/java ○ The source code directory. ○ com.hashedin.hu package created automatically by archetype. ● src/test/java ○ The test directory. ○ The test cases are also placed inside the same default package. ● pom.xml
  • 8. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Contents of pom.xml
  • 9. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Goals - Automated build commands ● mvn compile Compiles the source code, generates any files, copies resources to our target directory. ● mvn clean Deletes the target directory and any generated resources. ● mvn package Runs the compile command first, run test cases and packages the app based on its packaging type. ● mvn install Runs the package command then installs it in local repo.
  • 10. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL The ‘target’ directory ● Automatically created by maven. ● This is where everything gets compiled to. ● This is also the place where test cases run from. ● Contents in this directory gets packaged into jar, war etc.
  • 11. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL The pom.xml ● Holds the meta information about the project. ● Project Information ○ groupId ○ artifactId ○ version ○ packaging ● Dependencies - Direct dependencies used in our application. ● Build - Plugins, Directory Structure etc. ● Repositories - Holds all the dependencies and artifacts.
  • 12. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Creating a web application ● src/main/webapp ○ The base directory containing all the assets like JSP, Images etc. ● src/webapp/WEB-INF ○ web.xml - Deployment Desc. ● src/main/java ○ Standard java classes: Servlets, POJOs etc. ● pom.xml mvn archetype:generate -DgroupId=com.hashedin -DartifactId=hu-webapp -DarchetypeArtifactId=maven-archetype-webapp
  • 13. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Dependencies
  • 14. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Dependencies ● Resources that our project relies upon to function. ● To include, just list the dependency in pom.xml ○ Transitive dependencies will be pulled in by maven. ● Dependencies are identified by their coordinates ○ groupId ○ artifactId ○ version <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.5.Final</version> </dependency> Example:
  • 15. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Transitive Dependencies ● Our project dependencies’s dependencies. ○ Eg: Hibernate has dependency on 8 other resources: ■ hibernate-core ■ hibernate-commons-annotations ■ jboss-logging etc ● All the transitive dependencies are automatically downloaded and added by maven. ● The main reason people start off with maven.
  • 16. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL ● Not all resources are needed at all times! ● There are 6 available scopes: ○ compile - default scope, artifacts available everywhere. ○ provided - like compile, but not packaged into an artifact. It will be available where artifact is deployed. (servlets-api) ○ runtime - needed only for execution not for compiling. ○ test - available for test-compilation and execution phase. ○ system - provided, you specify the path to the jar on file system. ○ import - deals with dependencyManagement and multi modules. ● Lets add few dependencies to our simple application. Dependencies Scope
  • 17. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Repositories
  • 18. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Local Repository ● Where maven caches everything it downloads ○ /home/<username>/.m2/repository. ● When maven needs to resolve a dependency, it first looks into the local repository and then goes about downloading it. ● Stores the artifacts using the information we provide for artifactId, groupId and version. ○ ~/.m2/repository/com.hashedin/HelloWorldMaven/1.0-SNAPHOST/HelloWorldMaven-1.0- SNAPSHOT.jar ● No need to copy the artifact into every project and store it in the SCM (git etc)
  • 19. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Remote Repository ● Just a network accessible location that maven downloads dependencies from. ● Default location: http://repo.maven.apache.org/maven2/ ● We can add multiple repositories. ● All the artifacts that remote repository contains are open source. Dont add any private artifact of the organization.
  • 20. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Adding a new remote repository We can add a remote repository to search for dependencies if these dependencies aren’t available on default location.
  • 21. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Build lifecycle and plugins
  • 22. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Build Lifecycle ● Software projects undertake various steps before they are actually distributed. (eg. compile, test, package etc). ● Build lifecycle defines the process of building and distributing the artifacts. ● There are three built‐in build lifecycles: ○ Default/Build - Handles project building and deployment. ○ Clean - Handles project cleaning ○ Site - Handles project’s site generation. ● Each build lifecycle is made up of phases.
  • 23. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Build / Default lifecycle ● You can invoke a specific phase on the lifecycle ○ Example: mvn test ● When you invoke a specific phase, every previous phase run, including the one you specified. If we run, mvn test Refer: All lifecycle phases Validate DeployCompile Tes t Package Install Validate Compile Tes t
  • 24. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Clean and Site lifecycle phases ● Clean ○ Handles project cleaning. ○ Deletes target directory and any generated files. ● Site ○ Generates project documentation. ○ Used to generate various kind of reports. Pre - Clean Clean Post - Clean Pre - Site Site Post - Site Site - Deploy
  • 25. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Maven Architecture Maven - Core Engine Remote Repositories Plugin 1 Plugin 2 Plugin 3 Plugin N Remote RepositoriesRemote Repositories Local Repository Fig: Maven’s plugin based architecture.
  • 26. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Maven Architecture - Core Engine ● Project processing ○ Read the project configuration file and configure the project accordingly. ● Build lifecycle management ○ Run a series of phases (as seen before). ● Framework for plug‐ins
  • 27. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Maven Architecture - Plugins ● Provides the core operations to build your project. ○ For example, to create a jar the maven jar plugin will do the job, not maven itself. ● Plug‐ins provides one or more “Goals” ● A Goal perform some operation on the project. ○ Ex: compile, create a Jar, deploy to Jboss, etc. ● Goals can be bound to build lifecycle phases
  • 28. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Maven lifecycle, plugins, and goals ● Goals can be bound to build‐lifecycle phases ○ Eg: $ mvn test
  • 29. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Maven lifecycle, plugins, and goals We can bind a plugin’s goal to a lifecycle phase
  • 30. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Maven lifecycle, plugins, and goals ● Putting lifecycle phases plug‐ins and goals together ○ A lifecycle is a series of phases ○ A phase is made of goals ○ Goals are provided by plug ins ● Each phase have default bindings
  • 31. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Jetty The web interface for Maven.
  • 32. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Jetty - The Web Container We can use the jetty plugin to run our web application For jetty to function without any configuration, keep: ● resources in ${basedir}/src/main/webapp ● classes in ${project.build.outputDirectory} ● web.xml in ${basedir}/src/main/webapp/WEB-INF/ Plugin configuration: <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven- plugin</artifactId></plugin> Usage: Start: $mvn jetty:run Stop: <ctrl-c>, $mvn jetty:stop
  • 33. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL Summary - Benefits of maven ● Declarative way of defining the dependency. ● Transitive dependency management. ● Keeps the code organized. ● Various plugins available for almost any task we would need to undertake. ● Reduces build size by eliminating the need to check in jar files into source code repository.
  • 34. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL References Apache Maven Guides Maven by example Apache Maven tutorial
  • 35. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL That’s It! Questions, Suggestions Or Feedback?