Maven

From Wiki RB4

Introduction[edit]

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 is written in Java and is used to build projects written primariyl in Java, but also for C#, Scala, Ruby, etc. Based on the Project Object Model (POM), this tool has made the lives of Java developers easier while developing reports, checks build and testing automation setups. It defines a standard directory layout.

Lifecycles[edit]

Maven beinhaltet als wichtiges Konzept genau definierte Build Lifecycles. Die drei eingebauten Standard-Lifecycle sind:

  • clean
  • default, handles project build and deployment
  • site, handles creation of documentation

Phases[edit]

Build Lifecycles bestehen aus Lifecycle bzw. Build-Phasen, die sequentiell ausgeführt werden.

mvn <PHASE>

Beim Aufruf einer Phase werden vor der Phase auch die Vorgänger ausgeführt.

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

  • clean
  • validate
  • compile
  • test
  • package: take the compiled code and package it in its distributable format, such as a JAR
  • 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

The default lifecycle is made up of the following phases:

  • validate
  • compile
  • test
  • package
  • verify
  • install
  • deploy

Goals[edit]

A build phase is made up of a set of goals. Goals sind somit kleinere Tasks und vergleichbar mit Ant-Tasks. Goals können auch mehreren Phasen zugeordnet werden, oder sogar keiner Phase. Einige Phasen haben Standard Goals. Goals können direkt mit der Syntax

mvn <PLUGIN>:<GOAL>

aufgerufen.

To list all goals of a phase by

mvn help:describe -Dcmd=<PHASE> // e.g. mvn help:describe -Dcmd=compile

To list all goals in order how they are executed

mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:list

Plugins[edit]

Maven-Plugins sind Bibliotheken, die thematisch zusammengehörende Goals implementieren bzw. Gruppen von Goals beinhalten. Wichtige Plugins sind zum Beispiel:

  • archetype
  • compiler
  • surefire
  • jar
  • war
  • install
  • deploy
  • site
  • dependency
  • eclipse
  • jetty.

To list all goals of a plug-in

mvn <PLUGIN>:help // e.g. mvn spring-boot:help or mvn exec:help

Archetype[edit]

Ein 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. Archetypes haben

Artefakt[edit]

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

  • groupId (usually a reversed domain name),
  • artifactId (name of the root directory)
  • version (string).

Maven uses the groupId, artifactId, and version to identify dependencies (usually other jar files) needed to build and run your code. Commonly a software project build with maven consists of many maven-projects that build artifacts (e.g. jars) that constitute the product. Each maven project has a unique identifier consiting of [groupId, artifactId, version]. When a maven project requires resources of another project a dependency is configured in it's pom.xml using the above-mentioned identifier. The artifacts of the required projects are then loaded either from the local repository, which is a simple directory in your user's home, or from other (remote) repositories specified in you pom.xml.

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[edit]

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. Profiles can obviously be defined on different levels and can contain different specifications e.g.

<profiles>
  <profile>
    <id>local_dev</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <dependencies>
      <dependency>
      ...

Profiles are activated by the -P parameter e.g. mvn clean install -Plocal_dev.

Repository[edit]

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

  • downloaded jars referenced in the projects
  • downloaded jars maven needs

The central repositories (mvnrepository) are used:

  • to store plugins

The local repository on Windows computers is in

C:\Users\U1728\.m2\repository

Installation[edit]

Conventions[edit]

Project Configuration[edit]

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

pom.xml[edit]

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>[edit]

<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, name of the root directory -->
  <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>[edit]

<dependencies>
...
</dependencies>


<build>[edit]

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

<plugin>[edit]

JBoss AS Plugin[edit]
<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[edit]

mvn -version
mvn [<phase>|<plugin>:<goal>] 
mvn clean
mvn compile
mvn test  // delete all artefacts and target dir
mvn package // build artefacts
mvn help:active-profiles

Creating a project[edit]

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
archetypeArtifactId // template
archetypeGroupId // template
archetypeVersion // template
artifactId // => new project folder and eclipse project name
groupId // => new project package structure

Using Maven in Eclipse[edit]

Using JBoss Plugin[edit]

http://www.mastertheboss.com/jboss-maven/maven-jboss-as-7-plugin-tutorial/page-2

mvn jboss-as:deploy

Resources[edit]