Apache Maven Standard Directory Layout

Apache Maven follows a standard directory layout for organizing project files. This layout provides a structured and consistent way to manage project resources, making it easier for developers to navigate and maintain projects. Understanding and using the standard directory layout is crucial for effective Maven project management. In this blog post, we'll explore the key components of the Maven standard directory layout and explain the purpose of each directory and file.

Overview of the Standard Directory Layout

Here's an overview of the standard directory layout in a Maven project:

my-maven-project
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   ├── resources
│   │   ├── filters
│   │   ├── assembly
│   │   ├── config
│   │   ├── scripts
│   │   └── webapp
│   │       ├── WEB-INF
│   │       └── META-INF
│   └── test
│       ├── java
│       ├── resources
│       └── filters
└── target

Let's dive into each part of the directory layout and understand its purpose.

1. pom.xml

The pom.xml file is the core of a Maven project. It contains project configuration information such as dependencies, plugins, build configurations, and other settings. Here's a basic example of a pom.xml file:

<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-maven-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>my-maven-project</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.10.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

2. src Directory

The src directory is where all the source code, resources, and other files are stored. It contains two main subdirectories: main and test.

src/main

This directory contains the main source code and resources of the project.

  • java: This directory contains the Java source files.
  • resources: This directory contains non-Java resources, such as configuration files, properties files, and other static resources.
  • filters: This directory contains filter files used during the resource filtering process.
  • assembly: This directory contains assembly descriptors, which define how the project should be assembled into a distribution package.
  • config: This directory contains configuration files.
  • scripts: This directory contains scripts used by the project.
  • webapp: This directory contains web application resources. It is structured similarly to a WAR file.

src/test

This directory contains the test source code and resources.

  • java: This directory contains the Java test files.
  • resources: This directory contains non-Java test resources.
  • filters: This directory contains filter files used during the resource filtering process.

3. target Directory

The target directory is where Maven stores the output of the build process. This includes compiled classes, JAR files, WAR files, and other generated resources. The target directory is created during the build process and is typically not included in version control.

Detailed Explanation of Key Directories

src/main/java

This directory contains the main Java source code for the project. The package structure within this directory should follow the standard Java package naming conventions.

src/main/resources

This directory contains non-Java resources required by the application. These resources are copied to the classpath during the build process and can be accessed by the application at runtime.

src/main/webapp

This directory is used for web applications and contains web resources such as JSP files, HTML files, CSS files, JavaScript files, and other static resources. It also contains the WEB-INF directory, which includes web application configuration files like web.xml.

src/test/java

This directory contains the Java source files for unit tests. The package structure within this directory should mirror the package structure in src/main/java.

src/test/resources

This directory contains non-Java resources required for testing. These resources are copied to the classpath during the test phase and can be accessed by the test classes.

target

The target directory is where Maven stores the compiled output, including classes, JAR files, WAR files, and other generated resources. This directory is created during the build process and is typically cleaned up before a new build.

Conclusion

Understanding the standard directory layout in Maven is crucial for effective project management and collaboration. By following this layout, you ensure that your project is organized in a consistent and predictable manner, making it easier for you and other developers to navigate and maintain the project. Whether you're working on a simple Java application or a complex multi-module project, adhering to the Maven standard directory layout will help you manage your project more efficiently.

Comments