24
Maven 2.0 Maven 2.0 Introduction Introduction

Maven

Embed Size (px)

DESCRIPTION

A maven 2.0 introduction http://in.linkedin.com/in/darshanvartak/

Citation preview

Page 1: Maven

Maven 2.0Maven 2.0

IntroductionIntroduction

Page 2: Maven

Before ANTBefore ANT

Earlier tool used for build was "make" Earlier tool used for build was "make" The syntax used to be very crypticThe syntax used to be very cryptic Maintenance of many make filesMaintenance of many make files

Page 3: Maven

ANTANT

ANT is cross platformANT is cross platform ANT is java basedANT is java based ANT replaces the make file's difficult ANT replaces the make file's difficult

syntax with more standardized XMLsyntax with more standardized XML

Page 4: Maven

Tasks of a configuration Tasks of a configuration and build managerand build manager

Copy, Clean Copy, Clean Compile, JarCompile, Jar Jar class filesJar class files Generate the java docGenerate the java doc Test and Deploy Test and Deploy

Page 5: Maven

Disadvantages of ANTDisadvantages of ANT Generally you have a template with which Generally you have a template with which

you begin with for a new projectyou begin with for a new project Ant does not provide convenient ways of Ant does not provide convenient ways of

reuse of code. Most of the targets or build reuse of code. Most of the targets or build scripts look similar, but one has to resort scripts look similar, but one has to resort to copy paste to reuse the same code.to copy paste to reuse the same code.

Creating custom tasks is a workaround, Creating custom tasks is a workaround, but then it becomes an extra activity to but then it becomes an extra activity to write and maintain custom tags in ANTwrite and maintain custom tags in ANT

Page 6: Maven

Disadvantages of ANTDisadvantages of ANT The size of the script grows as the number The size of the script grows as the number

of tasks executed increase and one has to of tasks executed increase and one has to write all this and maintain these complex write all this and maintain these complex ANT scriptsANT scripts

Logic and looping structures are not Logic and looping structures are not supported and one has to use custom build supported and one has to use custom build tasks for the same. These custom tasks tasks for the same. These custom tasks might not be reusable across projects. If you might not be reusable across projects. If you intend to do that, then one will have to put in intend to do that, then one will have to put in lot of effort to write such tasks. Generally lot of effort to write such tasks. Generally you end up with numerous and redundant you end up with numerous and redundant task in each project.task in each project.

Page 7: Maven

MAVENMAVEN

Maven is a high-level, project Maven is a high-level, project management, build and deployment management, build and deployment tool from the Apache project that adds tool from the Apache project that adds a layer of abstraction above Ant. a layer of abstraction above Ant.

Page 8: Maven

Tasks Maven can doTasks Maven can do

Compile your code. Compile your code. Create Java doc documentation. Create Java doc documentation. Run unit tests. Run unit tests. Run source code metrics and analysis. Run source code metrics and analysis. Create a report detailing violations of your Create a report detailing violations of your

team's coding standards. team's coding standards. Create a report of the most recent CVS Create a report of the most recent CVS

commits. commits. Create a report of the CVS activity, pivoted by Create a report of the CVS activity, pivoted by

file and developer. file and developer.

Page 9: Maven

Maven AdvantagesMaven Advantages

Supports common build related tasksSupports common build related tasks Most of the generic tasks have already been Most of the generic tasks have already been

shipped with maven. shipped with maven. Most of the task have to be written manually Most of the task have to be written manually

in case of ant. Maven is standardized. Its in case of ant. Maven is standardized. Its basically based on POM. POM is the way basically based on POM. POM is the way one indicates the directory structure to one indicates the directory structure to maven. Once this is done one can execute maven. Once this is done one can execute most of the inbuilt maven goals (equivalent most of the inbuilt maven goals (equivalent of ant targets).of ant targets).

Page 10: Maven

Maven AdvantagesMaven Advantages

Tasks like Compile, Copy, Clean, Jar, Java Tasks like Compile, Copy, Clean, Jar, Java doc ,Test, Deploy can be executed using doc ,Test, Deploy can be executed using maven without writing even a single line of maven without writing even a single line of script.script.

Produces lot of technical information to track Produces lot of technical information to track the state of the projectthe state of the project

All the ant tasks are available from inside All the ant tasks are available from inside your Maven projectyour Maven project

Page 11: Maven

Maven GoalsMaven Goals

