Example of External Dependencies in Maven

Here, we created a sample maven project by using Spring Tool Suite IDE with required dependencies.

Step 1: Setup Maven Project and Dependencies

First, we created a maven project. Below we provide required dependencies and Project folder structure for your reference.

Dependencies:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.shared</groupId>
            <artifactId>maven-invoker</artifactId>
            <version>3.0.1</version>
        </dependency>

    </dependencies>

Project Folder Structure:


Step 2: Update Project Configuration (pom.xml)

Once the project created successfully, then update pom.xml file. Below we provide the example pom.xml file.

Here we take a external dependency,

 <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
  </dependency>


pom.xml:

XML
<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.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
         <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
    </dependencies>
</project>


Step 3: Run Maven Build Process

Now update the project, after this, start the maven build process by running the maven project.

Use below maven command:

 mvn clean install


Output:


Maven Build Successfully,


Step 4: Verify Project Execution

Once project is successfully running, then this project run on port number on 8080 with tomcat server by default.




Maven – External Dependencies

Apache Maven is a powerful and famous Build automation Tool. It is primarily used for Java-based projects. Maven uses a Project Object Model to manage project build information, project dependencies, project configuration, and project documentation. Handling External Dependencies is one of Maven’s core features, and the POM file serves as the central component of a Maven project.

External dependencies in Maven refer to the libraries or packages that your project relies on but are not part of your project’s source code. Maven manages these dependencies by downloading them from remote repositories and adding them to your project’s classpath during the build process. In this article, we will learn about External Dependencies in the Maven tool.

Key Concepts of Maven External Dependencies:

  • Declare dependencies in the project’s pom.xml using <dependencies> tags.
  • Specify group ID, artifact ID, and version for each dependency.
  • Optionally, set dependency scope (e.g., compile, test) using <scope> tags.
  • Run Maven commands by using mvn clean install to download dependencies and build the project.

Tools and Technologies:

Below we provide the required tools and technologies to understand this article.

  • Java Programming
  • Apache Maven Tool
  • Integrated Development Environment
  • Maven Central Repository

Prerequisites:

  • JDK
  • Maven
  • Basics of XML
  • Basics of Java Programming

Similar Reads

Example of External Dependencies in Maven

Here, we created a sample maven project by using Spring Tool Suite IDE with required dependencies....

Contact Us