Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Managing Dependencies | Core Maven Concepts
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Introduction to Maven

bookManaging Dependencies

Understanding Dependencies in Maven

Dependencies are external libraries or modules that your project needs to compile, run, or test successfully. In Maven, you declare these dependencies so that Maven can automatically download and manage them for you.

Why Dependencies Matter

  • Save time by reusing existing code instead of writing everything from scratch;
  • Avoid compatibility issues by using tested, well-maintained libraries;
  • Make your project easier to maintain and share with others;
  • Ensure consistent builds by always using the same library versions.

How Maven Resolves Dependencies

When you specify dependencies in your Maven project, Maven checks its local repository (a folder on your computer) to see if the required libraries are already downloaded. If not, Maven downloads them from a central online repository. Maven also manages transitive dependencies — libraries that your dependencies need — so you do not have to track them manually.

This automatic process makes it much easier to manage complex projects and ensures you always have the right libraries for your application.

pom.xml

pom.xml

copy
123456789101112131415161718192021222324252627282930313233343536
<!-- This is a minimal pom.xml file for a Maven project. It defines the project's coordinates and demonstrates how to add a dependency. --> <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"> <!-- The model version should always be 4.0.0 for Maven 3+ projects --> <modelVersion>4.0.0</modelVersion> <!-- The groupId uniquely identifies your project across all projects --> <groupId>com.example</groupId> <!-- The artifactId is the name of the jar without version --> <artifactId>dependency-demo</artifactId> <!-- The version of your project --> <version>1.0.0</version> <!-- The dependencies section is where you list libraries your project needs --> <dependencies> <!-- This dependency adds JUnit Jupiter for unit testing. Maven will automatically download it when you build the project. --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.9.2</version> <scope>test</scope> <!-- Only used during testing --> </dependency> </dependencies> </project>

Key Elements of a Dependency

When you add a dependency to your Maven project, you use the <dependency> tag in your pom.xml file. Each dependency is defined by three essential elements:

  • groupId: identifies the organization or group that created the project; this is often a reversed domain name, such as org.apache.maven.plugins;
  • artifactId: specifies the name of the project or library you want to use; this is a unique name within the group, such as junit-jupiter;
  • version: indicates the exact release of the library you want to include; this ensures you are using the correct and compatible code, such as 5.9.2.

Your dependency entry in pom.xml will look like this:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.9.2</version>
</dependency>

These three elements together tell Maven exactly which library to download and use in your project.

Transitive Dependencies in Maven

When you add a dependency to your Maven project, that dependency might itself rely on other libraries. These extra libraries are called transitive dependencies.

Maven automatically finds and includes all transitive dependencies your project needs. You do not have to manually add each one. This helps keep your pom.xml file clean and saves you time.

Example:

  • You add a library A as a dependency;
  • Library A depends on another library B;
  • Maven will include both A and B in your project automatically.

This process ensures your project has everything it needs to work, even when libraries depend on each other.

question mark

What is a Maven dependency?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookManaging Dependencies

Svep för att visa menyn

Understanding Dependencies in Maven

Dependencies are external libraries or modules that your project needs to compile, run, or test successfully. In Maven, you declare these dependencies so that Maven can automatically download and manage them for you.

Why Dependencies Matter

  • Save time by reusing existing code instead of writing everything from scratch;
  • Avoid compatibility issues by using tested, well-maintained libraries;
  • Make your project easier to maintain and share with others;
  • Ensure consistent builds by always using the same library versions.

How Maven Resolves Dependencies

When you specify dependencies in your Maven project, Maven checks its local repository (a folder on your computer) to see if the required libraries are already downloaded. If not, Maven downloads them from a central online repository. Maven also manages transitive dependencies — libraries that your dependencies need — so you do not have to track them manually.

This automatic process makes it much easier to manage complex projects and ensures you always have the right libraries for your application.

pom.xml

pom.xml

copy
123456789101112131415161718192021222324252627282930313233343536
<!-- This is a minimal pom.xml file for a Maven project. It defines the project's coordinates and demonstrates how to add a dependency. --> <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"> <!-- The model version should always be 4.0.0 for Maven 3+ projects --> <modelVersion>4.0.0</modelVersion> <!-- The groupId uniquely identifies your project across all projects --> <groupId>com.example</groupId> <!-- The artifactId is the name of the jar without version --> <artifactId>dependency-demo</artifactId> <!-- The version of your project --> <version>1.0.0</version> <!-- The dependencies section is where you list libraries your project needs --> <dependencies> <!-- This dependency adds JUnit Jupiter for unit testing. Maven will automatically download it when you build the project. --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.9.2</version> <scope>test</scope> <!-- Only used during testing --> </dependency> </dependencies> </project>

Key Elements of a Dependency

When you add a dependency to your Maven project, you use the <dependency> tag in your pom.xml file. Each dependency is defined by three essential elements:

  • groupId: identifies the organization or group that created the project; this is often a reversed domain name, such as org.apache.maven.plugins;
  • artifactId: specifies the name of the project or library you want to use; this is a unique name within the group, such as junit-jupiter;
  • version: indicates the exact release of the library you want to include; this ensures you are using the correct and compatible code, such as 5.9.2.

Your dependency entry in pom.xml will look like this:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.9.2</version>
</dependency>

These three elements together tell Maven exactly which library to download and use in your project.

Transitive Dependencies in Maven

When you add a dependency to your Maven project, that dependency might itself rely on other libraries. These extra libraries are called transitive dependencies.

Maven automatically finds and includes all transitive dependencies your project needs. You do not have to manually add each one. This helps keep your pom.xml file clean and saves you time.

Example:

  • You add a library A as a dependency;
  • Library A depends on another library B;
  • Maven will include both A and B in your project automatically.

This process ensures your project has everything it needs to work, even when libraries depend on each other.

question mark

What is a Maven dependency?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 1
some-alt