Saturday, December 27, 2025

thumbnail

How to Use Maven in a Java Project

 1. What Maven Does (Quick Overview)


Maven helps you:


Manage dependencies (external libraries)


Standardize project structure


Automate build lifecycle (compile, test, package, deploy)


Ensure repeatable builds


2. Install Maven

Check if Maven is installed

mvn -version


If not installed


Install from Apache Maven website


Ensure JAVA_HOME is set


Add Maven to your system PATH


3. Create a New Maven Project

Using Maven Archetype

mvn archetype:generate



Common archetype:


mvn archetype:generate \

  -DgroupId=com.example \

  -DartifactId=my-app \

  -DarchetypeArtifactId=maven-archetype-quickstart \

  -DinteractiveMode=false


4. Standard Maven Project Structure

my-app/

 ├── pom.xml

 └── src/

     ├── main/

     │   └── java/

     │       └── com/example/App.java

     └── test/

         └── java/

             └── com/example/AppTest.java



Maven expects this structure automatically.


5. Understanding pom.xml


The POM (Project Object Model) is the heart of a Maven project.


Basic pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0">

  <modelVersion>4.0.0</modelVersion>


  <groupId>com.example</groupId>

  <artifactId>my-app</artifactId>

  <version>1.0.0</version>


  <properties>

    <maven.compiler.source>17</maven.compiler.source>

    <maven.compiler.target>17</maven.compiler.target>

  </properties>


</project>


6. Adding Dependencies


Example: Add JUnit for testing


<dependencies>

  <dependency>

    <groupId>org.junit.jupiter</groupId>

    <artifactId>junit-jupiter</artifactId>

    <version>5.10.0</version>

    <scope>test</scope>

  </dependency>

</dependencies>



Maven automatically:


Downloads dependencies


Handles versions


Manages transitive dependencies


7. Common Maven Commands

Compile the project

mvn compile


Run tests

mvn test


Package the application (JAR/WAR)

mvn package


Clean build artifacts

mvn clean


Full build

mvn clean install


8. Maven Build Lifecycle (Simplified)

Phase What it Does

validate Check project structure

compile Compile source code

test Run unit tests

package Create JAR/WAR

install Install to local repo

deploy Deploy to remote repo


Running a later phase automatically runs earlier ones.


9. Running a Java App with Maven


Using the exec plugin:


<build>

  <plugins>

    <plugin>

      <groupId>org.codehaus.mojo</groupId>

      <artifactId>exec-maven-plugin</artifactId>

      <version>3.1.0</version>

      <configuration>

        <mainClass>com.example.App</mainClass>

      </configuration>

    </plugin>

  </plugins>

</build>



Run:


mvn exec:java


10. Local Repository


Maven stores downloaded dependencies in:


~/.m2/repository



This avoids downloading the same libraries repeatedly.


11. Best Practices


✅ Use specific dependency versions

✅ Prefer Maven Central dependencies

✅ Keep pom.xml clean and readable

✅ Use profiles for environment-specific builds

✅ Avoid unnecessary plugins


12. When to Use Maven vs Gradle

Maven Gradle

XML-based Groovy/Kotlin-based

Convention over configuration More flexible

Easier for beginners More powerful for complex builds

Learn Full Stack JAVA Course in Hyderabad

Read More

Maven vs Gradle – Build Tools Compared

Git Commands Every Developer Should Know

Getting Started with Git and GitHub

๐Ÿ› ️ Tools & Version Control

Visit Our Quality Thought Institute in Hyderabad

Get Directions 

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive