Home > Tutorial > maven > Maven Add Dependency

Maven Add Dependency

What is a dependency in terms of a Java Maven project? A dependency is a packaged set of classes that your Java project depends on. The dependency is usually a jar file and is defined in your pom.xml file.

Maven Dependency Pom.xml

When you created your first Maven project, a default pom.xml was created for you by Maven that looks like this. By default, Maven will download from the central repository. In this pom.xml file, Mave is going to the central repository and downloading junit version 3.8.1 package to your local repository. If you look in your .m2 Maven installation path, you will see this folder.

<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.jcd.java.tutorials</groupId>
  <artifactId>MavenProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MavenProject</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency> 
  </dependencies>
</project>


Add Dependency Package

So let’s say your Java project needs to use a class from the Apache Commons Language package. In a non-Maven Java project, you would need to download the package and and it to your build path. With a Maven project, you include the required dependency package in your pom.xml file,

Step 1 – Add Dependency to pom.xml

Our Java project needs to use the Apache Commons Language package (version 3.7) so all we need to do is add it to our pom.xml file and Maven does the rest. Make sure to place our dependency between the parent dependencies opening and closing tags.

<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.jcd.java.tutorials</groupId>
  <artifactId>MavenProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MavenProject</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
    </dependency> 
    
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.7</version>
    </dependency> 
    
  </dependencies>
</project>


Step 2 – Verify commons-lang3 package in local repository

Based on your Maven installation location, you can verify the commons-lang3 package was downloaded from Maven Central Repository to your local Maven Repository.



Step 3 – Verify Java Package Usage

Open the Default Java App.java file that was created when you created a new Maven Project and verify you are able to import the common-lang3 package and use it in your default Java application.

package com.jcd.java.tutorials.MavenProject;

import org.apache.commons.lang3.StringUtils;

public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        
        String myString = "JAVACODEDEPOT.COM LOVES JAVA PRGRAMMING";
        
        int ctr = StringUtils.countMatches(myString, "JAVA");
        
        System.out.println("Found 'JAVA' This Many Times: "+ctr);
    }
}



Concluding Thoughts

Anytime your Java application needs to include and use a new package, simply add a new dependency to your pom.xml file and Maven will handle retrieving it and making it available in your local Maven repository. The ability to quickly add required packages to the pom.xml file is one of the main benefits of building your Java project with Maven.



References:

Smedley
Posted by

Smedley

Smedley has been programming in Java for about twenty years. He enjoys sharing code samples to other fellow Java coders.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot