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 `
<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
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
Cons
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.