Maven overview

1,124 views

Published on

One of the training sessions I took

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,124
On SlideShare
0
From Embeds
0
Number of Embeds
148
Actions
Shares
0
Downloads
22
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • December 13, 2012
  • December 13, 2012 A goal is a specific task that may be executed as a standalone goal or along with other goals as part of a larger build. A goal is a “unit of work” in Maven. Examples of goals include the compile goal in the Compiler plugin, which compiles all of the source code for a project. When referring to a plugin goal, we frequently use the shorthandnotation: pluginId : goalId When the jar:jar goal needs to create a JAR file, it looks to the POM to find out what the JAR file’s name is
  • December 13, 2012 At the top level files descriptive of the project: a pom.xml file (and any properties, maven.xml or build.xml if using Ant). In addition, there are textual documents meant for the user to be able to read immediately on receiving the source: README.txt, LICENSE.txt, etc. There are just two subdirectories of this structure: src and target. The only other directories that would be expected here are metadata likeCVS or .svn, and any subprojects in a multiproject build (each of which would be laid out as above). The target directory is used to house all output of the build. The src directory contains all of the source material for building the project, its site and so on. It contains a subdirectory for each type: main for the main build artifact, test for the unit test code and resources, site and so on.
  • December 13, 2012 These four elements become the key to locating and using one particular project in the vast space of other “Mavenized” projects . Maven repositories (public, private, and local) are organized according to these identifiers. When this project is A Simple Maven Project installed into the local Maven repository, it immediately becomes locally available to any other project that wishes to use it. All you must do is add it as a dependency of another project using the unique Maven coordinates for a specific artifact. mavenbook:my-app:jar:1.0-SNAPSHOT. This notation also applies to project dependencies, our project relies on JUnit version 3.8.1, it contains a dependency on junit:junit:jar:3.8.1.
  • Maven overview

    1. 1. Maven for AMPer - An Overview By Yukti Kaura
    2. 2. The Intent 2 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?Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    3. 3. What is Maven ? 3 • 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 .Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    4. 4. Project Lifecycle 4 http://maven.apache.org/plugins/index.htmlMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    5. 5. Plugin goals in defined phases of a lifecycle 5 Plugin contains goals Bound Goals are Run when Their Phases ExecuteMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    6. 6. Standard Directory Layout of Maven 6 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 Projects license README.txt Projects readme Benefits of StandardsMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    7. 7. Dependency Management 7 Project A depends on projects B and A 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 C projects B, C, D, and E, but all B project A had to do was define a dependency on B and C. Transitive dependencies can come D E in handy when your project relies on other projects with several small dependencies (like Hibernate, Apache Struts, or the Spring You just tell Maven which Framework). libraries you need, and Maven will take care of the libraries that your libraries need (and so on).Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    8. 8. Maven Coordinates 8 pom.xml Maven Space is a coordinate system of projectsMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    9. 9. Maven Repository 9 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.Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    10. 10. How is Maven used in AMPer? 10 • 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. Which version of Maven are we • Coherent site of project information: Using the same using? Maven 2.0 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.Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    11. 11. What does the Maven site contain? 11AirMessenger Personal project reportThis report contains information about JUnit, FindBugs and CheckStyle.Italso 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 theproject. http://maven.apache.org/plugins/index.html LinksMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    12. 12. 12 What are the External Maven Tools used in Eclipse?Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    13. 13. What are the External Maven Tools used in Eclipse? 13 • 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 projects 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 toolMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    14. 14. 14 • Maven 2 Clean This tool deletes the output of a build by deleting the build directory. If you havent 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 toolMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    15. 15. 15 STOP & PONDERMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    16. 16. 16 Settings.xmlMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    17. 17. settings.xml 17 Settings in this file are settings which apply to many <settings> <localRepository/> projects and which should not be bundled to any <interactiveMode/> specific project, or distributed to an audience. These <usePluginRegistry/ > include values such as the local repository location, <offline/> alternate remote repository servers, and <pluginGroups/> authentication information. Location where <servers/> <mirrors/> a settings.xml file may live: <proxies/> » User-specific Settings File: ~/.m2/settings.xml <profiles/> <activeProfiles/> </settings> The settings element in the settings.xml file contains elements used to define values which configure Maven executionMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    18. 18. Settings.xml - Piece by Piece 18 SERVERS The 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 elements id </server> </servers> MIRRORS <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>Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    19. 19. settings.xml 19 PROFILES 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.Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    20. 20. settings.xml 20 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>Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    21. 21. settings.xml 21 PROFILES -> PLUGIN REPOSITORIES Repositories 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 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>Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    22. 22. 22 STOP & PONDERMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    23. 23. 23 Pom.xmlMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    24. 24. What is a POM? 24 • 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 The Project Object Model objects to be modeled and described. These objects are described by an XML file called a Project Object Model .Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    25. 25. Concepts 25 Project Structure extension |-- pom.xml The source directory, which `-- src is src/main/java; |-- main The test source directory, | `-- java which is src/main/test is | `-- com Default Project Structure | `-- airwidesolutions | `-- amper | `-- App.java `-- test `-- java `-- com `-- airwidesolutions `-- amper `-- AppTest.javaMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    26. 26. The Super POM 26 • 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.Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    27. 27. Maven Coordinate 27 <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 projects fully qualified artifact name. This is in the form of <groupId>:<artifactId>:<version> groupId A groupId groups a set of related artifacts. Group identifiers generally resemble a Java package name. e.g. com.airwidesolutions.amper artifactId The artifactId is the projects main identifier. When you generate an artifact, this artifact is going to be named with the artifactId. e.g. amper.build version When 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 .Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    28. 28. Project Inheritance 28 • 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:1s POM to the following configuration: <project> <parent> Notice that we now have an added <groupId>com.mycompany.app</groupId> section, the parent section. This <artifactId>my-app</artifactId> section allows us to specify which <version>1</version> artifact is the parent of our POM. </parent> <modelVersion>4.0.0</modelVersion> Alternatively, if we want the groupId <groupId>com.mycompany.app</groupId> <artifactId>my-module</artifactId> and / or the version of your modules to <version>1</version> be the same as their parents, you can </project> remove the groupId and / or the version identity of your module in its POM.Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    29. 29. Project Aggregation 29 • 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 parents 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> In the revised com.mycompany.app:my-app:1, the <modelVersion>4.0.0</modelVersion> packaging section and the modules sections were <groupId>com.mycompany.app</groupId> added. For the packaging, its value was set to "pom", and <artifactId>my-app</artifactId> for the modules section, we have the element <version>1</version> <module>my-module</module>. <packaging>pom</packaging> The value of <module> is the relative path from the com.mycompany.app:my-app:1 to <modules> com.mycompany.app:my-module:1s POM (by practice, <module>my-module</module> we use the modules artifactId as the module directorys </modules> name). </project>Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    30. 30. 30 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, youd 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. Youd 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)Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    31. 31. Example 3 31 To do both project inheritance and aggregation, you only have to apply all three rules. com.mycompany.app:my-app:1s 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:1s 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>Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    32. 32. Dependency Management 32 • 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. • provided provided dependencies are used when you expect the JDK or a container to provide. provided dependencies are available on the compilation classpath (not runtime). • runtime runtime 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 phasesMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    33. 33. Example from ldap/pom.xml 33 <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>Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    34. 34. Dependency Management 34 • The dependency management section is a mechanism for centralizing dependency information. When you have a set of projects that inherits a common parent its 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 dependencyManagement element. Youll usually see the dependencyManagement element in a top-level parent POM for an organization or project. Using the dependencyManagement 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 element, it will then use the version specified in this dependencyManagement 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.Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    35. 35. Example 35 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 projects 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 POMs dependencyManagement element, the version number is going to propagate to the child projects dependencyMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    36. 36. Build tag 36The <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 projects 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>Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    37. 37. SCM tag 37 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/selfprovi sioning/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/sel fprovisioning/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 projects browsable SCM repository </scm>Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    38. 38. Distribution Management 38 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>Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    39. 39. 39 STOP & PONDERMaven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012
    40. 40. References 40 • 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/Maven for AMPer - An Overview | CONFIDENTIAL 2008 December 13, 2012

    ×