30
© Zühlke 2010 4. Mai 2010 Torben Knerr Folie 1 Maven Intro Einführung in Maven (not the complete reference )

Maven Intro

Embed Size (px)

DESCRIPTION

Was ist eine POM? Wie erzeuge ich neues Maven Projekt? Wie verwende ich Maven in Eclipse? Diese grundlegenden Fragen werden in dieser Maven Einführung zunächst geklärt. Anschließend werden die folgenden Konzepte vertieft: Projekt-Koordinaten, POM Struktur, Dependencies, Parent POM, Repositories, Plugins & Goals, Build Lifecycle, settings.xml und Profile. Zum Schluss gibt es noch eine Übersicht über die wichtigsten Maven Befehle.

Citation preview

Page 1: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 1

Maven Intro

Einführung in Maven (not the complete reference )

Page 2: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 2

Agenda

1. Maven – was ist Das?

2. Getting Started – eine kurze Demo

3. Grundkonzepte von Maven

4. POM Walkthrough

Page 3: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 3

Maven – was ist Das?

Page 4: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 4

Was ist Maven?

Maven, The Definitive Guide: • “Maven is an attempt to apply patterns to a project's build

infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices.”

Maven ist...

• ...ein Build-Tool

• ...ein Abhängigkeiten-Verwalter

• ...ein Repository für Binaries

• ...ganz viel „Convention over Configuration“

Page 5: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 5

Getting Started

Page 6: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 6

Getting Started – eine kurze Demo

Ein neues Java Projekt erstellen:

…so sollte die Struktur aussehen:

mvn archetype:generate \

-DarchetypeArtifactId=maven-archetype-simple \

-DarchetypeGroupId=org.apache.maven.archetypes \

-DgroupId=com.acme \

-DartifactId=maven-intro

maven-intro

| pom.xml

|

\---src

+---main

| \---java

| \---com

| \---acme

| App.java

|

\---test

\---java

\---com

\---acme

AppTest.java

Page 7: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 7

Getting Started – eine kurze Demo II

Kompilieren, Testen, Verpacken:

Ausgabe:

cd maven-intro

mvn clean package

[INFO] Scanning for projects...

[INFO] ------------------------------------------------------------------------

[INFO] Building maven-intro

[INFO] task-segment: [clean, install]

[INFO] ------------------------------------------------------------------------

[INFO] [clean:clean {execution: default-clean}]

...

[INFO] [install:install {execution: default-install}]

[INFO] Installing W:\01_workspace\maven-intro\target\maven-intro-0.0.1-

SNAPSHOT.jar to W:\05_tools\.m2\repository\com\acme\maven-intro\0.0.1-

SNAPSHOT\maven-intro-0.0.1-SNAPSHOT.jar

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 4 seconds

[INFO] Finished at: Wed Feb 10 13:39:01 CET 2010

[INFO] Final Memory: 15M/27M

[INFO] ------------------------------------------------------------------------

Build erfolgreich!

Page 8: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 8

Getting Started – eine kurze Demo (m2eclipse)

Ein neues Java Projekt erstellen:

…und so sollte es aussehen:

File New Other… Maven / Maven Project

Page 9: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 9

Getting Started – eine kurze Demo II (m2eclipse)

Kompilieren, Testen, Verpacken:

Ausgabe:

[INFO] Scanning for projects...

[INFO] ------------------------------------------------------------------------

[INFO] Building maven-intro

[INFO] task-segment: [clean, install]

[INFO] ------------------------------------------------------------------------

[INFO] [clean:clean {execution: default-clean}]

...

[INFO] [install:install {execution: default-install}]

[INFO] Installing W:\01_workspace\maven-intro\target\maven-intro-0.0.1-

SNAPSHOT.jar to W:\05_tools\.m2\repository\com\acme\maven-intro\0.0.1-

SNAPSHOT\maven-intro-0.0.1-SNAPSHOT.jar

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 4 seconds

[INFO] Finished at: Wed Feb 10 13:39:01 CET 2010

[INFO] Final Memory: 15M/27M

[INFO] ------------------------------------------------------------------------

Build erfolgreich!

Project Run As Maven package

packaged jar file

Page 10: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 10

Grundkonzepte von Maven

Page 11: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 11

Maven Koordinaten

Maven, The Definitive Guide: • „Maven Coordinates define a set of identifiers which can be used

to uniquely identify a project, a dependency, or a plugin in a Maven POM.“

Beispiel für eine Koordinate:

Wiederfinden eines Artefakts:

<groupId>com.acme</groupId>

<artifactId>maven-intro</artifactId>

<packaging>jar</packaging>

<version>0.0.1-SNAPSHOT</version>

Page 12: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 12

POM Datei

Eine einfache pom.xml:

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

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.acme</groupId>

<artifactId>maven-intro</artifactId>

<packaging>jar</packaging>

<version>0.0.1-SNAPSHOT</version>

<name>maven-intro</name>

<url>http://maven.apache.org</url>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

</dependencies>

</project>

POM = Project

Object Model

Page 13: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 13

Elemente des POM

The Basics – Maven Koordinaten – Abhängigkeiten (Parent POM, Dependencies) – Properties / Variablen

Build Settings – Build Plugins und deren Konfiguration – Reports

More Project Information – Organisation, Entwickler, Lizenzen, etc..

Environment Settings – Repositories (Dependency Resolution, Distribution) – Issue Tracking, SCM, Continous Integration, Mailinglisten – Profile

Die komplette POM Referenz findet

Ihr hier:

http://maven.apache.org/pom.html

Page 14: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 14

Dependencies

werden in der pom.xml definiert – mittels <dependency>…</dependency>

werden von einem Repository geladen – vom lokalen .m2 repository – vom zentralen Maven repository – von anderen definierten Remote Repositories

sind transitiv – wenn A B und B C, dann A C

haben einen Scope – meistens:

compile, test, runtime, provided

<dependencies>

<dependency>

<groupId>com.acme</groupId>

<artifactId>some-dependency</artifactId>

<version>1.0</version>

<scope>compile</scope>

</dependency>

</dependencies>

Page 15: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 15

Parent POMs

Vererbungshierarchien können mit Parent POMs modelliert werden

• Was wird alles vererbt?

• Was passiert beim Überschreiben?

• Was ist die Super POM?

Tipp zum Debuggen vom POMs:

mvn help:effective-pom

<parent>

<groupId>com.acme</groupId>

<artifactId>acme-parent</artifactId>

<version>0.0.1</version>

</parent>

Page 16: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 16

Repositories

Alle Maven Artefakte (Binaries) werden über Repositories verteilt

• Zentrales Maven Repository – Das default Repository: http://repo1.maven.org/maven2/

• Lokales Maven Repository – Lokaler Cache der heruntergeladenen Artefakte – Hier wird immer zuerst „gesucht“ – Typischerweise in $USER_HOME/.m2/repository

• Weitere Remote Repositories – z.B. http://your.company.com:8080/artifactory/

Page 17: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 17

Plugins und Goals

Ein Plugin stellt eine Menge von Goals bereit

Goal Executions sind an Phasen gebunden (default phase)

Ein Goal kann aber auch explizit ausgeführt werden

führt nur „compile“ Goal des maven-compiler-plugin aus

mvn <plugin>:<goal>

mvn compiler:compile

Page 18: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 18

Plugin Konfiguration

Konfiguration in pom.xml

Konsistente Dokumentation für alle Plugins, z.B. http://maven.apache.org/plugins/maven-surefire-plugin/

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<executions>

<execution>

<id>scala-test</id>

<phase>test</phase>

<goals>

<goal>test</goal>

</goals>

<configuration>

<testSourceDirectory>src/test/scala</testSourceDirectory>

<includes>

<include>**/*Test.class</include>

<include>**/*Suite.class</include>

<include>**/*Spec.class</include>

</includes>

</configuration>

</execution>

</executions>

</plugin>

Page 19: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 19

Build Lifecycle

Ein Build durchläuft mehrere Phasen

An jede Phase können mehrere Goal Executions gebunden werden

Typischerweise:

durchläuft alle Phasen (inkl. aller Goals) bis einschließlich „package“

mvn package

Page 21: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 21

Profile

Profile erlauben „angepassten“ Build, z.B.

•für spezielle Betriebssysteme, Umgebungen (dev, prod), usw.

Aktivierung durch:

•<activeByDefault>, -P <profilname>, System Properties, etc...

<profile>

<id>development</id>

<activation>

<activeByDefault>true</activeByDefault>

</activation>

<properties>

<tomcat.home>W:\install\tomcat</tomcat.home>

</properties>

</profile>

<profile>

<id>production</id>

<properties>

<tomcat.home>/opt/tomcat</tomcat.home>

</properties>

</profile>

Page 22: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 22

settings.xml

Zentrale (vs. projektspezifische) Konfiguration von: •Profilen •Repositories •Proxies, Mirrors •Credentials

Global Settings vs. User Settings: • Global settings.xml: $M2_HOME/conf/settings.xml

• User settings.xml: $USER_HOME/.m2/settings.xml

Page 23: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 23

Nützliche Maven Befehle

Page 24: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 24

Nützliche Maven Befehle (1)

mvn clean

• cleans all files that have been created during the build process in the target folder

mvn install

• builds the artifact and installs it to your Local Maven Repository

mvn clean install –DskipTests

• cleans the target folder and installs the artifact in the local repository, but skips the test execution

mvn clean deploy

• cleans the target folder, builds, installs and deploys the artifact to the configured Remote Maven Repository

Page 25: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 25

Nützliche Maven Befehle (2)

mvn help:effective-pom

• shows the effective POM with all property substitutions and inherited POMs resolved

mvn dependency:tree

• shows the complete dependency tree (with duplicate dependencies)

mvn dependency:list

• shows the actually resolved dependencies (duplicates are resolved)

Page 26: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 26

Nützliche Maven Befehle (3)

mvn release:prepare

• prepares the release: set pom version to releaseVersion, commit to svn, create tag in svn, set pom version to next developmentVersion

mvn release:perform

• performs the release: check out tag version to target folder, build and test the tag version, deploy tag version to maven repository

mvn release:rollback

• if something goes wrong during the release, rollback restores your working copy to the state before the release and commits this state to svn

mvn release:clean

• removes auxiliary files that have been created during (a dry run of) the release process

Page 27: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 27

POM Walkthrough

Page 28: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 28

POM Walkthrough

<insert pom.xml here>

Page 29: Maven Intro

Einführung in Maven

© Zühlke 2010

4. Mai 2010

Torben Knerr

Folie 29

Diskussion / Fragen

POM, Parent POM, Super POM, Settings?

Multimodul Projekt?

mvn clean install vs. deploy?

Maven!?

Page 30: Maven Intro

Vielen Dank! Und lassen Sie uns hier weiter diskutieren: