EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Maven Install Dependencies

Secondary Sidebar
Maven Tutorial
  • Maven Basic and advance
    • What is Maven
    • How to Install Maven
    • Maven Commands
    • Maven GroupId
    • Maven WAR Plugin
    • Maven Build Command
    • Maven Failsafe Plugin
    • Maven Profile
    • IntelliJ Maven
    • Maven Enforcer Plugin
    • Maven Javadoc Plugin
    • Maven WAR Plugin
    • Maven Build Command
    • Maven GroupId
    • Maven Force Update
    • Maven Encrypt Password
    • Maven Environment Variables
    • AssertJ Maven
    • Maven Run Single Test
    • Maven kafka-clients
    • Maven Quickstart Archetype
    • Maven Install Dependencies
    • Maven XMLBeans
    • Maven Local Repository
    • Maven Versions
    • Maven Jar Plugin
    • Maven Assembly Plugin
    • Maven exec plugin
    • Maven Central Repository
    • Maven Surefire
    • Maven Deploy
    • Maven Phases
    • Maven Archetype
    • Maven Skip Test
    • Maven Dependency Scope
    • Maven Shade Plugin
    • Maven Repository Spring
    • Maven Eclipse Plugin
    • Maven Exclude Dependency
    • Maven Life Cycle
    • Maven Repository
    • Maven POM File
    • Maven Plugins
    • What is Maven Plugins
    • Maven Interview Questions
    • Maven Flags
    • Maven Project
    • Maven Settings.XML
Home Software Development Software Development Tutorials Maven Tutorial Maven Install Dependencies

Maven Install Dependencies

Introduction to Maven Install Dependencies

Maven install dependencies are required in the maven project. Maven contains a good mechanism for describing dependencies in our project, by using simple elements of XML. The pom.xml file of maven is used to download the dependencies. The installed dependencies are used by maven at the time of code compilation for direct dependent classes which were available for the compiler. Maven dependencies are required to run the project of maven.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,064 ratings)

Key Takeaways

  • Basically, there are two types of dependencies available in maven i.e. transitive and direct. The direct dependencies are nothing but which was explicitly included in the project.
  • The transitive dependencies are required in the direct dependencies, maven is automatically included in the required dependencies of transitive in a specified project.

Overview of Maven Install Dependencies

To install the dependencies of maven we need to execute the mvn dependency command which will install all the dependencies. We can use the maven dependency plugin for downloading the dependencies. We can change the target location by setting the property of the output directory. We are running mvn dependency with copy dependency for downloading all the dependencies.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

We can also use the intellij idea for adding maven dependency in our project. Maven easily include the third-party dependencies in our project, which was equivalent to the other languages like PHP or ruby. By using maven we can build the java project with the classes. Maven will be expecting the structure of the directory from the java source code.

How to Download Maven Install Dependencies?

Below steps shows how we can download the maven install dependencies as follows:

1. While downloading the maven install dependencies we are creating the template of the project by using spring initializer. Below we are creating the template of maven install dependencies by using spring initializer.

Group name – com.example
Artifact – maven_dependencies
Name – maven_dependencies
Packaging – jar
Java version – 8
Language – java

Maven Install Dependencies 1

After creating the template of maven install dependencies into spring initializer now, we need to download and extract the project template.

Extract the project template

2. In this step we are checking the project structure and pom.xml file. The project structure is as follows.

project structure and pom.xml file

3. In the below example we are adding the maven dependencies which we require in our project into the pom.xml file as follows.

Code:

<dependency>
  <groupId> org.springframework.boot </groupId>
  <artifactId> spring-boot-starter-web </artifactId>
</dependency>
<dependency>
  <groupId> org.springframework.boot </groupId>
  <artifactId> spring-boot-starter-test </artifactId>
<scope> test </scope>
</dependency>
<dependency>
  <groupId> junit </groupId>
  <artifactId> junit </artifactId>
  <version> 4.12 </version>
  <scope> test </scope>
</dependency>

Output:

Adding the dependencies

4. After adding the dependency in the below example we are opening the terminal where our pom.xml file is located. After opening the terminal we are executing the following command as follows.

Code:

mvn dependency:copy-dependencies

Output:

Opening the terminal

Maven Install Dependencies 6

