Categories: Maven

Maven Project Dependencies

For solving external libraries dependency problem Maven provides dependency management functionality for adding external libraries dependency to the project by adding some configurations into maven pom.xml file of project and then Maven downloads them for you and puts them in your local Maven repository. If any of these external libraries need other libraries, then these other libraries are also downloaded into your local Maven repository.

@ImageSource-guntherpopp.blogspot.com

You can specify your project dependencies inside the dependencies element in the POM file. Here is an example:

<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>LoggingExample</groupId>
  <artifactId>LoggingExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>

        <!--log4j2 dependencies-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.6</version>
        </dependency>
  
 <dependency>

     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.8.1</version>
     <scope>test</scope>
        </dependency>
    </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Notice above Maven POM file we have specific element dependencies and under this element there is list of dependency element with required elements groupId, artifactId and version.

The example above needs the org.apache.logging.log4j group’s log4j-api and log4j-core artifacts in version 2.6, and the junit group’s junit artifact in version 4.8.1

Now when we run this project by executing Maven POM file then Maven automatically downloaded all dependencies define into POM file from Maven central repository to Maven Local Repository. Suppose in case that required dependency already into Maven Local repository then Maven ignore to download from central repository. Only those dependencies downloaded by the maven which does not exist into maven local repository.

External Project Dependencies

Some time for any project we have requirement of Jar file which is not located in a Maven repository (neither local, central or remote repository). It may be somewhere in your computer machine’s hard disk. In this case you can configure an external dependency like this:

<dependency>
  <groupId>myexternaldependency</groupId>
  <artifactId>myexternaldependency</artifactId>
  <scope>system</scope>
  <version>1.0</version>
  <systemPath>${basedir}warWEB-INFlibmyexternaldependency.jar</systemPath>
</dependency>

Notice in above code groupId and artifactId are both set to the same name of the dependency as the name of the API used, that is. The scope element value is set to system. The systemPath element is set to point to the location of the JAR file containing the dependency

Snapshot Version Dependencies

This type of dependencies are under development. Snapshot versions are always downloaded into your local repository for every build, even if a matching snapshot version is already located in your local repository. Always downloading the snapshot dependencies assures that you always have the latest version in your local repository, for every build.

<modelVersion>4.0.0</modelVersion>
  <groupId>LoggingExample</groupId>
  <artifactId>LoggingExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>


<dependency>
    <groupId>LoggingExample</groupId>
    <artifactId>LoggingExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
   </dependency>

SNAPSHOT added with version element under the dependency in the Maven POM file.

 

Previous
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago