Introduction to Maven

29

description

Maven

Transcript of Introduction to Maven

  • BuildPreprocessingCompilationPostprocessingDistributionDeployment

  • What is Maven?Mostly used as a build tool for Java projectsIt is more than a build toolProject Object Model (POM)Project lifecyclesDependency managementPlugin frameworkIt is a project management tool

  • A Simple Maven Example

    4.0.0 edu.calstatela.cs520 maven-exmaple 1.0

    pom.xmlRun:mvn compilemvn package

  • pom.xml and modelVersionpom.xml is a description of the projectmodelVersion is the version of the grammar of the description

  • Maven CoordinatesgroupIdName of the company, organization, team etc., usually using the reverse URL naming conventionartifactIdA unique name for the project under groupIdversionpackaging, default: jarclassifierMaven coordinates uniquely identifies a project.

  • Convention Over ConfigurationSystems, libraries, and frameworks should assume reasonable defaults.See the Effect POM tab of pom.xml in Eclipse for allthe defaults.

  • Default Directory Structuresrc/main/javasrc/main/resources for files that should be placed under classpathsrc/main/webapp for web applicationssrc/test/javatarget

  • How Does Maven Work?Q: what happens when you run mvn compile?A: Maven will go through each phase of the build lifecycle up to the compile phase, and run the operations associated with each phase

  • Build LifecycleThe process for building and distributing a projectA build lifecycle consists of a number of steps called phases.

  • Some Lifecycle Phasesvalidatecompiletestpackagedeployhttp://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

  • About Lifecycle PhasesNot all projects utilize all phasesNot all phases have operations associated with them

  • Goals and PluginsGoals, a.k.a. Mojos, are operations provided by Maven plugins

  • Some Maven Pluginsresourcescompilersurefirejar, war

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

  • Example of Using a Plugin

    org.apache.maven.plugins maven-compiler-plugin 2.3.2 default-compile compile compile 1.6

  • About The Plugin ExampleA plugin is uniquely identified by its coordinates just like any other projectGoals are associated (i.e. bound) to a build lifecycle phaseThe behavior of a goal can be customized with additional parameters in the section

  • Run a Maven BuildMaven will go through each build lifecycle phase up to the specified phaseIn each phase, execute the goals bound to that phasemvn

  • Run a Maven Build in EclipseNeed the m2e Eclipse pluginRight click on the project then select Run As Maven Build Give the build a nameEnter the phase name for GoalsClick Run

  • Why Not Just Use an IDECan your IDE do everything you want?Deploy a web application to a remote serverGenerate source code from some metadata filesCreate a zip package of selected files for homework submission

  • Why Use MavenEverybody uses it!Common framework for project build and managementProject Object ModelBuild lifecyclesArchetypeDependency managementResource filtering

  • ArchetypeAn archetype is a template for a Maven project which can be used to create new projects quicklyExample: creating a project from archetypemaven-archetype-quickstartmaven-archetype-webappUsers can create new archetypes and publish them through catalogsMain Maven archetype catalog: http://repo.maven.apache.org/maven2/archetype-catalog.xml

  • Dependency ManagementA dependency of a project is a library that the project depends onAdding a dependency to a project is as simple as adding the coordinates of the library to pom.xmlMaven automatically downloads the library from an online repository and store it locally for future use

  • Dependency ExampleAdd a dependency to pom.xmlAdd a dependency in Eclipse

    javax.servlet javax.servlet-api 3.0.1

  • Dependencies and RepositoriesSearch for dependency coordinates at http://mvnrepository.com/Maven Central Repository - http://repo.maven.apache.org/maven2/Additional libraries and repositories - https://maven.nuxeo.org/

  • More About Dependency ManagementDependencies of a dependency are automatically includedDependency conflicts are automatically resolvedSee CSNS2 for example

  • Resource FilteringUse placeholders in resource files and replace them with actual value during the build process

  • Resource Filtering Example

    build.properties src/main/resources true

  • SummaryProject Object Model (POM)CoordinatesLifecycles and phasesPlugins and goalsArchetypeDependency managementResource filtering

  • Thank You