Mavenppt

Preview:

DESCRIPTION

 

Citation preview

Maven

IDEALINVENT Technologies

Maven is a software project management and automation tool used primarily for Java projects. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information. Maven uses an XML file to describe the software project being built, its dependencies on other external modules and components, the build order, directories, and required plug-ins.

Introduction

Convention and configuration

When a Maven project is created, Maven creates default project structure.

Developer is only required to place files accordingly and he/she need not to define any configuration in pom.xml.

As an example, following table shows the default values for project source code files, resource files and other configurations.

Assuming, ${basedir} denotes the project location:

source code : ${basedir}/src/main/javaResources : ${basedir}/src/main/resourcesTests : ${basedir}/src/test

Complied byte code : ${basedir}/target/classesdistributable JAR : ${basedir}/target

Maven POM • POM stands for Project Object Model.

• It is fundamental Unit of Work in Maven. It is an XML file.

• A file with this name has to exist in the root of every maven project.

• In this file are defined the plugins to use,paths and settings to override the maven defaults of your project.

• Each POM inherits automatically from a superPOM,a virtual POM embedded in the Maven core.

Some of the configuration that can be specified in the POM are following: project dependencies plugins goals build profiles project version developers mailing list

The pom describes the project and its dependencies.Each with 3 "coordinates":

-Group ID :This is an Id of project's group. This is generally unique amongst an organization or a project. For example, a banking group com.company.bank has all bank related projects.

-Artifact ID (common) :This is generally name of the project.

-Version (1.0) :This is the version of the project.

While executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal.

Maven is declarative. Specify the jars used,where they come from,and what release,and maven will manage the files without explicit coding. Even if the jar filename has no version,Maven keeps track.

Maven Repositories

A repository is a place i.e. directory where all the project jars, library jar, plugins or any other project specific artifacts are stored and can be used by Maven easily.

Maven repository are of three types

• local

• central

• remote

Local Repository• It gets created when you run any maven command for the first time.

• In simple, when you build a Maven project, all dependency files will be stored in your Maven local repository.

• When you run a Maven build, then Maven automatically downloads all the dependency jars into the local repository.

•It helps to avoid references to dependencies stored on remote machine every time a project is build.

• Maven local repository by default get created by Maven in %USER_HOME% directory. To override the default location, mention another path in Maven settings.xml file available at %M2_HOME%\conf directory.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings1.0.0.xsd"> <localRepository>C:/MyLocalRepository</localRepository> </settings>

Central Repository

Maven central repository is repository provided by Maven community. It contains a large number of commonly used libraries.• When Maven does not find any dependency in local repository, it starts searching in central repository using following URL: http://repo1.maven.org/maven2/

Key concepts of Central repository

• This repository is managed by Maven community.

• It is not required to be configured.

• It requires internet access to be searched.

Remote Repository It is developer's own custom repository containing required libraries or other

project jars.<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.companyname.projectgroup</groupId> <artifactId>project</artifactId> <version>1.0</version> <dependencies> <dependency> <groupId>com.companyname.common-lib</groupId> <artifactId>common-lib</artifactId> <version>1.0.0</version> </dependency> <dependencies> <repositories> <repository> <id>companyname.lib1</id> <url>http://download.companyname.org/maven2/lib1</url> </repository> <repository> <id>companyname.lib2</id> <url>http://download.companyname.org/maven2/lib2</url> </repository> </repositories> </project>

bus-core-api project

<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>bus-core-api</groupId> <artifactId>bus-core-api</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> </project>

Example for Dependency

Consider a team is developing a project bus-core-api on which two other projects app-web-ui and app-desktop-ui are dependent.

app-web-ui project is using 1.0-SNAPSHOT of bus-core-api project

<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>app-web-ui</groupId> <artifactId>app-web-ui</artifactId> <version>1.0</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>bus-core-api</groupId> <artifactId>bus-core-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies></project>

app-desktop-ui project is using 1.0-SNAPSHOT of bus-core-api project

<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>app-desktop-ui</groupId> <artifactId>app-desktop-ui</artifactId> <version>1.0</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>bus-core-api</groupId> <artifactId>bus-core-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies></project>

In case of Version, if Maven once downloaded the mentioned version say data-service:1.0, it will never try to download a newer 1.0 available in repository. To download the updated code, data-service version is be upgraded to 1.1.

In case of SNAPSHOT, Maven will automatically fetch the latest SNAPSHOT (data-service:1.0-SNAPSHOT) everytime app-ui team build their project.

Snapshot vs Version

A Simple Ant build.xml file. <project name="my-project" default="dist" basedir="."> <description> simple example build file </description> <!-- set global properties for this build --> <property name="src" location="src/main/java"/> <property name="build" location="target/classes"/> <property name="dist" location="target"/> <target name="init"> <!-- Create the time stamp --> <tstamp/> <!-- Create the build directory structure used by compile --> <mkdir dir="${build}"/> </target>

<target name="compile" depends="init" description="compile the source " > <!-- Compile the java code from ${src} into ${build} --> <javac srcdir="${src}" destdir="${build}"/> </target>

<target name="dist" depends="compile" description="generate the distribution" > <!-- Create the distribution directory --> <mkdir dir="${dist}/lib"/>

<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --> <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/> </target> <target name="clean" description="clean up" > <!-- Delete the ${build} and ${dist} directory trees --> <delete dir="${build}"/> <delete dir="${dist}"/> </target></project>

Ant Vs Maven

A Sample Maven pom.xml.

<project> <modelVersion>4.0.0</modelVersion> <groupId>org.sonatype.mavenbook</groupId> <artifactId>my-project</artifactId> <version>1.0</version></project>

You have to tell Ant exactly where your source is, where you want the resulting bytecode to be stored, and how to package this all into a JAR file.

In Maven, to create a JAR file from some Java source, all you need to do is create a simple pom.xml, place your source code in ${basedir}/src/main/java and then run mvn install from the command line.

Running mvn install from the command line will process resources, compile source, execute unit tests, create a JAR, and install the JAR in a local repository for reuse in other projects.

Advantages of Using Maven

Making the build process easy and provide Making the build process easy and provide uniform build environment.uniform build environment. Reusable plugins, repositories.Reusable plugins, repositories. Dependencies are downloaded Dependencies are downloaded automatically.automatically. Standardized, very consistent naming.Standardized, very consistent naming. Maven forces you to have a standard Maven forces you to have a standard directory structure directory structure

Thank you