git
.gitignore for Java
Comprehensive .gitignore for Java projects covering Maven, Gradle, compiled classes, IDE files, and build artifacts.
Overview
A comprehensive .gitignore for Java projects using Maven or Gradle. Covers compiled class files, build output, IDE project files, dependency caches, and runtime artifacts for Spring Boot and general Java applications.
Configuration
# .gitignore
# ── Compiled Class Files ──
*.class # Java compiled bytecode
# ── Package Files ──
*.jar # Java Archive
*.war # Web Application Archive
*.nar # Native Archive
*.ear # Enterprise Archive
*.zip # Compressed archives
*.tar.gz # Compressed archives
# ── Maven ──
target/ # Maven build output directory
pom.xml.tag # Maven release plugin
pom.xml.releaseBackup # Maven release plugin backup
pom.xml.versionsBackup # Maven versions plugin
pom.xml.next # Maven release plugin
release.properties # Maven release properties
dependency-reduced-pom.xml # Maven shade plugin
buildNumber.properties # Maven build number plugin
.mvn/timing.properties # Maven timing data
.mvn/wrapper/maven-wrapper.jar # Maven wrapper JAR (download on build)
# ── Gradle ──
.gradle/ # Gradle cache and config
build/ # Gradle build output
!gradle/wrapper/gradle-wrapper.jar # Keep Gradle wrapper JAR
!**/src/main/**/build/ # Don't ignore source build dirs
!**/src/test/**/build/ # Don't ignore test build dirs
# ── Spring Boot ──
application-local.properties # Local Spring config overrides
application-local.yml # Local Spring config overrides (YAML)
*.log # Application log files
logs/ # Log directory
# ── JVM Crash Logs ──
hs_err_pid* # HotSpot error log files
replay_pid* # HotSpot replay files
# ── IntelliJ IDEA ──
.idea/ # IntelliJ project directory
*.iws # IntelliJ workspace
*.iml # IntelliJ module files
*.ipr # IntelliJ project files
out/ # IntelliJ build output
# ── Eclipse ──
.apt_generated # Eclipse annotation processing
.classpath # Eclipse classpath
.factorypath # Eclipse factory path
.project # Eclipse project file
.settings/ # Eclipse settings directory
.springBeans # Spring IDE beans
.sts4-cache/ # Spring Tool Suite cache
bin/ # Eclipse build output
# ── NetBeans ──
/nbproject/private/ # NetBeans private config
/nbbuild/ # NetBeans build
/nbdist/ # NetBeans distribution
/.nb-gradle/ # NetBeans Gradle plugin
# ── VS Code ──
.vscode/* # VS Code settings
!.vscode/settings.json # Keep shared settings
!.vscode/extensions.json # Keep extension recommendations
# ── Environment ──
.env # Environment variables
.env.local # Local environment overrides
*.pem # SSL certificates / private keys
*.key # Private keys
# ── OS Files ──
.DS_Store # macOS metadata
Thumbs.db # Windows thumbnails
*~ # Backup files
# ── Testing ──
test-output/ # TestNG output
surefire-reports/ # Maven Surefire test reports
jacoco.exec # JaCoCo coverage data
Key Options Explained
*.class— Compiled Java bytecode files. Always regenerated by the compiler from.javasource files.target/— Maven’s default output directory for compiled code, packaged JARs/WARs, and test reports. Entirely reproducible from source..gradle/— Gradle’s local cache of downloaded dependencies and build metadata. Recreated automatically.!gradle/wrapper/gradle-wrapper.jar— The negation keeps the Gradle wrapper JAR, allowing developers to build without pre-installing Gradle..idea/— IntelliJ IDEA stores user-specific project settings here. These differ between developers and cause merge conflicts.hs_err_pid*— JVM crash dump files generated when the JVM encounters a fatal error. Not useful in version control.application-local.properties— Spring Boot convention for local developer overrides (database URLs, API keys). Never commit these.
Common Modifications
- Keep IntelliJ code style: Add
!.idea/codeStyles/to share code formatting rules across the team. - Gradle wrapper: Some teams track
gradle-wrapper.jar(already negated above), while others usegradle wrapper --gradle-version X.Yto download it. - Lombok: Add
.apt_generated_tests/if using Lombok with annotation processing. - Docker: Add
.dockerignorealongside this file to exclude the same patterns from Docker build context. - JPA/Hibernate: Add
*.ddlif generating DDL scripts, or keep them if they serve as migration references.