1. Introduction
Apache Maven is one of the most popular build tools for Java projects. Apart from just decentralizing dependencies and repositories, promoting a uniform directory structure across projects is also one of its important aspects.
In this quick article, we’ll explore the standard directory layout of a typical Maven project.
2. Directory Layout
A typical Maven project has a pom.xml file and a directory structure based on defined conventions:
└───maven-project
├───pom.xml
├───README.txt
├───NOTICE.txt
├───LICENSE.txt
└───src
├───main
│ ├───java
│ ├───resources
│ ├───filters
│ └───webapp
├───test
│ ├───java
│ ├───resources
│ └───filters
├───it
├───site
└───assembly
The default directory layout can be overridden using project descriptors, but this is uncommon and discouraged.
Going ahead in this article, we’ll uncover more details about each standard file and subdirectory.
3. The Root Directory
This directory serves as the root of every Maven project.
Let’s take a closer look at the standard files and subdirectories that are typically found at root:
- maven-project/pom.xml – defines dependencies and modules needed during the build lifecycle of a Maven project
- maven-project/LICENSE.txt – licensing information of the project
- maven-project/README.txt – summary of the project
- maven-project/NOTICE.txt – information about third-party libraries used in the project
- maven-project/src/main – contains source code and resources that become part of the artifact
- maven-project/src/test – holds all the test code and resources
- maven-project/src/it – usually reserved for integration tests used by the Maven Failsafe Plugin
- maven-project/src/site – site documentation created using the Maven Site Plugin
- maven-project/src/assembly – assembly configuration for packaging binaries
4. The src/main Directory
As the name indicates, src/main is the most important directory of a Maven project. Anything that is supposed to be part of an artifact, be it a jar or war, should be present here.
Its subdirectories are:
- src/main/java – Java source code for the artifact
- src/main/resources – configuration files and others such as i18n files, per-environment configuration files, and XML configurations
- src/main/webapp – for web applications, contains resources like JavaScript, CSS, HTML files, view templates, and images
- src/main/filters – contains files that inject values into configuration properties in the resources folder during the build phase
5. The src/test Directory
The directory src/test is the place where tests of each component in the application reside.
Note that none of these directories or files will become part of the artifact. Let’s see its subdirectories:
- src/test/java – Java source code for tests
- src/test/resources – configuration files and others used by tests
- src/test/filters – contains files that inject values into configuration properties in the resources folder during the test phase
6. Conclusion
In this article, we looked at the standard directory layout for an Apache Maven project.
Multiple examples of Maven project structures can be found in the GitHub project.