27

I want to know what files in a Maven project should be committed to git.

Am I suppose to perform a mvn clean before committing, or do I add certain files to the .gitignore file?

tyleax
  • 1,556
  • 2
  • 17
  • 45

4 Answers4

35

Personally I use Maven gitignore and Java gitignore for a Maven project. You might need to adjust it with the languages used in your Maven project.

https://github.com/github/gitignore/blob/master/Maven.gitignore

target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar

https://github.com/github/gitignore/blob/master/Java.gitignore

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

Is it good practice to perform mvn clean before committing, or do I add certain files to the .gitignore file?

Add rules to your .gitignore file first, which makes Git ignores the undesired files correctly. Understanding Maven standard directory layout will also help you better determine which are the undesired directories.

Mincong Huang
  • 5,284
  • 8
  • 39
  • 62
  • Does this avoid .mvn directory to be tracked? Does .mvn directory need to be tracked? I am not talking about the local Maven repo, which can be somewhere else in your filesystem, but the .mvn directory WITHIN the Springboot project itself (where wrapper/MavenWrapperDownloader.java for instance is located). – ElPiter Mar 23 '21 at 09:10
  • @ElPiter the `Maven.gitignore` file that I shared, it does not prevent the whole `.mvn` directory being tracked, it only excluded some files there. According to https://maven.apache.org/configure.html#mvn-folder , `.mvn` is located within the project’s top level directory, the files `maven.config`, `jvm.config`, and `extensions.xml` contain project specific configuration for running Maven. This folder is part of the project and may be checked in into your version control. For example, JGit uses `.mvn`, see source code here https://github.com/eclipse/jgit/tree/v5.11.0.202103091610-r/.mvn – Mincong Huang Mar 23 '21 at 20:27
7

Is it good practice to perform mvn clean before committing, or do I add certain files to the .gitignore file?

Executingmvn clean before committing is not practical at all. Developers can forget that and besides they should rebuild their projects at each commit.
The correct way is using .gitignore to specify files to ignored in the tracking. Just commit it and push into the remote branch and all developers could work with the same rules.

I want to know what files in a Maven project should be committed to git.

You want to commit/push files that you want to version/track.
But it is very broad. You cannot have rules just for Maven. Maven have some specificities (target folder for example that you want to ignore) but you would have probably more things to ignore.
You want to generally commit/push the source code and application configuration files such as pom.xml or any configuration files used in your build but you can also add any other kind of files. For example committing a changelog or even a word document (more rare but possible) may also be valid.
Generally what you don't want to commit are files that :

  • depends on the developer machine (IDE, custom files)
  • created by a build operation (target folder in Maven but you could also have other folders according to your pom configuration)
  • temporary files using during the build, the application execution or still the release operations.
  • archives
davidxxx
  • 125,838
  • 23
  • 214
  • 215
6

Check this:

https://www.gitignore.io/api/maven

# Created by https://www.toptal.com/developers/gitignore/api/maven
# Edit at https://www.toptal.com/developers/gitignore?templates=maven

### Maven ###
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar

# Eclipse m2e generated files
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath

# End of https://www.toptal.com/developers/gitignore/api/maven

In general you should ignore all targets and metadata. If you ignore targets, mvn clean is not required before pushing.

questionto42
  • 7,175
  • 4
  • 57
  • 90
mc20
  • 1,145
  • 1
  • 10
  • 26
1

I had a Maven project in VSCodium and had to decide whether to commit the .project file or not. That should be linked to in this Q/A since it happens with other IDE:s as well that have Maven extensions.

This is 2010, only for Eclipse:

.classpath and .project - check into version control or not?

which says overall that it should be committed. I guess the discussion is timeless. It is a Maven generated file, but it should still be in the repository, and even more, if the repository is at work with the same setup and tools by the team.

Other questions:

The same for the .classpath. Even if it is made by Maven, it should be in the repo.

I am a beginner at Maven and only guess this. I cannot understand why this was not in this Q/A up to now. The accepted answer lists the ignored files, but from reading that, I was not fully sure what to do with these meta files from Maven. And there is even one answer that lists the two files as files that are to be ignored in this Q/A here. Which, as far as I can see from a repository I took over, and guessing from the accepted answer, is wrong: the two files belong to the version control.

questionto42
  • 7,175
  • 4
  • 57
  • 90