Maven project structure
Introduction
One of Maven’s biggest advantages is that it enforces a standard project directory layout.
This structure ensures consistency across projects and eliminates the need to manually tell Maven where to find source code, resources, or tests.
By following Maven’s default directory layout, you make your project:
- Easier for other developers to understand
- Compatible with Maven’s automated build lifecycle
- Compatible with most Java IDEs like IntelliJ IDEA, Eclipse, and VS Code
1. Standard Maven Directory Layout
When you create a Maven project (e.g., using mvn archetype:generate
), Maven generates a standard folder structure like this:
my-app/
│
├── pom.xml
├── src/
│ ├── main/
│ │ ├── java/ → Application source code
│ │ ├── resources/ → Config files, property files, XML, etc.
│ └── test/
│ ├── java/ → Unit and integration test code
│ ├── resources/ → Test-specific resources
│
└── target/ → Build output (compiled classes, packaged JAR/WAR)
2. src/main/java
– Application Source Code
- This directory contains all your Java application source files.
- Maven automatically compiles everything here and places the
.class
files in thetarget/classes
directory. - No need to configure this path — Maven knows this is your main source folder by default.
Example: src/main/java/com/example/app/Main.java
3. src/main/resources
– Application Resources
- Contains non-Java files needed by the application at runtime:
.properties
files.xml
configuration files- Static assets (if applicable)
- Maven copies these directly to the
target/classes
folder so they are available on the classpath.
Example: src/main/resources/application.properties
4. src/test/java
– Test Source Code
- Holds all unit and integration tests for your application.
- Maven automatically runs these tests when you execute:
mvn test
- Test classes usually mirror the main source package structure for clarity.
Example:src/test/java/com/example/app/MainTest.java
5. src/test/resources
– Test Resources
- Stores files required only during testing:
- Test-specific
.properties
or.xml
files - Mock data files (JSON, CSV, XML)
- Test-specific
- Maven copies them to
target/test-classes
during the test phase.
6. The target
Folder – Build Outputs
The target
folder is where Maven puts everything generated during the build process:
Folder/File | Purpose |
---|---|
classes/ | Compiled .class files from src/main/java and resources from src/main/resources |
test-classes/ | Compiled test classes and resources |
my-app-1.0.0.jar or .war | Final packaged output (depends on <packaging> in pom.xml ) |
generated-sources/ | Sources generated by annotation processors or plugins |
maven-status/ | Internal Maven build status tracking |
Important: The target
directory is automatically deleted when you run:
mvn clean
Why Standard Structure Matters
✅ Convention over configuration – You don’t need to manually define paths in the POM
✅ Team collaboration – Any developer can join and instantly understand the project layout
✅ IDE compatibility – Most Java IDEs automatically recognize the Maven layout
✅ Build automation – Maven automatically knows where to find sources, resources, and tests
Customizing the Structure (Optional)
While it’s highly recommended to stick to Maven’s defaults, you can change directories in the pom.xml
under the <build>
section:
<build>
<sourceDirectory>src/code/java</sourceDirectory>
<testSourceDirectory>src/code/test</testSourceDirectory>
</build>
However, doing this may break tooling compatibility unless necessary.
Key Takeaways
- Maven’s default structure saves time and reduces configuration effort.
src/main/java
→ application code;src/test/java
→ test code.- Resources are copied into the classpath automatically.
target
contains all build artifacts and is safe to delete.