Maven is a build automation tool used primarily for Java projects. This presentation will cover the basics of Maven and its usage while developing Java application.This is for anyone interested to learn Maven especially the Java developers.
2. • What is Maven
• Structure
• Dependencies
• Maven Repositories
• Plugins
Agenda
3. • A build tool
Produces one artifact(jar, war, zip)
Manage Dependencies
• Can be used a project management tool
Can handle versioning and releases
Can produce Javadocs
What is Maven
5. • Looks for src/main/java
• compiles it into target folder
• pom.xml
• Unit Testing src/test/java
• target
Compiled output get stored
Tests get ran from
Its contents gets packaged into jar/war.
Folder Structure
6. • Can be divided into 4 basic
parts
1. Project Info
2. Dependencies
3. Build
Plugins
Directory Structure
4. Repositories
pom.xml
7. 1.Project Info
pom.xml
Packaging types can be
jar, war, pom, maven-plugin, ejb, ear, rar, par
Default is jar
<!-- More Project Information -->
<url>...</url>
<licenses>
...
</licenses>
<organization>
...
</organization>
<developers>
...
</developers>
<contributors>
...
</contributors>
8. 1. Dependencies
• The external Libraries/Resources which we want to use in our
application
• Are imported via pom.xml by their naming conventions
groupId (Same as our package structure,
com.mucompany.myproduct)
artifactId (name of the artifact )
version
• We list dependencies that we want to use in our application, and
maven pulls all the transitive dependencies automatically.
• Versions
Release numbers of the artefacts that we want to use
SNAPSHOT: every time we compile it checks for if there is any
new version to pull down and use.
Example: myapp-1.0-SNAPSHOT.jar
Doesn’t have any specific naming convention
appname-1.0.jar
pom.xml
9. 1. Dependencies
groupId
artifactId (name of the artifact )
version
Type(Corresponds to the dependant artifact's packaging
type)
jar as default
Others can be
• pom
• maven-plugin
• ejb
• war
• ear
• rar
• par
Scope (Availability/Visibility of the dependency)
pom.xml
11. 1. Dependencies
pom.xml
• Scope
• 6 types of scopes available for dependencies
1. compile - default scope, artifacts available everywhere
2. provided - available in all phases but will not be
included in the final artifact, means it will be provided
where it is deployed, like servlet-api.jar
3. runtime - not needed for compilation, but needed for
execution.
4. test - only available in test compilation and execution
phase
5. system - same as provided, but need to specify the
path tho the jar on the file system
6. import - deals with dependencyManagement section
12. 1. Dependencies
• Transitive Dependencies
One of the main reason to use maven
If we add a dependency it automatically downloads its
transitive dependencies.
Also it resolves conflicts it resolves by always choosing
the newer one.
pom.xml
14. • Local Repository
Maven stores every thing it downloads
Users<username>.m2repository
Users<username>.m2groupIdartifactIdversionartifactName.jar
Helps to avoid duplication
• Central Repository
• htttp://repo.maven.apache.org/maven2
• It has been created by the apache maven community.
• Can be viewed at http://search.maven.org/#browse
• Remote Repository
• This is also located in the web, but other then central repository.
• This can be defined in the pom.xml file.
Maven searches for the dependencies in the following order:
Maven Repo
15. • Plugins are use to build and package our
application
Goals
Phases
Compiler Plugin
Jar Plugin
Maven Plugins
16. • clean
Deletes the target directory and any generated resources
• compile
Compiles the source code
• package
Runs compile, Runs the tests, generates the package
• install
Runs the package command and install it in local repo.
• deploy
Runs the install command and deploys it to the corporate
repo
Maven Goals
17. • 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.
• 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.
A Build Lifecycle/Phases
18. • Used to compile the main code and test code.
Compiler Plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<meminitial>128m</meminitial>
<maxmem>512m</maxmem>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
19. • Used to package code into jar.
• Tied to the package phase
Jar Plugin
Javadoc Plugin
• Used to attach Javadocs to a jar
• Tied to the package phase
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>attach-javadics</id>
<phase>verify</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<useDefaultManifestFile>true</useDefaultManifestFile>
</configuration>
</plugin>