Maven Project Structure

 

What is a Maven Project?

Maven project is a structured Java-based software project that uses Apache Maven as its build automation and dependency management tool. It is defined using a file called pom.xml (Project Object Model), which describes the project, its dependencies, build plugins, goals, and other configuration.


Standard Maven Project Folder Structure

Here's the default Maven directory structure (used in most Java projects):

project-root/

├── pom.xml                 # Main configuration file (Project Object Model)
├── src/
   ├── main/
      ├── java/           # Application/Source code
      ├── resources/      # Configuration files, properties files, XMLs
   ├── test/
      ├── java/           # Unit and integration test code
      ├── resources/      # Resources needed for testing

├── target/                 # Output directory for compiled classes and built artifacts (e.g., .jar/.war)
└── .mvn/                   # Maven wrapper files (optional)



Description of Important Folders and Files


Folder/FileDescription
pom.xmlCore file. Contains project info, dependencies, plugins, build configurations, etc.
src/main/java/Java source code for the application.
src/main/resources/Non-code resources like application.properties, log configs, XMLs.
src/test/java/Java unit or integration test source code.
src/test/resources/Resource files used for testing (e.g., test configs or mock data).
target/Auto-generated folder. Stores compiled classes, JAR/WAR files, and reports.
.mvn/Optional folder for Maven wrapper support (mvnw, mvnw.cmd, .mvn/wrapper).


Build Lifecycle Overview

When you run a Maven build (mvn clean install), Maven:

  • Compiles your source code
  • Runs tests in src/test/java
  • Packages the code into a .jar or .war file
  • Installs it into your local Maven repository

pom.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>myproject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <!-- Example dependency -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>