building applications with apache maven

85
Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd. dIon Gillard — Building Applications with Apache Maven Page 1 Building Applications with Apache Maven dIon Gillard Multitask Consulting Pty. Ltd.

Upload: others

Post on 12-Sep-2021

31 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 1

Building Applications with Apache MavendIon GillardMultitask Consulting Pty. Ltd.

Page 2: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 2

AgendaWhat is Maven?The POMSource + POM = BuildCustomizing MavenMaven & IDEsMigrating from AntMulti-project buildsYour own plugins

Page 3: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 3

What Is Maven?A build toolA project comprehension toolSite management toolDocumentation toolQuality control toolRepositoryAnt replacementProject best practices guide

Page 4: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 4

Maven – Build ToolCommand line or IDE integratedHeavily biased towards JavaData-driven from the project object model

Functionality supplied by built-in and third party plug ins, leveraging the POM

Built in functionality for most common J2SE and J2EE needs

Jar, war, ear, compile, test, javadoc, etc.

Page 5: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 5

Maven – Project Comprehension

Tried working with a new project and getting up to speed quickly?Where's the source?What do each of the jars do?What's the API look like?How well do unit tests cover the codeIs there documentation?

Page 6: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 6

Maven – Project Comprehension (Continued)

Each 'project' produces a single jarEach project has a single source directory

Generated sites provide basic information that should always be present.

Links to external systems for bug tracking and SCM help newbies

Encourage best practices, such as testing, code quality, etc.

Page 7: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 7

Maven – Site Project-based siteConsistent documentation format (xdoc)

Can integrate other formats (Docbook, Word, latex, HTML)Can produce PDF, WAR file

Easy to deploy to file system or remote machine (via ssh/scp)

Page 8: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 8

Maven – DocumentationProduces project documentation with just source and POM

Javadocs, Unit Test reports, Code metricsTest coverage, CheckstyleChange log, File and Developer ActivityColoured Source Cross reference, Task list

Other reports rely on youFAQ, Changes report

Page 9: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 9

Maven – QualityMaven can't enforce quality

Several plug ins provide project statistics to help measure qualityTest Coverage: jcoverage and cloverCode style: CheckstyleCode 'smells': PMD, Simian, Checkstyle, FindbugsSCM: Developer activity, statcvs

Page 10: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 10

Maven – Ant ReplacementMaven builds on top of AntEffectively provides a set of reusable Ant targets, with well documented requirementsAnt needs you to write the logic and provide the dataMaven needs you to provide the data and knows how to do most thingsScripting in Maven provided by Jelly, which can easily use Ant tasks

Page 11: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 11

Maven – RepositoryHosted @ ibiblioCentral repo of well named jar files, tld's,....Easily accessibleSimple format

Page 12: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 12

Maven – RepositoryLicenses are availableDistributions in binary and sourceJar files Maven POMs as well

Page 13: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 13

Maven – Local RepositorySingle location for downloads

Md5 files for verification

Version numbers as part of the fileCreated automatically

Page 14: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 14

Maven – Best PracticesHelp to document what they are

Development process URL as part of documentationTest failing stop jar creationSingle jar single projectContents of manifest, project distribution, JavadocsDependency visibility and repository management

Page 15: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 15

AgendaWhat is Maven?The POMSource + POM = BuildCustomizing MavenMaven & IDEsMigrating from AntMulti-project buildsYour own plugins

Page 16: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 16

POMProject Object Model

Captures important meta data about your development effort

XML format means it can be generated or transformed for IDEs, e.g. Eclipse, Idea, JBuilder, Jdeveloper, etc.Maven can run without one, but functionality is severely reducedDefined by schema

Page 17: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 17

POM – Elements<project>

root element

<extend>a project may optionally extend another and inherit its values, overriding as neededThis means many related projects can specify common data in a single placeExamples: plug ins, sub-projects, tag libraries, etc.

Page 18: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 18

POM – Elements (Continued)

<pomVersion>Version of the POM maven is using

<id>Unique name for the projectFormat like a Java variable

<name>A short name for the project

<groupId>The identifier for the organization/team

Page 19: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 19

POM – Elements (Continued)

<currentVersion>The version number of the project, e.g. 1.0, 2.1-alpha-5, 1.5-SNAPSHOT

SNAPSHOT is a 'special' name recognized by maven when downloading<organization>

Details about the development team or organization<name>, <url> and <logo> sub-elements

Page 20: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 20

POM – Elements (Continued)

<inceptionYear>The year the project started – e.g. used for copyright in Javadocs

<package>Java package base for the source codeJUST ONE - why?

<logo>Project logo

Page 21: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 21

POM – Elements (Continued)

<gumpRepositoryId>If your project is being built by gump, it may have a different id

<description>Description of the project, suitable for an about page

<shortDescription>Suitable for an index listing. Typically a one-liner

Page 22: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 22

POM – Elements (Continued)

<url>Web site for the project

<issueTrackingUrl>Web site for bug tracking, e.g. Bugzilla, Jira, etc.

<siteAddress>Host name for the Web site used for publishing

<siteDirectory>Directory on that host where the Web site is to be placed

Page 23: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 23

POM – Elements (Continued)

<distributionSite>Host name where source and binary distributions are published

<distributionDirectory>Directory on that host where the distributions are to be placed

Page 24: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 24

POM – Elements (Continued)

<repository>Details about the SCM system being used

• <connection> – public connection string• scm:cvs:pserver:[email protected]:

/home/cvspublic:maven• Format is scm:type:type-specific-details• <developerConnection> – read write connection• <url> – Web based access to source, e.g. viewcvs

Page 25: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 25

POM – Elements (Continued)

<versions>Details about the versions present in the scm system<version>

• Details about a single version, <id>, <name>, <tag>

<branches>Like versions, except development is happening rather than being fixed

Page 26: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 26

POM – Elements (Continued)

<mailingLists>Nested <mailingList> elements<name>, <subscribe> url, <unsubscribe> url

<developers>Nested <developer> elements<name>, <id> from scm, <email>, <organization>, <roles>/<role>, <url>, <timezone>

Page 27: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 27

POM – Elements (Continued)

<contributors>Like <developers> but no scm <id>

<licenses>Nested <license> elements

• <name>• <url>• <distribution> manual or via central repository

Page 28: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 28

POM – Elements (Continued)

<dependencies>Jars and other artifacts your project requires to build or test or run or...Nested <dependency> elements<groupId> – as before<artifactId> – id of artifact produced by group<version> – specific requirement by your project<jar> – file name if not <artifactId>-<version>-<type>

Page 29: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 29

POM – Elements (Continued)

<dependencies> continued...<type> – jar, war, ear, plugin, tld, etc.<url> – typically where to download it or the dependent project's home page<properties>

• Arbitrary string properties used to mark the dependency for processing later, e.g. <war.bundle>true</war.bundle>

Page 30: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 30

POM – Elements (Continued)

<build>Build time information<nagEmailAddress> - email address to send to on build failure (not used by maven)<sourceDirectory> - only ONE<sourceModifications> - exclude/include code based on classes being available

• <className>, <excludes>/<exclude>, <includes>/<include>

Page 31: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 31

POM – Elements (Continued)

<build> cont...<unitTestSourceDirectory><integrationUnitTestSourceDirectory><aspectSourceDirectory><unitTest>

• <includes>/<include>, <excludes>/<exclude>• <resources>

<directory> – where to find them<targetPath> – where they go<excludes>/<exclude>, <includes>/<include>, <filtering>

Page 32: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 32

POM – Elements (Continued)

<build> cont...<resources> for inclusion in the jar, etc.

• <directory> – where to find them• <targetPath> – where they go• <excludes>/<exclude>, <includes>/<include>, <filtering>

<reports>Nested <report> elementNamed for the plug in producing the report

Page 33: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 33

POM – Elements (Continued)

<properties>Arbitrary string content about the project for use in later processingNot used by Maven yet

Mandatory top levelpomVersion, id, name, currentVersion, organization, inceptionYear, shortDescription, developers, build

Page 34: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 34

AgendaWhat is Maven?The POMSource + POM = BuildCustomizing MavenMaven & IDEsMigrating from AntMulti-project buildsYour own plugins

Page 35: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 35

Source + POM = BuildAdding a project.xml to your projectConfigure it to reflect your code

project.xmlsrc/java/...src/test/...

We'll use the simple-java example that comes with Maven as a start

Page 36: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 36

Source + POM = Buildmaven --goals tells you which plug-ins are installed and availableProduce a jar file

maven jar

Run testsmaven test

Delete generated filesmaven clean

Page 37: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 37

Source + POM = BuildGenerate Javadocs

maven javadoc

Produce project Web sitemaven site

Code coveragemaven clover (commercial)Maven jcoverage (open source)

Generate unit testsMaven junitdoclet

Page 38: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 38

Source + POM = BuildDirectory Structure

Maven specifies directory structureConfigurable via properties, except

project.xmlmaven.xmlMust be in the root directory, or use

• maven --find• maven --dir

Page 39: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 39

Source + POM = BuildDirectory Structure

basedir – directory of project.xmlmaven.src.dir – ${basedir}/srcmaven.build.dir – ${basedir}/targetmaven.build.dest – ${maven.build.dir}/targetmaven.docs.dest – ${maven.build.dir}/docsMost configuration happens through properties a plugin provides

Page 40: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 40

Source + POM = BuildProperty Resolution

${user.dir}/build.properties, e.g.~/build.properties on Unix, or %USERPROFILE%\build.properties on Windows${basedir}/build.properties${basedir}/project.properties${plugin.dir}/plugin.propertiesMaven's defaults.properties

Page 41: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 41

AgendaWhat is Maven?The POMSource + POM = BuildCustomizing MavenMaven & IDEsMigrating from AntMulti-project buildsYour own plugins

Page 42: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 42

Customizing MavenCustomizing via the POMReporting choicesCustomizing via propertiesProviding goals in maven.xmlWriting plug-ins

Page 43: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 43

Customizing Maven - POM

This is the first way you will customize Maven

Tailoring the sourceDirectory and other elements tells Maven and its plug-ins about your directory structure and project meta data.

We've also covered the POM heavily in the previous section, so it's mentioned here as reference only

Page 44: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 44

Customizing Maven – ReportsThis is usually customized via the POM

Can also use preGoal and postGoal in your maven.xml fileReports all come with similar goals:

<report>:register, <report>:deregister, <report>:report

You can call these goals yourself from a custom maven.xml file

Page 45: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 45

Customizing Maven – Reports<reports>

<report>maven-jdepend-plugin</report><report>maven-checkstyle-plugin</report><report>maven-changes-plugin</report><report>maven-changelog-plugin</report><report>maven-findbugs-plugin</report><report>maven-file-activity-plugin</report><report>maven-developer-activity-plugin</report><report>maven-javadoc-plugin</report><report>maven-jxr-plugin</report><report>maven-junit-report-plugin</report><report>maven-tasklist-plugin</report><report>maven-pmd-plugin</report><report>maven-simian-plugin</report>

</reports>

Page 46: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 46

Customizing Maven – ReportsRemove or add a report

<postGoal name="xdoc:register-reports">

<attainGoal name="maven-linkcheck-plugin:deregister"/>

</postGoal>

Page 47: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 47

Customizing Maven –Properties

${user.dir}/build.propertiesAcross all projects

${basedir}/build.propertiesYour changes for a build

${basedir}/project.propertiesProperties provided by the project

${plugin.dir}/plugin.propertiesPlugin defaults

Maven's defaults.properties

Page 48: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 48

Customizing Maven –Installation Properties

$MAVEN_HOME, maven.home.localWhere maven install files and user working files are kept

maven.repo.local${maven.home.local}/repositoryWhere the local repository of artifacts is kept

maven.plugin.dir${maven.home}/pluginsWhere plug-in jars are kept

Page 49: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 49

Customizing Maven –Installation Properties

maven.plugin.unpackeddir${maven.home.local}/pluginsWhere plug-ins are expanded and processed

maven.repo.remotehttp://www.ibiblio.org/maven/Where dependent jars can be downloaded

maven.scp.executable, maven.ssh.executableFor site deployment

Page 50: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 50

Customizing Maven –Directory Structure

basedir – directory of project.xmlmaven.src.dir – ${basedir}/srcmaven.conf.dir – ${basedir}/confmaven.build.dir – ${basedir}/targetmaven.build.dest – ${maven.build.dir}/targetmaven.docs.src – ${basedir}/xdocsmaven.docs.dest – ${maven.build.dir}/docsmaven.gen.docs – ${maven.build.dir}/generated-xdocs

Page 51: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 51

Customizing Maven – Java Plugin Properties

maven.compile.debug = onmaven.compile.optimize = offmaven.compile.deprecation = offmaven.compile.target = 1.1maven.compile.src.set – code to be compiled as an ant pathmaven.compile.compilerargsmaven.compile.fork etc

Page 52: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 52

