Apache Maven is a popular build automation tool for Java projects that uses a POM file to manage build configuration and dependencies. One of its key features is handling external dependencies, which are libraries not included in the project but required for its execution. Maven automatically downloads these dependencies from remote repositories and adds them to the project during the build process.
- Run Maven commands by using mvn clean install to download dependencies and build the project.
- Optionally, set dependency scope (e.g., compile, test) using <scope> tags.
- Specify group ID, artifact ID, and version for each dependency.
- Declare dependencies in the project's pom.xml using <dependencies> tags.
Prerequisites:
- JDK
- Maven
- Basics of XML
- Basics of Java Programming
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:
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://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.
