22
Jyotirmaya Dehury Mindfire Solutions MAVEN Fundamentals

Introduction to Maven

Embed Size (px)

Citation preview

Jyotirmaya Dehury

Mindfire Solutions

MAVEN Fundamentals

• What is Maven

• Structure

• Dependencies

• Maven Repositories

• Plugins

Agenda

• 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

• Folder Structure

• POM.xml File Basics

• Basic Commands inside POM.xml

• Dependencies

• Local Repo

Maven Structure

• 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

• Can be divided into 4 basic

parts

1. Project Info

2. Dependencies

3. Build

Plugins

Directory Structure

4. Repositories

pom.xml

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>

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

1. DependenciesgroupId

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

1. Dependencies

pom.xml

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

<type>jar</type>

</dependency>

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-core</artifactId>

<version>4.3.8.Final</version>

</dependency>

<dependency>

<groupId>commons-lang</groupId>

<artifactId>commons-lang</artifactId>

<version>2.6</version>

</dependency>

</dependencies>

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

1. Dependencies

• Transitive DependenciesOne 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

1. Dependencies

• Exclusions

pom.xml

<dependency>

<groupId>org.apache.maven</groupId>

<artifactId>maven-embedder</artifactId>

<version>2.0</version>

<exclusions>

<exclusion>

<groupId>org.apache.maven</groupId>

<artifactId>maven-core</artifactId>

</exclusion>

</exclusions>

</dependency>

• Local Repository

Maven stores every thing it downloads

Users\<username>\.m2\repository\

Users\<username>\.m2\groupId\artifactId\version\artifactName.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

• Plugins are use to build and package our

application

Goals

Phases

Compiler Plugin

Jar Plugin

Maven Plugins

• 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

• 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

• 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>

• 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>

Maven Plugins<build>

<sourceDirectory>src</sourceDirectory>

<plugins>

<plugin>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.1</version>

<configuration>

<source>1.8</source>

<target>1.8</target>

</configuration>

</plugin>

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>exec-maven-plugin</artifactId>

<version>1.2.1</version>

<configuration>

<mainClass>com.vogella.build.maven.intro.Main</mainClass>

</configuration>

</plugin>

</plugins>

</build>

References

http://www.pluralsight.com/courses/maven-fundamentals

Thank You!