The Maven cache, also called the local repository, is a folder on your system where Maven stores downloaded dependencies, plugins, and other build artifacts. This helps speed up project builds by reusing existing files instead of downloading them again.
- Stores all project dependencies and plugins locally
- Reduces build time by avoiding repeated downloads
- Located by default in ~/.m2/repository on your computer
Steps to Maven Project Setup and Cache Clearing
Follow below steps to set up a Maven project and clear the local Maven cache.
Step 1: Create a Maven Project
Use this command to create a Maven Project.
mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Step 2: Add Dependencies and Plugins to pom.xml
Here is an example pom.xml file:
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>
Step 3: Build the Project
To build the project, run the below command:
mvn clean install
Step 4: Clear the Maven Cache
To clear the Maven cache, run the below command:
mvn dependency:purge-local-repository -DreResolve=true
Step 5: Verify Project Structure
Check that the project folders are created properly (src/main/java, src/test/java).
- Ensures correct Maven standard directory layout
- Helps avoid build issues later
Step 6: Run the Application
Navigate to the main class and run it (or use command line).
mvn exec:java -Dexec.mainClass="com.example.App"
Step 7: Run Tests
- Validates application functionality
- Ensures build stability
Execute unit tests using Maven:
mvn test
Step 8: Rebuild After Cache Clear
After clearing cache, rebuild again:
mvn clean install
- Re-downloads all dependencies
- Ensures a fresh and error-free build