Maven: Difference between revisions

From Wiki RB4
Line 11: Line 11:
*Distribution
*Distribution


Maven beinhaltet als wichtiges Konzept genau definierte Build '''Lifecycles'''. Die drei eingebauten Standard-Lifecycle sind: '''clean''', '''default''' und '''site'''.
Maven beinhaltet als wichtiges Konzept genau definierte Build '''Lifecycles'''. Die drei eingebauten Standard-Lifecycle sind: '''clean''', '''default''' und '''site'''. Build Lifecycles bestehen aus Lifecycle bzw. Build-'''Phasen''', die sequentiell ausgeführt werden.
Build Lifecycles bestehen aus Lifecycle bzw. Build-'''Phasen'''. 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 phases are (full list see [http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference here]):
The most common phases are (full list see [http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference here]):
Line 25: Line 24:
*site: generate documentation to /target/site
*site: generate documentation to /target/site
*deploy: copy package to remote repository for sharing with other developers
*deploy: copy package to remote repository for sharing with other developers
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.


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.
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.

Revision as of 22:14, 1 February 2014

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 eingebauten Standard-Lifecycle sind: clean, default und site. Build Lifecycles bestehen aus Lifecycle bzw. Build-Phasen, die sequentiell ausgeführt werden.

The most common phases 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

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.

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.

Profiles let you customize the build for different environments. Often you have property files that specify database connections and passwords for your development, staging, and production environment.

Repository

There are central and a local repository. The local repository is used for:

  • download jars referenced in the projects

The central repositories (mvnrepository) are used:

  • to store plugins

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 -->
  <dependencies>...</dependencies>
  <build>...</build>
</project>

<dependencies>

<dependencies>
...
</dependencies>


<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