.gitignore for Gradle

*.class *.jar build/ !**/src/**/build/ .gradle/ !gradle/wrapper/gradle-wrapper.jar

A Gradle .gitignore ignores build/ in every project, the .gradle/ cache folder, and stray *.class and *.jar files, then re-includes gradle/wrapper/gradle-wrapper.jar so the wrapper survives a clean checkout. Those are the key lines of the tool’s Java (Gradle) template, which has 14 in all.

What the blocks ignore

  • Build output: build/, at any depth, so app/build/ and buildSrc/build/ go too. !**/src/**/build/ follows it, because a package such as com.acme.build lives in a source folder called build, and Gradle writes its own output outside src/.
  • Caches: .gradle/, the project’s cache and daemon state, rebuilt on the next run.
  • Compiled files: *.class, *.jar, *.war, *.ear, plus JVM crash logs and *.hprof heap dumps.
  • IDE helpers: gradle-app.setting and .gradletasknamecache.

In a scratch repository with this file, git check-ignore -v -n --no-index names the line that matched each path, or prints :: when none did:

.gitignore:10:build/	app/build/classes/java/main/App.class
.gitignore:10:build/	buildSrc/build/libs/buildSrc.jar
.gitignore:12:!**/src/**/build/	app/src/main/java/com/acme/build
::	app/src/main/java/com/acme/build/Tool.java
.gitignore:14:.gradle/	.gradle/8.10/fileHashes/x
.gitignore:19:!gradle/wrapper/gradle-wrapper.jar	gradle/wrapper/gradle-wrapper.jar
::	gradlew
::	gradle/libs.versions.toml
.gitignore:3:*.jar	libs/legacy.jar

Commit the wrapper, all four files

Gradle’s documentation says the wrapper files belong in version control: gradlew, gradlew.bat, gradle/wrapper/gradle-wrapper.properties and gradle/wrapper/gradle-wrapper.jar. Ignoring that jar is the classic mistake, because *.jar matches it; without it, ./gradlew fails on a fresh clone. Commit gradle/libs.versions.toml and settings.gradle.kts too.

The ! line has to come last

The last matching line wins, so the re-include must follow *.jar. The same two lines in the other order leave the jar ignored:

.gitignore:3:*.jar	gradle/wrapper/gradle-wrapper.jar

It also needs its folder to be visible: after a line like gradle/, Git never looks inside, and no ! line below can bring the jar back.

Kotlin and Android

Kotlin 2 writes a .kotlin/ folder at the root, which this template doesn’t know (:: .kotlin/sessions/x above); add the Kotlin template. Android projects also need local.properties and *.apk ignored, which the Android template covers.

Already committed build/ or .gradle/?

.gitignore only affects untracked files. Untrack what was committed, keeping it on disk, and commit:

git rm -r --cached build app/build .gradle
git commit -m "Stop tracking Gradle output"

Editor and OS files

.idea/ per-user files and .DS_Store come from your machine, not from Gradle. Pick the JetBrains template, or put them in a global ignore file:

git config --global core.excludesFile ~/.gitignore_global

Try it in the Gitignore Generator

In the file: Java (Gradle)

.gitignore

1 template · 14 pattern lines
# Java (Gradle)
*.class
*.jar
*.war
*.ear
hs_err_pid*
replay_pid*
*.hprof
# Gradle writes compiled output, test reports and coverage here
build/
# A source folder called build, such as the package com.example.build, is kept
!**/src/**/build/
# Gradle's caches and daemon state
.gradle/
# GUI settings
gradle-app.setting
.gradletasknamecache
# The wrapper is committed, so it survives a clean checkout
!gradle/wrapper/gradle-wrapper.jar
!gradle/wrapper/gradle-wrapper.properties
  • ✓Not ignored: "!gradle/wrapper/gradle-wrapper.jar" (Java (Gradle)) is the last line that matches.

Lines are applied top to bottom, and the last one that matches wins — which is why a line of your own can overrule a template. This one was read as a file.

Runs in your browser. Nothing you enter is sent anywhere.