Explain Codes LogoExplain Codes Logo

How do I add a Maven dependency in Eclipse?

java
maven
eclipse
dependency-management
Alex KataevbyAlex Kataev·Feb 11, 2025
TLDR

To quickly add a Maven dependency in Eclipse, follow these steps:

  1. Right-click your project in the Project Explorer.
  2. Head to Maven > Add Dependency.
  3. Fill in the GroupId, ArtifactId, and Version for your desired library.

Your pom.xml gets updated:

<dependency> <groupId>your.groupid</groupId> <!-- Do I even exist --> <artifactId>your-artifact</artifactId> <!-- Who am I? --> <version>1.0.0</version> <!-- Time travelling back to v1.0.0 --> </dependency>

Hit save and Eclipse does the rest — importing and fetching the dependency.

Ensuring access to multiple repositories

In cases where a dependency is not present in Maven Central, you can set up additional repositories directly in your pom.xml:

<repositories> <repository> <id>another-repo</id> <url>http://repository.url/repo</url> <!-- I have a secret stash of jars. Don't tell Maven Central --> </repository> </repositories>

Go ahead and rebuild the Maven index in Eclipse via the Maven Repositories view, so it nods at your new entries.

Addressing potential hiccups

Adding dependencies could introduce conflicts or resolution issues:

  • Conflict: When two dependencies insist on different versions of the same sub-dependency.
    • Fix: Use dependency management or exclusions to mediate the version war.
  • Resolution Failure: When a dependency cannot be found in any repository.
    • Fix: See if you've made any typos, check for the version's availability, or confirm your internet didn't abandon you.

Addressing the elephant in the room: JARs

Sometimes, the JAR you need to use takes the road less travelled and is not available in a public repository. To counter this, you can add these JARS to your local repository using the maven-install-plugin:

mvn install:install-file -Dfile=path-to-your-artifact-jar \ -DgroupId=your.groupid \ <!-- In case you forgot me --> -DartifactId=your-artifactId \ <!-- Guess who? --> -Dversion=version \ <!-- Still at v1.0.0, time machine working fine --> -Dpackaging=jar <!-- JARred and ready! -->

Taking a snapshot

Snapshot versions of a dependency can be your best friend during early development stages:

<dependency> <groupId>your.groupid</groupId> <artifactId>your-artifactId</artifactId> <version>1.0.0-SNAPSHOT</version> <!-- It's just a phase mom, I'm growing --> </dependency>

Eclipse takes extra care with these, updating snapshot dependencies frequently so that you're not left behind.

Maven and Eclipse: An M2E love story

The integration between Maven and Eclipse via M2E simplifies a lot of processes:

  • M2E Lifecycles: Understanding the wedding ceremony of Maven lifecycles and Eclipse build phases can prevent a rough married life.
  • POM Editing: You aren't alone in this, Eclipse's POM editor provides autocomplete and syntax help.
  • Dependency Graph: Eclipse also promises to help you make sense of your project's dependency family tree.

Good-to-know tips and tricks

If you think of setting up a Maven Project in Eclipse as a game of Jenga, here's a cheat sheet:

  1. File > New > Project.
  2. Select Maven Project and simply follow the prompts. Trust the process!