Agentic AI with Java: Live Cohort
MavenDependencies, POM & IDEs

Maven Dependencies

Add a third party library such as spring-core to pom.xml, watch Maven download it from Maven Central, and learn dependency scopes, transitive dependencies and mvn dependency:tree.

Almost no real application is written from scratch. You use frameworks and libraries written by other teams: Spring for application structure, Hibernate for databases, JUnit for testing, a Kafka or Redis client for messaging. Each of these is someone else's code, packaged as one or more JAR files full of classes, interfaces and annotations you want to use.

Without Maven you would search the internet, download those JARs and add them to your build path by hand (we saw that pain in Why and what is build tool). With Maven you write a few lines in pom.xml and Maven does the rest. Those libraries are called dependencies: things your project depends on to compile or run.

Step 1: find the dependency

Search for the library on a Maven repository search site. Two common ones:

SiteWhat it is
central.sonatype.comThe official search for Maven Central, run by Sonatype, the steward of Central
mvnrepository.comA popular third party index of Central and other repositories. The video uses this one

In the demo we search for spring-core, open it, pick version 6.2.8, and copy the Maven snippet:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>6.2.8</version>
</dependency>

A dependency is identified by the same GAV coordinates you learned in Maven Terminologies: org.springframework:spring-core:6.2.8.

Picking a version

Prefer the newest stable release of the line your project uses. Skip versions with -M1, -RC1 or -SNAPSHOT, which are milestones, release candidates and development builds. Both search sites also show known vulnerabilities for each version, which is worth a glance before you choose.

Step 2: add it to pom.xml

Open telusko-app/pom.xml in any editor or IDE. Find the <dependencies> section, which already contains JUnit, and paste the new <dependency> inside it, next to the existing one:

pom.xml
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>6.2.8</version>
  </dependency>
</dependencies>
pom.xml open in IntelliJ IDEA with the junit dependency and the new spring-core 6.2.8 dependency inside the dependencies element

The spring-core dependency pasted inside <dependencies>, right after JUnit. (This is the archetype 1.0 POM. If you used archetype 1.5, your JUnit entries look different, and that is fine.)

The meaning of these lines is simply: "Maven, this project depends on spring-core 6.2.8, please get it for me." Save the file.

Spring 6 needs Java 17

Spring Framework 6 is compiled for Java 17. If your POM still compiles for Java 8, or your JDK is older than 17, the build fails as soon as your code uses Spring classes. Make sure your project targets Java 17 or newer, as set up in Maven Goals.

Step 3: build and watch the download

From the project folder:

mvn clean
mvn package

(or mvn clean package in one go). Near the top of the output you now see lines like:

Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/6.2.8/spring-core-6.2.8.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/6.2.8/spring-core-6.2.8.jar (2.0 MB at ...)
...
[INFO] BUILD SUCCESS

Maven fetched the JAR from Maven Central and put it on the project's classpath. If you open the project in an IDE, you can now import org.springframework... classes and they compile. You did not download or copy anything yourself.

You can add as many dependencies as you need: Spring, Spring Security, Hibernate, JUnit, Kafka, Redis clients. Each one is another <dependency> block inside the same <dependencies> element.

Transitive dependencies: the ones you did not ask for

Libraries have their own dependencies. spring-core 6.2.x needs spring-jcl (Spring's logging bridge). You never declared it, but Maven reads spring-core's own POM, sees that requirement, and downloads it too. These are transitive dependencies.

telusko-app depends directly on spring-core and junit; spring-core brings spring-jcl as a transitive dependency

See the full tree at any time:

mvn dependency:tree
[INFO] com.telusko:telusko-app:jar:1.0-SNAPSHOT
[INFO] +- junit:junit:jar:3.8.1:test
[INFO] \- org.springframework:spring-core:jar:6.2.8:compile
[INFO]    \- org.springframework:spring-jcl:jar:6.2.8:compile

This command is your best friend when two libraries pull in different versions of the same JAR. When that happens Maven picks the version nearest to your project in the tree. If you need a specific version, declare it directly in your own POM, and that declaration wins.

Dependency scopes

The <scope> element controls where a dependency is available. If you leave it out, the scope is compile.

ScopeAvailable whenPackaged in WAR / fat JAR?Typical example
compile (default)Compiling, testing, runningYesSpring, Jackson
testOnly compiling and running testsNoJUnit, Mockito
providedCompiling and testing. The runtime environment supplies itNoServlet API (Tomcat already has it)
runtimeTesting and running, not compilingYesJDBC drivers such as MySQL Connector/J
systemLike provided, but from a file path on your diskNoAvoid. Legacy only
importOnly for importing a BOM in <dependencyManagement>Not applicableSpring Boot or JUnit BOMs

That is why JUnit has <scope>test</scope>: you need it to run tests, but it must never ship inside your production JAR.

Common problems

Try It Yourself

  1. Add spring-core 6.2.8 to telusko-app, run mvn clean package, and find the Downloading from central lines for it.
  2. Run mvn dependency:tree and identify the transitive dependency spring-core brought in.
  3. Add com.mysql:mysql-connector-j with <scope>runtime</scope> and run mvn dependency:tree again. Note the scope shown at the end of the line.
  4. Introduce a typo in the version (for example 6.2.88), build, and read the error. Fix it.

Official references


Written By: Shramik Masti

How is this guide?

Last updated on