mvn cleanmvn clean mvn compilemvn compile mvn packagemvn package mvn testmvn test mvn site:generatemvn site:generate mvn eclipse:eclipsemvn eclipse:eclipse

Page 12: Maven

POMPOM

Its a XML file. Describes project directory structure, Its a XML file. Describes project directory structure, project dependencies like jar files. Its a kind of project dependencies like jar files. Its a kind of metadata. Once the project is described using POM, metadata. Once the project is described using POM, any of the maven tasks can be invoked. The any of the maven tasks can be invoked. The pom.xml file is the core of a project's configuration in pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains Maven. It is a single configuration file that contains the majority of information required to build a project the majority of information required to build a project in just the way you want. The POM is huge and can in just the way you want. The POM is huge and can be daunting in its complexity, but it is not necessary be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it to understand all of the intricacies just yet to use it effectively.effectively.

Page 13: Maven

POMPOM<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-

v4_0_0.xsd">v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion><modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId><groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId><artifactId>my-app</artifactId> <packaging>jar</packaging><packaging>jar</packaging> <version>1.0-SNAPSHOT</version><version>1.0-SNAPSHOT</version> <name>my-app</name><name>my-app</name> <url>http://maven.apache.org</url><url>http://maven.apache.org</url> <dependencies><dependencies> <dependency><dependency> <groupId>junit</groupId><groupId>junit</groupId> <artifactId>junit</artifactId><artifactId>junit</artifactId> <version>3.8.1</version><version>3.8.1</version> <scope>test</scope><scope>test</scope> </dependency></dependency> <dependency><dependency> <groupId>org.apache.maven.plugins</groupId><groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId><artifactId>maven-jar-plugin</artifactId> <version>2.2</version><version>2.2</version> <scope>test</scope><scope>test</scope> </dependency></dependency> </dependencies></dependencies></project></project>

Page 14: Maven

Standard Directory StructureStandard Directory Structure

By using the standard conventions the By using the standard conventions the POM becomes very small and you POM becomes very small and you haven't had to tell Maven explicitly haven't had to tell Maven explicitly where any of your sources are or where any of your sources are or where the output should go. By where the output should go. By following the standard Maven following the standard Maven conventions you can get a lot done conventions you can get a lot done with very little effortwith very little effort

Page 15: Maven

Standard Directory StructureStandard Directory Structure

my-appmy-app|-- pom.xml|-- pom.xml`-- src`-- src |-- main|-- main | `-- java| `-- java | `-- com| `-- com | `-- mycompany| `-- mycompany | `-- app| `-- app | `-- App.java| `-- App.java `-- test`-- test `-- java`-- java `-- com`-- com `-- mycompany`-- mycompany `-- app`-- app `-- AppTest.java`-- AppTest.java

Page 16: Maven

Unit Testing SupportUnit Testing Support

Maven makes it easy for the Maven makes it easy for the programmer to build his test cases and programmer to build his test cases and execute them. The support for test execute them. The support for test cases is build into the maven and only cases is build into the maven and only thing programmer has to do is put the thing programmer has to do is put the test classes in the appropriate directorytest classes in the appropriate directory

Page 17: Maven

Protecting Your ResourcesProtecting Your Resources

Class files are usually loaded from the classpath. Class files are usually loaded from the classpath. Non-class files (resources) can be loaded from the Non-class files (resources) can be loaded from the classpath as well, using the various getResource classpath as well, using the various getResource methods. If you want to keep these resources from methods. If you want to keep these resources from getting wiped out every time you invoke the clean getting wiped out every time you invoke the clean goal, you need to store resources outside of the goal, you need to store resources outside of the classes directory and copy them into the classes classes directory and copy them into the classes directory as part of the build process. Maven has a directory as part of the build process. Maven has a goal for this purpose called jar-resources. It doesn't goal for this purpose called jar-resources. It doesn't jar anything, it just copies resources to the jar anything, it just copies resources to the target/classes directory. Maven automatically calls target/classes directory. Maven automatically calls the jar-resources goal when you run the jar goal. the jar-resources goal when you run the jar goal.

Page 18: Maven

Protecting Your ResourcesProtecting Your Resources

First, you need to tell Maven where your resource files are: First, you need to tell Maven where your resource files are:

Add the resources element, as shown below, to POM.xml: Add the resources element, as shown below, to POM.xml: ...... </unitTest></unitTest> <resources><resources> <resource><resource> <directory><directory> ${basedir}/src/conf${basedir}/src/conf </directory> </directory> </resource></resource> </resources></resources></build></build>......