Customizing Maven – Test Plugin Properties

Uses all compile options from Java pluginmaven.test.dest = ${maven.build.dir}/test-classes

maven.test.reportsDirectory = ${maven.build.dir}/test-reportsmaven.test.skip = falsemaven.junit.jvmargs=maven.junit.fork = no maven.junit.usefile = truemaven.test.failure = true

Page 53: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 53

Customizing Maven – xdoc Plugin Properties

maven.xdoc prefixdate, date.format, date.localedevelopmentProcessUrl = Maven URLincludeProjectDocumentation = yespoweredBy.image, .title, .urlmaven.ui.* - 'stylesheet' values

Page 54: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 54

Customizing Maven – xdoc Sample

Page 55: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 55

Customizing Maven – War plug-in Properties

maven.war.src = src/webappmaven.war.webxml = ${maven.war.src}/WEB-INF/web.xml

maven.war.webapp.dir = ${maven.war.build.dir}/${pom.artifactId} where the Web app is created from source

<properties><war.bundle>true</war.bundle></properties> on a dependency

Page 56: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 56

Customizing Maven – Java Runtime Properties

maven.dependency.classpathAll dependent jars as an Ant path

sourcesPresent – if the sourceDirectory is specified and existsunitTestSourcesPresent – same for unit testsmaven.compile.src.setmaven.test.compile.src.set

Page 57: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 57

Customizing Maven –maven.xml

maven.xml is to maven as build.xml is to AntWhere you can write your own scriptsUses Jelly, not just Ant tasksHave complete access to Maven plugins and core, as well as any Java object you can instantiate

Page 58: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 58

Customizing Maven –maven.xml goals

Like Ant targets, a named action to be executedMay consist of many 'tasks'Maven adds 'preGoal', and 'postGoal' to allow easy hooking of existing processesSyntax is Jelly – a scripting language in XML formatProvided by Werkz

Page 59: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 59

Customizing Maven –maven.xml goals

Sample goalSample preGoalSample postGoal

<project default="jar:jar">

<goal name=”helloWorld”><echo>Hello World!</echo>

</goal>

<preGoal name=”java:compile”><echo>Compile code</echo>

</preGoal>

<postGoal name=”jar:jar”><copyfile=”${maven.final.name”}toDir=”c:/install”/>

</postGoal></project>

Page 60: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 60

Customizing Maven –maven.xml scripting

Jelly has a raft of tag libraries that are available for use in Maven

The core, ant, define and util tag libraries are available simply by using an xmlns declaration in maven.xml<project xmlns:ant=”jelly:ant”>

Page 61: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 61

Customizing Maven –maven.xml scripting

Core:arg, break, case, catch, choose, default, expr, file, forEach, if, import, include, invoke, jelly, new, otherwise, remove, scope, set, setProperties, switch, thread, useBean, useList, when, while, whitespace

Define (allows defining new tags inline)attribute, bean, classLoader, dynaBean, extend, invoke, invokeBody, jellyBean, script, super, tag, taglib

Page 62: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 62

Customizing Maven –maven.xml scripting

Util:available, file, loadText, properties, replace, sleep, tokenize

Ant:Any ant task (well, almost)ant, filescanner, setProperty

Samples

Page 63: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 63

AgendaWhat is Maven?The POMSource + POM = BuildCustomizing MavenMaven & IDEsMigrating from AntMulti-project buildsYour own plugins

Page 64: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 64

Maven & IDEsMaven is still essentially a command line toolHowever it does provide integration with various IDEs

EclipseJDeveloperJBuilderIdeaJDEE

Page 65: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 65

Maven & IDEs – EclipseEclipse plugin

add-maven-repo: adds the MAVEN_REPO variable to Eclipseexternal-tools: adds maven goals to the Run menu of Eclipsegenerate-classpath: creates a .classpath file with the dependencies listed in the POMgenerate-project: creates a .project file with Java nature

Page 66: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 66

Maven & IDEs – Idea, Jbuilder, JDeveloper

idea:generate-project – generate .iml, .ipr and .iws files from POMjbuilder:generate-library – generates project.library file from POMjbuilder:generate-project – generates project.ipr file from POMjdeveloper:generate-project – generates .jpr file from POMJdee:generate-project – generate project file from POM

Page 67: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 67

Maven & IDEs – Eclipse

Page 68: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 68

Maven & IDEs – Eclipse

Page 69: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 69

