When creating a Java project, a structured and logical directory layout is crucial for organization and maintainability. Here's an outline of a common directory structure that many Java projects follow, especially when using build tools like Maven or Gradle. This structure adheres to convention over configuration principles for ease of understanding and automated tool compatibility.
my-java-project/
|-- src/
|-- main/
|-- java/
|-- com/
|-- mycompany/
|-- myapp/
|-- MyApp.java
|-- resources/
|-- application.properties
|-- logback.xml
|-- test/
|-- java/
|-- com/
|-- mycompany/
|-- myapp/
|-- MyAppTest.java
|-- resources/
|-- test-data.json
|-- lib/
|-- target/ (or build/)
|-- .git/ (if using Git)
|-- .gitignore
|-- pom.xml (if using Maven)
|-- build.gradle (if using Gradle)
|-- README.md
|-- LICENSE
Here's a breakdown of each section of the common structure:
-
src/ - The source folder contains all the code, resources, and property files.
main/ - Contains all the production code and resources.
java/ - Contains the Java source files. It's further broken down by package structure (e.g., com/mycompany/myapp/).
resources/ - Stores non-java files like a property files, XML configurations, and anything that should be included in the classpath.
test/ - Contains tests for your application.
java/ - Contains the Java test files, typically mirroring the package structure of the main/java directory.
resources/ - Resources needed for testing, such as sample data, configurations, etc.
-
lib/ - If you are not using a build tool or need to include specific libraries manually, they can be placed here (though this is less common with the usage of Maven and Gradle).
-
target/ or build/ - This directory is used by the build tool to store compiled bytecode (.class files), packaged JARs/WARs, and other build artifacts. Maven uses target/, while Gradle uses build/.
-
.git/ - The folder created by Git for repository metadata (if using Git).
-
.gitignore - Specifies intentionally untracked files that Git should ignore.
-
pom.xml - The Project Object Model (POM) file for Maven projects, which contains configurations for the project's build, dependencies, plugins, etc.
-
build.gradle - The build configuration script for Gradle projects.
-
README.md - A markdown document providing an introduction and overview of the project.
-
LICENSE - A text file specifying the license under which the project is provided.
This structure is often automatically generated when you create a new project with an IDE or when you use a tool like Maven Archetype or Gradle's Init plugin. Since many tools expect this structure, adhering to these conventions can simplify the build and deployment processes.