7 Einführung in das Projekt Objektmodell

Das Projekt Objektmodell (POM) ist das zentrale Element eines Maven-Projekts. Es definiert grundlegende Informationen und Konfigurationen, die den gesamten Build-Prozess steuern. Üblicherweise ist das POM in XML verfasst und enthält verschiedene Tags, die spezifische Aspekte des Projekts beschreiben.

7.1 Beispiel POM

<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/xsd/maven-4.0.0.xsd">

    <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>Ein Beispielprojekt zur Demonstration von Maven</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <!-- Weitere Abhängigkeiten können hier hinzugefügt werden -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
            <!-- Weitere Plugins können hier hinzugefügt werden -->
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>central</id>
            <url>https://repo.maven.apache.org/maven2</url>
        </repository>
    </repositories>

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <environment>development</environment>
            </properties>
            <build>
                <plugins>
                    <!-- Plugins spezifisch für das Entwicklungsprofil -->
                </plugins>
            </build>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <environment>production</environment>
            </properties>
            <build>
                <plugins>
                    <!-- Plugins spezifisch für das Produktionsprofil -->
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

7.2 Erklärung der Hauptfelder

7.2.1 Group ID

Identifiziert die Organisation oder Gruppe, die das Projekt entwickelt (z.B. com.example). Der Name sollte domänenbasiert und eindeutig sein.

7.2.2 Artifact ID

Name des Projekts oder Moduls (z.B. my-app). Sollte innerhalb der Gruppe eindeutig sein.

7.2.3 Version

Gibt die Projektversion an (z.B. 1.0.0). Ermöglicht die Verwaltung mehrerer Versionen desselben Artefakts.

7.2.4 Packaging

Definiert das Ausgabeformat des Projekts (z.B. jar, war oder pom). Im Beispiel ist jar gesetzt.

7.3 Umfassende Erklärung der weiteren Elemente

7.3.1 Properties

Enthält projektspezifische Einstellungen, die in anderen Bereichen des POM referenziert werden. Im Beispiel wird die Quellcode-Kodierung und Java-Version festgelegt:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

7.3.2 Dependencies

Listet alle benötigten Bibliotheken und Frameworks auf. Jede Abhängigkeit wird über groupId, artifactId und version definiert, optional ergänzt durch scope:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>

7.3.3 Repositories

Gibt an, in welchen Repositories Maven nach Abhängigkeiten sucht. Standardmäßig ist Maven Central konfiguriert:

<repository>
    <id>central</id>
    <url>https://repo.maven.apache.org/maven2</url>
</repository>

7.3.4 Profiles

Ermöglicht das Anlegen unterschiedlicher Build-Konfigurationen, beispielsweise für Entwicklungs- und Produktionsumgebungen:

<profile>
    <id>dev</id>
    <properties>
        <environment>development</environment>
    </properties>
</profile>

7.3.5 Build

Umfasst Konfigurationen für den Build-Prozess, insbesondere Plugins und deren Einstellungen:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

7.3.6 Name und Description

Zusätzliche Metadaten, die das Projekt näher beschreiben:

<name>My Application</name>
<description>Ein Beispielprojekt zur Demonstration von Maven</description>

Dieses Zusammenspiel der POM-Elemente standardisiert und vereinfacht den gesamten Build-Prozess in Maven.