Maven

From Wiki RB4

Introduction

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 is essentially a project management and comprehension tool and as such provides a way to help with managing:

  • Builds
  • Documentation
  • Reporting
  • Dependencies
  • SCMs
  • Releases
  • Distribution

Maven beinhaltet als wichtiges Konzept genau definierte Build Lifecycles. Die drei Standard-Lifecycle sind: clean, default und site. Build Lifecycles bestehen aus Lifecycle-Phasen. Wichtige Phasen sind beispielsweise: clean, compile, test, package, integration-test, verify, install, deploy und site. In den Lifecycle-Phasen werden jeweils bestimmte Plugin-Goals ausgeführt. Maven-Plugins sind Bibliotheken, die thematisch zusammengehörende Goals implementieren. Wichtige Plugins sind zum Beispiel: archetype, compiler, surefire, jar, war, install, deploy, site, dependency, eclipse und jetty. Goals können bestimmten Lifecycle-Phasen zugeordnet werden und werden dann automatisch zum richtigen Zeitpunkt aufgerufen (z.B. "compiler:compile"). Maven-Goals sind vergleichbar mit Ant-Tasks.

The most common lifecycle are (full list see here):

  • clean
  • validate
  • compile
  • test
  • package
  • integration-test
  • verify
  • install: install the package into local repository e.g. for use by different projects locally
  • site: generate documentation to /target/site
  • deploy: copy package to remote repository for sharing with other developers

Ein Maven-Archetype ist ein Projekt-Template. Über das archetype-Plugin können Standard-Directory-Layouts und Projektvorlagen für verschiedene Projekttypen erstellt werden (z.B. quickstart, webapp und mojo). Dabei wird auch eine vorläufige pom.xml angelegt.

Artefakt sind im Zusammenhang mit Maven Arbeitsergebnisse gemeint. Maven-Projekte haben in der Regel ein Hauptergebnis-Artefakt, oft eine jar-, war- oder ear-Datei. Mit Koordinaten werden die fünf ein Artefakt identifizierenden Informationsbestandteile bezeichnet. Oft sind vor allem die drei wichtigsten gemeint: groupId, artifactId und version.

In Java programmierte eigene Maven-Plugins bestehen aus Mojos. Ein Mojo ("Maven (plain) old Java Object") ist eine Java-Klasse die das Interface org.apache.maven.plugin.Mojo implementiert (oder org.apache.maven.plugin.AbstractMojo erweitert) und damit ein Plugin-Goal realisiert.

Installation

Conventions

Project Configuration

All information is stored in pom.xml (Project Object Model).

pom.xml

Pom.mxl 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; and so on.

When executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal.

The Super POM is Maven's default POM. All POMs extend the Super POM unless explicitly set, meaning the configuration specified in the Super POM is inherited by the POMs you created for your projects.

<project>

<project>
  <modelVersion> </modelVersion> <!-- mandatory e.g. 4.0.0 -->
  <groupId> </groupId> <!-- mandatory, organization identifier e.g. www.uweheuer.de -->
  <artefactId> </artefactId> <!-- mandotory, base name of the primary artefact extended with version and extension -->
  <packaging> </packaging> <!-- package type with impact on the lifecycle -->
  <version> </version> <!-- mandotory -->

  <!-- groupId, artifactId, and version form the project's fully qualified artifact name -->

  <name> </name> <!-- for documentation -->
  <url> </url> <!-- for documentation -->
  <description> </description> <!-- for documentation -->
  <build>...</build>
</project>

<build>

<build>
  ...
  <plugins>
    <plugin>...</plugin>
  </plugins>
</build>

<plugin>

JBoss AS Plugin
<plugin>
  <groupId>org.jboss.as.plugins</groupId>
  <artifactId>jboss-as-maven-plugin</artifactId>
  <version>7.1.0.Beta1b</version>
</plugin>
mvn [jboss-as:deploy|jboss-as:redeploy|jboss-as:undeploy]

Operation

mvn archetype:generate // starts a wizard beginning with selecting an archetype
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=my-app
mvn <cmd> -Dverbose
mvn clean
mvn compile
mvn test  // delete all artefacts and target dir
mvn package // build artefacts

Resources