Java > Java Build Tools > Maven > POM Files

POM File with Multiple Dependencies and Plugins

This snippet expands upon the basic POM file by demonstrating how to include multiple dependencies, configure a build plugin (shade plugin), and define a custom repository.

POM Structure with Dependencies, Plugins, and Repository

This `pom.xml` file demonstrates more advanced Maven features: * Multiple Dependencies: Includes dependencies for SLF4J API, Logback Classic (logging framework), and Gson (JSON processing library). Notice the `scope` is explicitly set to `runtime` for Logback. * Maven Shade Plugin: This plugin is configured to create a shaded JAR, which includes all dependencies within a single JAR file. This is useful for creating executable JARs. * Custom Repository: The `` section defines a custom Maven repository. This is useful when dependencies are not available in the central Maven repository. * Properties for Compiler versions This example shows the usage of properties to define maven compiler source and target versions. These can be overriden via command line.

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

    <name>Complex Application</name>

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

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>my-repo</id>
            <url>http://example.com/maven/repo</url>
        </repository>
    </repositories>

</project>

Concepts Behind the Snippet

This snippet builds upon the fundamental concepts of Maven and introduces additional features like plugins and repositories. Plugins extend Maven's functionality, while repositories provide locations to download dependencies.

Real-Life Use Case Section

Let's say you have an application that needs to be deployed as a single executable JAR file, including all its dependencies. The Maven Shade Plugin allows you to create this shaded JAR easily. Also, if some of your internal libraries are not available in Maven Central, you can create a custom repository and define it in your POM file.

Best Practices

  • Choose the right plugins: Select plugins that are well-maintained and have a large community.
  • Configure plugins carefully: Review the plugin documentation to understand all the available configuration options.
  • Use a repository manager: Tools like Nexus or Artifactory can help you manage your dependencies and repositories.

Interview Tip

Be prepared to explain how plugins work in Maven and how they extend Maven's functionality. Also, understand the concept of Maven repositories and how Maven resolves dependencies from different repositories.

When to Use Them

Use this more complex POM structure when your project requires multiple dependencies, needs custom build steps, or relies on dependencies from custom repositories.

Alternatives

For creating executable JARs, alternatives to the Shade plugin include the Assembly plugin. Custom repositories can be replaced by using local file-based repositories, although this is generally discouraged for team-based projects.

Pros

  • Flexibility: Maven plugins provide a high degree of flexibility for customizing the build process.
  • Reusability: Plugins can be reused across multiple projects.
  • Dependency Isolation: Shaded JARs can help isolate dependencies and prevent conflicts.

Cons

  • Plugin Configuration Complexity: Configuring plugins can be complex and require a good understanding of the plugin's documentation.
  • Repository Management Overhead: Managing custom repositories can add overhead to the build process.
  • Shaded JAR Size: Shaded JARs can be large due to the inclusion of all dependencies.

FAQ

  • What is a shaded JAR?

    A shaded JAR is a JAR file that contains all of its dependencies within it. This is useful for creating executable JARs that can be run without requiring external dependencies.
  • How does Maven resolve dependencies when multiple repositories are defined?

    Maven searches repositories in the order they are defined in the POM file. If a dependency is found in multiple repositories, Maven will use the first one it finds.