Maven Cheatsheet
Überblick
Apache Maven ist ein Build Automation und Projektmanagement-Tool, das in erster Linie für Java-Projekte verwendet wird. Es verwendet XML-basierte Konfigurationsdateien (POM) und folgt dem Convention-over-configuration Prinzip.
Installation
Paketmanager
# macOS
brew install maven
# Ubuntu/Debian
sudo apt install maven
# CentOS/RHEL
sudo yum install maven
# Windows (Chocolatey)
choco install maven
# Manual installation
wget https://archive.apache.org/dist/maven/maven-3/3.9.0/binaries/apache-maven-3.9.0-bin.tar.gz
tar -xzf apache-maven-3.9.0-bin.tar.gz
export PATH=$PATH:/path/to/apache-maven-3.9.0/bin
```_
### Überprüfung
```bash
mvn --version
mvn -v
```_
## Projektstruktur
### Standard Directory Layout
project/ ├── pom.xml ├── src/ │ ├── main/ │ │ ├── java/ │ │ ├── resources/ │ │ └── webapp/ │ └── test/ │ ├── java/ │ └── resources/ ├── target/ └── README.md ```_
Basic POM (pom.xml)
Minimaler POM
```xml
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>My Application</name>
<description>A sample Maven project</description>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
```_
Vollständiges POM Beispiel
```xml
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>My Application</name>
<description>A comprehensive Maven project example</description>
<url>https://github.com/example/my-app</url>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.3.21</spring.version>
<junit.version>5.8.2</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>$\\\\{spring.version\\\\}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>$\\\\{junit.version\\\\}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
</plugins>
</build>
```_
Abhängigkeiten
Abhängigkeitsbereiche
```xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>custom-lib</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>$\\\\{project.basedir\\\\}/lib/custom-lib.jar</systemPath>
</dependency>
```_
Abhängigkeitsmanagement
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
```_
Ausschlüsse
xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.21</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
_
Lebenszyklus aufbauen
Default Lifecycle Phasen
```bash
Validate
mvn validate
Compile
mvn compile
Test
mvn test
Package
mvn package
Integration test
mvn integration-test
Verify
mvn verify
Install
mvn install
Deploy
mvn deploy
Clean
mvn clean ```_
Gemeinsame Phasenkombinationen
```bash
Clean and compile
mvn clean compile
Clean and package
mvn clean package
Clean, test, and install
mvn clean test install
Skip tests
mvn clean package -DskipTests
Skip test compilation
mvn clean package -Dmaven.test.skip=true ```_
Plugins
Core Plugins
```xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<includes>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M7</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.Main</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
```_
Beliebte Third-Party Plugins
```xml
Profile
Umweltprofile
```xml
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<database.url>jdbc:h2:mem:testdb</database.url>
<log.level>DEBUG</log.level>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.212</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>prod</id>
<properties>
<database.url>jdbc:mysql://prod-server:3306/mydb</database.url>
<log.level>INFO</log.level>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>test</id>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
```_
Aktivierungsbedingungen
```xml
```_
Multi-Module Projekte
Elternteil POM
```xml
<groupId>com.example</groupId>
<artifactId>my-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>My Parent Project</name>
<modules>
<module>core</module>
<module>web</module>
<module>api</module>
</modules>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.3.21</spring.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>$\\\\{spring.version\\\\}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
```_
Kindermodul POM
```xml
<parent>
<groupId>com.example</groupId>
<artifactId>my-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>core</artifactId>
<packaging>jar</packaging>
<name>Core Module</name>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
```_
Verwendung der Befehlszeile
Grundlegende Befehle
```bash
Create new project
mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Compile
mvn compile
Test
mvn test
Package
mvn package
Install to local repository
mvn install
Deploy to remote repository
mvn deploy
Clean
mvn clean ```_
Erweiterte Optionen
```bash
Activate profile
mvn clean package -Pprod
Set properties
mvn clean package -Denvironment=production -Dlog.level=INFO
Skip tests
mvn clean package -DskipTests
Offline mode
mvn clean package -o
Debug mode
mvn clean package -X
Quiet mode
mvn clean package -q
Update snapshots
mvn clean package -U
Parallel builds
mvn clean package -T 4
Resume from specific module
mvn clean package -rf :web-module ```_
Abhängigkeitsbefehle
```bash
Show dependency tree
mvn dependency:tree
Show effective POM
mvn help:effective-pom
Show effective settings
mvn help:effective-settings
Analyze dependencies
mvn dependency:analyze
Copy dependencies
mvn dependency:copy-dependencies
Resolve dependencies
mvn dependency:resolve
Show dependency sources
mvn dependency:sources
Show dependency javadocs
mvn dependency:resolve -Dclassifier=javadoc ```_
Einstellungen und Konfiguration
Einstellungen.xml (~/.m2/settings.xml)
```xml
<localRepository>$\\\\{user.home\\\\}/.m2/repository</localRepository>
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<mirrors>
<mirror>
<id>central-mirror</id>
<name>Central Repository Mirror</name>
<url>https://repo1.maven.org/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://localhost:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
```_
Repository Konfiguration
```xml
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
Best Practices
POM Organisation
```xml
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>My Application</name>
<description>Application description</description>
<url>https://github.com/example/my-app</url>
<properties>
</properties>
<dependencyManagement>
</dependencyManagement>
<dependencies>
</dependencies>
<build>
</build>
<profiles>
</profiles>
```_
Versionsmanagement
```xml
<spring.version>5.3.21</spring.version>
<junit.version>5.8.2</junit.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
Fehlerbehebung
Gemeinsame Themen
```bash
Clear local repository
rm -rf ~/.m2/repository
Force update snapshots
mvn clean package -U
Debug dependency resolution
mvn dependency:tree -Dverbose
Check for dependency conflicts
mvn dependency:analyze
Validate POM
mvn validate
Check effective POM
mvn help:effective-pom ```_
Debug Mode
```bash
Enable debug output
mvn clean package -X
Show version information
mvn --version
Show help
mvn help:help ```_
Ressourcen
- offizielle Dokumentation: maven.apache.org
- POM Referenz: [maven.apache.org/pom.html](https://_LINK_4___
- Plugin List: maven.apache.org/plugins
- *Zentral Repository: [search.maven.org](__LINK_4___