maven overview

40
Maven for AMPer - An Overview By Yukti Kaura

Upload: yukti-kaura

Post on 13-May-2015

918 views

Category:

Documents


27 download

DESCRIPTION

One of the training sessions I took

TRANSCRIPT

Page 1: Maven overview

Maven for AMPer - An Overview

By Yukti Kaura

Page 2: Maven overview

2

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

The Intent

The intent of this presentation is to clarify the understanding of how Maven is integrated with AMPer. During the course of this presentation the focus would be to answer the following basic questions

THE BASICS• What is Maven?• What is Maven to AMPer: Case Study?• What does the Maven site contain?• What are the External Maven Tools used in Eclipse?

DIVING DEEPER• What is settings.xml?• What is pom.xml?

Page 3: Maven overview

3

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

What is Maven ?

• MAVEN – is a project management tool.• It provides a superset of features found in a build tool

such as ANT. • In addition to providing build capabilities- used to

build deployable artifacts from source code, Maven can also run reports, generate a web site etc…

• More formally,

Maven is a project management tool which encompasses a project object model, a set of standards, a project lifecycle, a dependency management system, and logic for executing plugin goals at defined phases in a lifecycle. When you use Maven, you describe your project using a well-defined project object model .

Page 4: Maven overview

4

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Project Lifecycle

http://maven.apache.org/plugins/index.html

Page 5: Maven overview

5

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Plugin goals in defined phases of a lifecycle

Plugin contains goals

Bound Goals are Run when

Their Phases Execute

Page 6: Maven overview

6

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Standard Directory Layout of Maven

src/main/java Application/Library sources

src/main/resources Application/Library resources

src/main/filters Resource filter files

src/main/assembly Assembly descriptors

src/main/config Configuration files

src/main/webapp Web application sources

src/test/java Test sources

src/test/resources Test resources

src/test/filters Test resource filter files

src/site Site

LICENSE.txt Project's license

README.txt Project's readme

Benefits of Standards

Page 7: Maven overview

7

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Dependency Management

Project A depends on projects B and C. Project B depends on project D, and project C depends on project E. The full set of direct and transitive dependencies for project A would be projects B, C, D, and E, but all project A had to do was define a dependency on B and C.

Transitive dependencies can come in handy when your project relies on other projects with several small

dependencies (like Hibernate, Apache Struts, or the Spring Framework).

You just tell Maven which You just tell Maven which libraries you need, and libraries you need, and

Maven will take care of the Maven will take care of the libraries that your libraries libraries that your libraries

need (and so on).need (and so on).

A

CB

ED

Page 8: Maven overview

8

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Maven Coordinates

Maven Space is a coordinate system of projects

pom.xml

Page 9: Maven overview

9

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Maven Repository

Maven ships with the bare minimum and fetches from a remote repository when it needs to. Maven ships with a default remote repository location (http://repo1.maven.org/maven2) which it uses to download the core Maven plugins and dependencies.

Maven repositories are both a local cache of artifacts downloaded from a remote repository and a mechanism for allowing your projects to depend on each

other.

Page 10: Maven overview

10

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

How is Maven used in AMPer?

• Making the build process easy

• Providing quality project information» Unit test reports including coverage

• Providing guidelines for best practices development E.g. Source code is assumed to be in ${basedir}/src/main/java and resources are assumed to be in ${basedir}/src/main/resources. Tests are assumed to be in ${basedir}/src/test, and a project is assumed to produce a JAR file.

• Coherent site of project information: Using the same metadata as for the build process, Maven is able to generate a web site

• Maven also assists in project workflow such as release management.

» Maven integrates with source control system SVN and manages the release of a project.

Which version of Maven are we using? Maven

2.0

Page 11: Maven overview

11

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

What does the Maven site contain?

This report contains information about JUnit, FindBugs and CheckStyle.It also contains data about The Lines Of Code, Change Log, Build Log and % Code Coverage.

This is an example of the continuous integration system used by the project.

AirMessenger Personal project report

http://maven.apache.org/plugins/index.html Links

Page 12: Maven overview

12

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

What are the External Maven Tools used in Eclipse?

Page 13: Maven overview

13

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

What are the External Maven Tools used in Eclipse?

• Maven 2 install This tool builds all the modules, and installs it in the local repository. The local repository

is created in your home directory (or alternative location that you created it), and is the location where all downloaded binaries and the projects you built are stored. When you run install, Maven will copy the each project's main artifact and each assembly to your local Maven repository. All of these artifacts are now available for reference as dependencies in other projects locally.

Sample output after running the tool

Page 14: Maven overview

14

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

• Maven 2 Clean This tool deletes the output of a build by deleting the build directory. If you

haven't customized the location of the build directory it will be the ${basedir}/target directory as defined by the Super POM.

Sample output after running the tool

Page 15: Maven overview

15

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

STOP & PONDERSTOP & PONDER

Page 16: Maven overview

16

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Settings.xml

Page 17: Maven overview

17

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

settings.xml

Settings in this file are settings which apply to many projects and which should not be bundled to any specific project, or distributed to an audience. These include values such as the local repository location, alternate remote repository servers, and authentication information. Location where a settings.xml file may live:

» User-specific Settings File: ~/.m2/settings.xml

The settings element in the settings.xml file contains elements used to define values which configure Maven execution

<settings>

<localRepository/>

<interactiveMode/>

<usePluginRegistry/>

<offline/>

<pluginGroups/>

<servers/>

<mirrors/>

<proxies/>

<profiles/>

<activeProfiles/>

</settings>

Page 18: Maven overview

18

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Settings.xml - Piece by Piece

SERVERSSERVERSThe distributionManagement element of the POM defines the repositories for

deployment. However, certain settings such as security credentials should not be distributed along with the pom.xml. This type of information should exist on the build server in the settings.xml.

<servers>

<server>

<username>maven</username> These elements appear as a pair denoting the login <password>maven</password> and password required to authenticate to this server.

<id>firsthop</id> This is the id of the server (not of the user to login as) that

matches the distributionManagement repository element's id </server>

</servers>

MIRRORSMIRRORS<mirrors> <mirror> <id>standalone.repository</id> - The unique identifier of this mirror

<url>file:///C:/AlexandriaPlatformSDK/bin/../m2_fhrepository</url> The base URL of this mirror. <mirrorOf>central</mirrorOf> The id of the server that this is a mirror of.

For example, to point to a mirror of the Maven central server (http://repo1.maven.org/maven2), set this

element to central. </mirror></mirrors>

Page 19: Maven overview

19

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

PROFILESPROFILES

The profile element in the settings.xml is a truncated version of the pom.xml profile element. It consists of the

• activation, • repositories, • pluginRepositories • and properties elements.

The profile elements only include these four elements because they concern themselves with the build system as a whole (which is the role of the settings.xml file), not about individual project object model settings.

If a profile is active from settings, its values will override any equivalent profiles which matching identifiers in a POM.PROFILES - >REPOSITORIES

Repositories are remote collections of projects from which Maven uses to populate the local repository of the build system. It is from this local repository that Maven calls it plugins and dependencies. Different remote repositories may contain different projects, and under the active profile they may be searched for a matching release or snapshot artifact.

settings.xml

Page 20: Maven overview

20

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Snapshot and Release These are the policies for each type of artifact, Release or snapshot. With these two sets, a POM has the power to alter the policies for each type independent of the other within a single repository. For example, one may decide to enable only snapshot downloads, possibly for development purposes.

<repository>

<snapshots>

<enabled>true</enabled> true or false for whether this repository is enabled for the respective type (releases or snapshots)

<updatePolicy>always</updatePolicy> This element specifies how often updates should attempt to occur. Maven will compare the local POMs timestamp to the remote. The choices are: always, daily (default), interval:X (where X is an integer in minutes) or never.

</snapshots>

<releases>

<enabled>true</enabled>

<updatePolicy>always</updatePolicy>

</releases>

<id>firsthop</id>

<name>Airwide Solutions Maven Repository</name> <url>http://maven.alexmontreal.airwidesolutions.com/maven/repository2</url>

</repository>

settings.xml

Page 21: Maven overview

21

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

PROFILES -> PLUGIN REPOSITORIESRepositories are home to two major types of artifacts. The first are artifacts that are used as dependencies of other artifacts. The other type of artifact is plugins. Maven plugins are themselves a special type of artifact. Because of this, plugin repositories are separated from other repositories. The structure of the pluginRepositories element block is similar to the repositories element. The pluginRepository elements each specify a remote location of where Maven can find new plugins

ACTIVE PROFILESACTIVE PROFILES

The final piece of the settings.xml puzzle is the activeProfiles element. This contains a set of active profile elements, which each have a value of a profile id. Any profile id defined as an activeProfile will be active, regardless of any environment settings

<activeProfiles> <activeProfile>firsthopDefault</activeProfile> </activeProfiles>

settings.xml

Page 22: Maven overview

22

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

STOP & PONDERSTOP & PONDER

Page 23: Maven overview

23

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Pom.xml

Page 24: Maven overview

24

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

What is a POM?

• A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Examples for this is the build directory, which is target; the source directory, which is src/main/java; the test source directory, which is src/main/test

• Maven projects, dependencies, builds, artifacts: all of these are objects to be modeled and described. These objects are described by an XML file called a Project Object Model .

The Project Object Model

Page 25: Maven overview

25

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Concepts

Project StructureProject Structure

extensionextension

|-- pom.xml |-- pom.xml

`--`-- srcsrc

|-- main|-- main

| `-- java| `-- java

| `-- com| `-- com

| `-- airwidesolutions| `-- airwidesolutions

| `-- amper | `-- amper

| `-- App.java | `-- App.java

`-- test `-- test

`-- java`-- java

`-- com`-- com

`-- airwidesolutions`-- airwidesolutions

`-- amper`-- amper

`-- AppTest.java`-- AppTest.java

The source directory, which The source directory, which is src/main/java;is src/main/java;

The test source directory, The test source directory, which is src/main/test is which is src/main/test is

Default Project Default Project StructureStructure

Page 26: Maven overview

26

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

The Super POM

• All Maven project POMs extend the Super POM which defines a set of defaults shared by all projects.

• The Super POM defines some standard configuration variables that are inherited by all projects.

The default Super POM defines a single remote Maven repository with an ID of central

• The central Maven repository also contains Maven plugins. The default plugin repository is the central Maven repository.

• The build element sets the default values for directories in the Maven Standard Directory layout .i.e. it specifies the default directory structure.

Page 27: Maven overview

27

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Maven Coordinate

<project><modelVersion>4.0.0</modelVersion> - Current model version should be set to 4.0.0<groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1</version> </project>

Maven Coordinate is made up of three components. These three values form the project's fully qualified artifact name. This is in the form of <groupId>:<artifactId>:<version>

groupIdA groupId groups a set of related artifacts. Group identifiers generally resemble a Java package

name.e.g. com.airwidesolutions.amper

artifactIdThe artifactId is the project's main identifier. When you generate an artifact, this artifact is

going to benamed with the artifactId. e.g. amper.build

versionWhen an artifact is released, it is released with a version number. This version number is an identifier of the form <major version>.<minor version>.<incremental version>-<qualifier> such as "1.1.2-alpha-01". We can also use a snapshot version. A snapshot version is a version for a component which is under development, snapshot version numbers always end in SNAPSHOT; for example, "1.0-SNAPSHOT“.Maven will expand SNAPSHOT token to a date and time value converted to UTC (Coordinated Universal Time) e.g. 1.0-20080207-230803-1 .

Page 28: Maven overview

28

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Project Inheritance

• The following slides deal with 3 scenarios as examples and the variation in the definition of pom.xml in each case

• Example 1 Let us specify their directory structure as this

Let us reuse our previous artifact, com.mycompany.app:my-app:1 as displayed in the previous slide. Now, if we were to turn com.mycompany.app:my-app:1 into a parent artifact of com.mycompany.app:my-module:1,we will have to modify com.mycompany.app:my-module:1's POM to the following configuration:

<project> <parent> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-module</artifactId> <version>1</version></project>

Notice that we now have an added section, the parent section. This section allows us to specify which artifact is the parent of our POM.

Alternatively, if we want the groupId and / or the version of your modules to be the same as their parents, you can remove the groupId and / or the version identity of your module in its POM.

Page 29: Maven overview

29

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Project Aggregation

• Project Aggregation is similar to Project Inheritance. But instead of specifying the parent POM from the module, it specifies the modules from the parent POM. By doing so, the parent project now knows its modules, and if a Maven command is invoked against the parent project, that Maven command will then be executed to the parent's modules as well. To do Project Aggregation, you must do the following:

• Change the parent POMs packaging to the value "pom" .• Specify in the parent POM the directories of its modules (children POMs)

<project> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1</version> <packaging>pom</packaging>

<modules><modules>

<module>my-module</module><module>my-module</module>

</modules></modules></project>

In the revised com.mycompany.app:my-app:1, the packaging section and the modules sections were added. For the packaging, it's value was set to "pom", and for the modules section, we have the element <module>my-module</module>. The value of <module> is the relative path from the com.mycompany.app:my-app:1 to com.mycompany.app:my-module:1's POM (by practice, we use the module's artifactId as the module directory's name).

Page 30: Maven overview

30

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Project Inheritance vs Project Aggregation

INHERITANCE

• If you have several Maven projects, and they all have similar configurations, you can refactor your projects by pulling out those similar configurations and making a parent project. Thus, all you have to do is to let your Maven projects inherit that parent project, and those configurations would then be applied to all of them.

AGGREGATION• And if you have a group of projects that are built or processed together, you can

create a parent project and have that parent project declare those projects as its modules. By doing so, you'd only have to build the parent and the rest will follow.

• But of course, you can have both Project Inheritance and Project Aggregation. Meaning, you can have your modules specify a parent project, and at the same time, have that parent project specify those Maven projects as its modules. You'd just have to apply all three rules:

• Specify in every child POM who their parent POM is.• Change the parent POMs packaging to the value "pom" .• Specify in the parent POM the directories of its modules (children POMs)

Page 31: Maven overview

31

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Example 3To do both project inheritance and aggregation, you only have to apply all three rules. com.mycompany.app:my-app:1's POM

<project> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1</version> <packaging>pom</packaging> <modules> <module>../my-module</module> </modules></project>com.mycompany.app:my-module:1's POM

<project> <parent> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1</version> <relativePath>../parent/pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>my-module</artifactId></project>

Page 32: Maven overview

32

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Dependency Management

• Maven can manage both internal and external dependencies. An external dependency for a Java project might be a library such as Log4J. An internal dependency is illustrated by a web application project depending on another project that contains service classes, model objects, or persistence logic.

Dependency Scope • Scope controls which dependencies are available in which classpath, and which

dependencies are included with an application. • compile compile is the default scope; all dependencies are compile-scoped if a scope is not

supplied. compile dependencies are available in all classpaths.Furthermore, those dependencies are propagated to dependent projects.

• providedprovided dependencies are used when you expect the JDK or a container to provide.provided dependencies are available on the compilation classpath (not runtime).

• runtimeruntime dependencies are required to execute and test the system, but they are not required for compilation

• test test-scoped dependencies are not required during the normal operation of an

application, and they are available only during test compilation and execution phases

Page 33: Maven overview

33

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Example from ldap/pom.xml

<dependencies> <dependency> <groupId>com.firsthop.wb2</groupId> <artifactId>wb2.platform.nnapi</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>com.firsthop.wb2</groupId> <artifactId>wb2.platform.nnapi</artifactId> <type>test-jar</type> <scope>provided</scope> </dependency> <dependency> <groupId>com.firsthop.wb2</groupId> <artifactId>wb2.platform.mndeploy</artifactId> <scope>compile</scope> </dependency>

</dependencies>

Page 34: Maven overview

34

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Dependency Management

• The dependency management section is a mechanism for centralizing dependency information. When you have a set of projects that inherits a common parent it's possible to put all information about the dependency in the common POM and have simpler references to the artifacts in the child POMs.

• Maven provides a way for you to consolidate dependency version numbers in the dependencyManagementdependencyManagement  element. You'll usually see the  dependencyManagement dependencyManagement  element in a top-level parent POM for an organization or project. Using the  dependencyManagementdependencyManagement  element in a pom.xml allows you to reference a dependency in a child project without having to explicitly list the version. Maven will walk up the parent-child hierarchy until it finds a project with a  dependencyManagement dependencyManagement  element, it will then use the version specified in this  dependencyManagementdependencyManagement  element.

• Default dependency information for projects that inherit from this one. The dependencies in this section are not immediately resolved. Instead, when a POM derived from this one declares a dependency described by a matching groupId and artifactId, the version and other values from this section are used for that dependency if they were not already specified.

Page 35: Maven overview

35

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Example

For example, if you have a large set of projects which make use of the amper.personalservicelogic.api version 0.0.7, you could define the following dependencyManagement element in your multi-module project's top-level POM.<dependencyManagement>

<dependencies>

<dependency>

<groupId>com.airwidesolutions.amper</groupId> <artifactId>amper.personalservicelogic.api</artifactId>

<version>0.0.7</version>

</dependency> <dependencies>

</dependencyManagement>

Then, in a child project, you can add a dependency to the amper.personalservicelogic.api using the following dependency XML:

<dependencies>

<dependency>

<groupId>com.airwidesolutions.amper</groupId> <artifactId>amper.personalservicelogic.api</artifactId>

</dependency>

<dependencies>

The child project did not have to explicitly list the version of the amper.personalservicelogic.api dependency. Because this dependency was defined in the top-level POM's dependencyManagement element, the version number is going to propagate to the child project's dependency

Page 36: Maven overview

36

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Build tag The <build> element contains informations required to build the project. <build> <plugins> The list of plugins to use. <plugin> <artifactId>maven-release-plugin</artifactId> <configuration>

<tagBase>https://svn.alexmontreal.airwidesolutions.com/svn/repository /amper/selfprovisioning/tags</tagBase>

</configuration> </plugin> </plugins> <testResources> This element describes all of the classpath resources such as properties files associated with a project's unit tests. <testResource> <filtering>true</filtering> Whether resources are filtered to replace tokens

with parameterised values or not. The values are taken from the properties element

<directory>${basedir}/src/test/resources</directory>Describe the directory where the resources are stored.

<includes> <include>**/wb2.xml</include> A list of patterns to include, e.g. **/*.xml. </includes> </testResource> </testResources> </build>

Page 37: Maven overview

37

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

SCM tag

The <scm> element contains informations required to the SCM (Source Control Management) of the project.

<scm> <connection>scm:svn:https://svn.alexmontreal.airwidesolutions.com/svn/repository/amper/selfprovisioning/trunk</connection>

The source control management system URL that describes the repository and how

  and how to connect to the repository. Note :- The SCM we use is SubVersioN which is among the fully implemented SCM of Maven

<developerConnection>scm:svn:https://svn.alexmontreal.airwidesolutions.com/svn/repository/amper/selfprovisioning/trunk</developerConnection>

Just like connection, but for developers, i.e. this scm connection will not be read only.

<url>https://svn.alexmontreal.airwidesolutions.com/svn/repository/amper/selfprovisioning/trunk</url>

The URL to the project's browsable SCM repository </scm>

Page 38: Maven overview

38

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

Distribution Management

This element describes all that pertains to distribution for a project. It is primarily used for deployment of artifacts and the site produced by the build.

<distributionManagement> <repository> Information needed to deploy the artifacts generated by the

project to a remote repository <id>firsthop</id> A unique identifier for a repository. This is used to

match the repository to configuration in the settings.xml file

<name> Airwide Solutions Maven Repository</name> Human readable name of the repository. <url> scp://maven.alexmontreal.airwidesolutions.com/srv/maven/repository2</url>

The url of the repository, in the form protocol://hostname/path. </repository> </distributionManagement>

Page 39: Maven overview

39

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

STOP & PONDERSTOP & PONDER

Page 40: Maven overview

40

April 12, 2023Maven for AMPer - An Overview | CONFIDENTIAL 2008

References

• http://maven.apache.org/ref/current/maven-model/maven.html#class_DependencyManagement

• http://www.sonatype.com/book/reference/public-book.html

• http://maven.apache.org/guides/