Sobes.tech
Junior

What is the structure of the pom.xml file in the Maven package manager?

sobes.tech AI

Answer from AI

The root element <project> contains all other configurations.

Mandatory elements:

  • <modelVersion>: The Maven object model version (usually 4.0.0).
  • <groupId>: The artifact group identifier (e.g., com.example).
  • <artifactId>: The artifact identifier (project name).
  • <version>: The artifact version.

Other common elements:

  • <packaging>: The project packaging type (e.g., jar, war, pom).
  • <name>: The display name of the project.
  • <description>: The project description.
  • <url>: The project URL.
  • <parent>: References the parent POM for inheritance. Contains <groupId>, <artifactId>, <version>.
  • <properties>: Defines properties that can be used elsewhere in the POM.
<properties>
    <java.version>11</java.version>
    <spring.version>5.3.10</spring.version>
</properties>
  • <dependencies>: List of project dependencies. Each <dependency> contains:
    • <groupId>
    • <artifactId>
    • <version>
    • <scope>: The scope of the dependency (e.g., compile, test, provided, runtime).
    • <optional>: true or false, indicating if the dependency is optional.
    • <exclusions>: List of dependencies to exclude from transitive dependencies.
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.7.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>
  • <build>: Build configuration. Contains:
    • <finalName>: The name of the output artifact.
    • <plugins>: List of Maven plugins for various build tasks. Each <plugin> contains:
      • <groupId>
      • <artifactId>
      • <version>
      • <executions>: Plugin execution settings for specific build phases.
      • <configuration>: Plugin configuration.
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>
  • <repositories>: List of Maven repositories for dependency resolution.
  • <distributionManagement>: Repository information for deploying artifacts.
  • <profiles>: Define different build configurations activated under certain conditions.
  • <modules>: Used in pom type POMs for aggregating child modules.

Main structure:

<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-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>My Awesome Project</name>
    <description>A sample Maven project</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <!-- Dependencies listed here -->
    </dependencies>

    <build>
        <!-- Build configuration here -->
    </build>

    <!-- Other elements can be added here -->

</project>