AgendaWhat is Maven?The POMSource + POM = BuildCustomizing MavenMaven & IDEsMigrating from AntMulti-project buildsYour own plugins

Page 70: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 70

Migrating from AntMaven is a project based tool, Ant is a scripting toolMaven requires meta data up front, Ant is easy entry to hack awayYou have to learn Ant's tasks and their attributes to write a simple 'jar' targetNo procedural scripting in the distributed AntAnt is complete freedom

Page 71: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 71

Migrating from AntHow many build files do you have?Is your Ant build file a single Maven project?Are the targets covered by Maven's plugins?

Does your directory structure fit with Maven's expectations, e.g. single source directory, test in same tree, etc.What do you want to get from the migration?Are there reusable Ant targets that you can turn into plugin(s)?

Page 72: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 72

Migrating from AntSet up project.xmlSet up project.propertiesCopy relevant targets from build.xml to maven.xml as goalsUse plugins wherever possibleTest and iterate

Page 73: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 73

AgendaWhat is Maven?The POMSource + POM = BuildCustomizing MavenMaven & IDEsMigrating from AntMulti-project buildsYour own plugins

Page 74: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 74

Multi-project BuildsMaven itself is a typical exampleCore code and lots of 'plugins'See Jelly and its taglibs, Log4J and its appenders, James and Mailets, Tomcat and valves, etc., Apache and Modules, Netscape and plugins, etc.Multi-project plugin is the start of addressing these needs

Page 75: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 75

Multi-project Builds – Reactor Tag

Tag provided with Maven<maven:reactor

basedir="."

banner="Reactored build"

includes="*/project.xml"

goals=”clean,jar:jar”

postProcessing="true"

ignoreFailures="false"/>

Page 76: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 76

Multi-project Builds – The Plugin

Part of 1.0-beta-10Goals:

site: generate a combined site for all projectscreate-overview-page: create a page with descriptions of all projectsgoal: Run the same goal in all projectsartifact: builds an artifact based on typeinstall: install the artifact for all projectsdeploy: deploy all projects

Page 77: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 77

Multi-project Builds – The Plugin (Continued)

Goals continued...deploy-snapshot: deploy a snapshot of all projects

report: register, deregister and run the dependency convergence reportclean: clean all projects

Page 78: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 78

Multi-project Builds – the plugin properties

Basedir – base directory to search fromIncludes – ant pattern to find projectsExcludes – ant pattern to ignore filesIgnoreFailures – whether to stop if a single project dies

Navigation – aggregate or independent site structureType – jar, war, ear, plugin, etc.

Page 79: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 79

AgendaWhat is Maven?The POMSource + POM = BuildCustomizing MavenMaven & IDEsMigrating from AntMulti-project buildsYour own plugins

Page 80: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 80

Writing PluginsOnce you've experimented with maven.xml, you may find reusable piecesThese reusable pieces are easiest for others to use as a pluginPlugins are installed into $MAVEN_HOME/pluginsAutomatically expanded and unpacked.Plugins can be specified in the dependencies

Page 81: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 81

Writing Plugins – Details

A plugin is like any other project, you need a project.xmlSee the ones in Maven's source tree for a startNext, create a plugin.jelly file to hold the goals and tags you will create

Put properties you expect a user to supply in plugin.properties

Page 82: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 82

Writing Plugins – Details (Continued)

Any properties you need to compile and build your plugin, add to project.properties as per usualIf you think you've got it right run:

maven plugin:install

Now run one of the goals in your pluginmaven myplugin:mygoal

Generate docs: maven plugin:generate-docs

Page 83: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 83

Writing Plugins – Details (Continued)

Create a site for your pluginmaven site

Distribute to usersChange, plugin:install, testDone!Another option

edit in place ~/.maven/plugins/<plugin-name>/plugin.jelly

Page 84: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 84

SummaryMaven is a powerful build toolIt can create lots of useful documentation

What are you going to do with it?

You can work with it or against itIt's only open source

Suggestions and improvements are always welcome

Page 85: Building Applications with Apache Maven

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Multitask Consulting Pty. Ltd.

dIon Gillard — Building Applications with Apache Maven Page 85

Resourceshttp://maven.apache.orghttp://ant.apache.orghttp://jakarta.apache.org/commons/jelly/http://maven-plugins.sourceforge.nethttp://[email protected]://wiki.codehaus.org/maven/[email protected]