5. In the above example we can see that all the dependencies are downloaded and copied into the target folder. After executing the above command we can see that all the dependency is installed into the target/dependency folder. In the below example, we can see that all the dependency is downloaded in the specified folder as follows.

Maven Install Dependencies 7

Maven Install Dependencies 8

How to Install Dependencies for Maven?

In the below example we are installing junit dependencies for maven as follows. For installing the junit dependency we need to add the dependency into the pom.xml file. We are adding the junit dependency into the pom.xml file as follows.

Code:

<dependency>
  <groupId> junit </groupId>
  <artifactId> junit-maven </artifactId>
  <version> 4.12 </version>
  <scope> maven-test </scope>
</dependency>

Output:

Maven Install Dependencies 9

After adding the dependency we are executing the below command to install the dependencies for maven as follows. First, it will download the dependencies then all dependencies are copied to the specified directory.

Code:

mvn dependency:copy-dependencies

Output:

Maven Install Dependencies 10

Maven Install Dependencies 11

In the below example, we are downloading and installing the dependency into the specified location. We can see that we are downloading the dependency in /mnt/c/dependency location as follows.

Code:

mvn dependency:copy-dependencies –DoutputDirectory = /mnt/c/dependency

Output:

Maven Install Dependencies 12

Maven Install Dependencies 13

After downloading and installing the dependency we can check that specified location where our dependency file is generated as follows.

Maven Install Dependencies 14

Maven Dependencies for Scope

Dependency scope helps us to limit the transitivity dependency. It is modifying the classpath for the different task build. Below is the default scope where no other scope is provided.

Code:

<dependency>
  <groupId> commons-lang </groupId>
  <artifactId> commons-lang </artifactId>
  <version> 2.6 </version>
</dependency>

Output:

Maven Install Dependencies 15

The provided scope is used to mark the dependencies which was provided at runtime by the container of JDK. Below example of provided scope is as follows.

Code:

<dependency>
  <groupId> javax.servlet </groupId>
  <artifactId> javax.servlet-api </artifactId>
  <version> 4.0.1 </version>
  <scope> provided </scope>
</dependency>

Output:

Container of JDK

The dependency with runtime scope is required at runtime. But we have not required the same at compile time as follows.

Code:

<dependency>
  <groupId> mysql </groupId>
  <artifactId> mysql-connector-java </artifactId>
  <version> 8.0.28 </version>
  <scope> runtime </scope>
</dependency>

Output:

Maven Install Dependencies 17

We use the test scope for indicating the specified dependency is not required while runtime.

Code:

<dependency>
  <groupId> junit </groupId>
  <artifactId> junit </artifactId>
  <version> 4.12 </version>
  <scope> test </scope>
</dependency>

Output:

Maven Install Dependencies 18

The scope of the system is similar to the provided scope. The system scope requires a direct point.

Code:

<dependency>
  <groupId> com.baeldung </groupId>
  <artifactId> custom-dependency </artifactId>
  <version> 1.3.2 </version>
  <scope> system </scope>
  ……
</dependency>

Output:

similar scope of system

Import Maven Dependencies

Import will indicate that the dependency will be replaced with the effective dependency which was declared in a POM. In the below example, we are replacing the custom project dependency in a pom.xml file.

Code:

<dependency>
  <groupId> com.baeldung </groupId>
  <artifactId> custom-project </artifactId>
  <version> 1.3.2 </version>
  <type> pom </type>
  <scope> import </scope>
</dependency>

Output:

Maven Install Dependencies 20

After adding the dependency, we need to go into the project folder and need to execute the mvn clean install command for importing the dependency which we have added. In the below example, we are running this command from the command line.

Code:

mvn clean install

Output:

Maven Install Dependencies 21

In the below example we are importing the maven dependencies using the spring tool suite.

Maven Install Dependencies 22

Conclusion

To install the dependencies of maven we need to execute the mvn dependency command which will install all the dependencies. Maven contains a good mechanism for describing dependencies in our project, by using simple elements of XML. The pom.xml file of maven is used to download the dependencies.

Recommended Articles

This is a guide to Maven Install Dependencies. Here we discuss the introduction, and how to download maven install dependencies. and maven dependencies for scope. You may also have a look at the following articles to learn more –

  1. Maven Build Command
  2. Maven WAR Plugin
  3. Maven Profile
  4. Maven Versions
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More