EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Maven Tutorial Maven Install Dependencies
 

Maven Install Dependencies

Updated June 28, 2023

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 using simple XML elements. The pom.xml file of maven is used to download the dependencies. Maven uses the installed dependencies 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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Key Takeaways

  • Basically, two types of dependencies are available in Maven, i.e., transitive and direct. The direct dependencies are nothing but what was explicitly included in the project.
  • The transitive dependencies are required in the direct dependencies; maven is automatically included in the required transitive dependencies 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.

We can also use the IntelliJ idea for adding maven dependency in our project. Maven easily included 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 directory structure from the Java source code.

How to Download Maven Install Dependencies?

The below steps show how we can download the maven install dependencies as follows:

1. While downloading the maven install dependencies, we create the project template using spring initializer. Below we are creating the template of maven install dependencies 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, 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 example below, we are adding the maven dependencies 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 open 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, 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 in the target/dependency folder. The example below shows 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 to 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 the specified location where our dependency file is generated.

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 provided at runtime by the container of JDK. Below, an 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 the 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 to indicate the specified dependency is not required during 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 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 execute the mvn clean install command to import the dependence 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 using simple XML elements. The pom.xml file of maven is used to download the dependencies.

Recommended Articles

We hope this EDUCBA information on “Maven Install Dependencies” benefited you. You can view EDUCBA’s recommended articles for more information.

  1. Maven Build Command
  2. Maven WAR Plugin
  3. Maven Profile
  4. Maven Versions

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

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

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

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

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW