Project Structure Setup
Standard Java Project Layout
Establishing a consistent project structure is essential for maintainability and scalability in Java development within the LabEx learning environment.
graph TD
A[Java Project Root] --> B[src]
A --> C[pom.xml/build.gradle]
A --> D[README.md]
B --> E[main]
B --> F[test]
E --> G[java]
E --> H[resources]
F --> I[java]
F --> J[resources]
Directory Structure Breakdown
1. Project Root Directory
## Create project skeleton
mkdir -p my-java-project/src/main/java
mkdir -p my-java-project/src/main/resources
mkdir -p my-java-project/src/test/java
mkdir -p my-java-project/src/test/resources
## Navigate to project root
cd my-java-project
2. Standard Directories
Directory |
Purpose |
src/main/java |
Production source code |
src/main/resources |
Configuration files, properties |
src/test/java |
Unit and integration tests |
src/test/resources |
Test configuration files |
Maven Project Setup
## Generate Maven project structure
mvn archetype:generate \
-DgroupId=com.labex.demo \
-DartifactId=my-java-project \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
Gradle Project Setup
## Initialize Gradle project
gradle init --type java-application \
--dsl groovy \
--test-framework junit \
--project-name my-java-project
Project Configuration Files
pom.xml (Maven)
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.labex.demo</groupId>
<artifactId>my-java-project</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
build.gradle (Gradle)
plugins {
id 'java'
id 'application'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'junit:junit:4.13.2'
}
Package Naming Conventions
Recommended Structure
com
└── labex
└── demo
├── model
├── service
├── repository
└── controller
Version Control Integration
## Initialize Git repository
git init
## Create .gitignore
echo "target/
build/
*.class
*.log" > .gitignore
Best Practices
- Maintain consistent directory layout
- Separate production and test code
- Use meaningful package names
- Include build configuration files
- Add version control
By following these guidelines in the LabEx environment, developers can create well-structured, maintainable Java projects on Ubuntu 22.04.