Place a resource file, say box.properties, in <project_home>/src/conf. Place a resource file, say box.properties, in <project_home>/src/conf. Run the jar goal, which indirectly runs the jar-resources goal: mvn jarRun the jar goal, which indirectly runs the jar-resources goal: mvn jarLook in the target/classes folder. You should see that Maven copied box.properties Look in the target/classes folder. You should see that Maven copied box.properties from src/conf to target/classes.from src/conf to target/classes.

Page 19: Maven

DependenciesDependencies

The dependencies section of the pom.xml The dependencies section of the pom.xml lists all of the external dependencies that lists all of the external dependencies that our project needs in order to build (whether our project needs in order to build (whether it needs that dependency at compile time, it needs that dependency at compile time, test time, run time, or whatever)test time, run time, or whatever)

scope : compile , test , and runtimescope : compile , test , and runtime

Page 20: Maven

DependenciesDependencies

......<dependencies><dependencies> <dependency><dependency> <groupId>junit</groupId><groupId>junit</groupId> <artifactId>junit</artifactId><artifactId>junit</artifactId> <version>3.8.1</version><version>3.8.1</version> <scope>test</scope><scope>test</scope> </dependency></dependency> </dependencies></dependencies>......

Page 21: Maven

Repository FeatureRepository Feature

Another interesting feature of Maven is its use of a central repository Another interesting feature of Maven is its use of a central repository to access the .jars needed to build your project . You list what .jars to access the .jars needed to build your project . You list what .jars and versions you need, and Maven is intelligent enough to go and and versions you need, and Maven is intelligent enough to go and download them for you. When you run Maven for the first time, or download them for you. When you run Maven for the first time, or attain a new goal, you will see console output detailing repository attain a new goal, you will see console output detailing repository access to download any required .jars. This is a great feature that access to download any required .jars. This is a great feature that not only makes Maven easier to use and set up, but also saves you not only makes Maven easier to use and set up, but also saves you a lot of time and the headaches of keeping your own collection of a lot of time and the headaches of keeping your own collection of dependencies up to date in a local directory or in your source-code dependencies up to date in a local directory or in your source-code control system.control system.

Maven checks the local repository and download the jars, which are Maven checks the local repository and download the jars, which are not available in the local repositorynot available in the local repository..

(~/.m2/repository is the default location(~/.m2/repository is the default location e.g C:\Documents and Settings\darshan.vartak\.m2e.g C:\Documents and Settings\darshan.vartak\.m2

Page 22: Maven

InternationalizationInternationalization

<project xmlns="http://maven.apache.org/POM/4.0.0"<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">http://maven.apache.org/xsd/maven-4.0.0.xsd"> ...... <build><build> <plugins><plugins> <plugin><plugin> <groupId>org.apache.maven.plugins</groupId><groupId>org.apache.maven.plugins</groupId> <artifactId>maven-site-plugin</artifactId><artifactId>maven-site-plugin</artifactId> <configuration><configuration> <locales>en,fr</locales><locales>en,fr</locales> </configuration></configuration> </plugin></plugin> </plugins></plugins> ......

Page 23: Maven

Maven TermsMaven Terms

Project Object Model (POM)

An XML file that declaratively describes a project Basically, the POM is project meta-data. Meta-data includes build,

dependency, and general project management information. This information is used by goals.

Goal

An executable function that acts upon a project Goals are written in Jelly Script. They can be project-specific or

reusable across projects. In either case, they act upon your project. In OOP terms, think of your project and its meta-data as an object and goals as the methods that act upon that project.

Jelly Script

Executable XML Jelly is an XML-based scripting language. Imagine a combination

of Ant and JSTL tags that's more general purpose. It's not coupled to a servlet engine.

Page 24: Maven

Maven TermsMaven Terms

maven.xmlWhere you define project-specific goals, as Jelly Scripts maven.xml is analogous to Ant's build.xml

Plug-in

Where you define reusable, cross-project goals Plug-ins represent Maven's major advantage: goal reuse. The actual Jelly Script goals are defined in a file called plugin.jelly within the plug-in's folder.

RepositoryA folder for storing jar files or, more generally, build artifacts Think of the repository as a more structured lib folder. Maven supports shared network repositories and local repositories.

Dependency A jar file or other artifact upon which your project depends