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, whichmvn packagerebuilds. A second line,!**/src/**/target/, keeps a source package that happens to be calledtarget. - Compiled files and archives:
*.class,*.jar,*.war,*.ear,*.nar. - JVM leftovers:
hs_err_pid*andreplay_pid*crash logs, and*.hprofheap dumps, which are large and can hold production data. - Plugin scratch files:
pom.xml.releaseBackup,pom.xml.versionsBackup,release.propertiesand the shade plugin’sdependency-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.