.gitignore for Java with Maven

*.class *.jar hs_err_pid* target/ !**/src/**/target/ release.properties .mvn/wrapper/maven-wrapper.jar

A Maven project’s .gitignore centers on target/, where Maven writes classes, test reports and the packaged jar, plus stray *.class and *.jar files, HotSpot crash logs (hs_err_pid*), the release plugin’s release.properties and backups, and .mvn/wrapper/maven-wrapper.jar. Those are the key lines of the tool’s Java (Maven) template, which has 19 in all.

What the blocks ignore

  • Build output: target/ in every module, which mvn package rebuilds. A second line, !**/src/**/target/, keeps a source package that happens to be called target.
  • Compiled files and archives: *.class, *.jar, *.war, *.ear, *.nar.
  • JVM leftovers: hs_err_pid* and replay_pid* crash logs, and *.hprof heap dumps, which are large and can hold production data.
  • Plugin scratch files: pom.xml.releaseBackup, pom.xml.versionsBackup, release.properties and the shade plugin’s dependency-reduced-pom.xml.

In a scratch repository with this file, git check-ignore -v -n --no-index shows which line matched each path, or :: for none:

.gitignore:14:target/	module-a/target/app-1.0.jar
.gitignore:16:!**/src/**/target/	src/main/java/com/acme/target
::	pom.xml
.gitignore:28:.mvn/wrapper/maven-wrapper.jar	.mvn/wrapper/maven-wrapper.jar
::	.mvn/wrapper/maven-wrapper.properties
::	mvnw
.gitignore:4:*.jar	libs/vendor-sdk.jar
.gitignore:4:*.jar	src/test/resources/fixture.jar

The wrapper: commit the scripts, not the jar

Commit mvnw, mvnw.cmd and .mvn/wrapper/maven-wrapper.properties; they pin the Maven version every machine uses. The jar beside them is ignored because mvnw downloads it when it’s missing, and the newer only-script wrapper doesn’t use one at all.

Keeping a jar you really need

*.jar also catches jars you mean to commit, such as a vendor SDK that isn’t in any repository or a test fixture. A later ! line brings one back, because its folder isn’t excluded:

.gitignore:31:!libs/*.jar	libs/vendor-sdk.jar

That only works outside target/. Git never looks inside an excluded directory, so !target/app-1.0.jar changes nothing and the jar is still reported against target/ on line 14.

Already committed target/?

.gitignore only affects untracked files. If a module’s target/ or a built jar was committed earlier, untrack it, keeping the files on disk, and commit:

git rm -r --cached target module-a/target
git commit -m "Stop tracking build output"

IDE and OS files

Eclipse’s .settings/, IntelliJ’s per-user .idea/ files and .DS_Store aren’t Maven’s. Pick the Eclipse or JetBrains template if the whole team uses that IDE; otherwise keep them in a global ignore file:

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

Without that setting, Git reads ~/.config/git/ignore.

Try it in the Gitignore Generator

In the file: Java (Maven)

.gitignore

1 template · 19 pattern lines
# Java (Maven)
# Compiled bytecode
*.class
*.jar
*.war
*.ear
*.nar
# Crash logs from the HotSpot JVM
hs_err_pid*
replay_pid*
# Heap dumps, which are large and often hold data
*.hprof
# Maven's build directory
target/
# A source folder called target, such as the package com.example.target, is kept
!**/src/**/target/
# Rewritten by the release plugin, which keeps the old pom under these names
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
# Written by the shade plugin
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# Downloaded by mvn wrapper:wrapper; Maven 3.9+ only needs maven-wrapper.properties committed
.mvn/wrapper/maven-wrapper.jar
  • ✓Not ignored: no line in this file matches "/.mvn/wrapper/maven-wrapper.properties".

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.

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