Maven 项目中的哪些文件应该提交给 git?

What files in a Maven project should be committed to git?

我想知道 Maven 项目中的哪些文件应该提交给 git。

我应该在提交之前执行 mvn clean,还是将某些文件添加到 .gitignore 文件?

检查这个:

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

一般来说,您应该忽略所有目标和元数据。如果忽略目标,则在推送之前不需要 mvn clean

我个人使用 Maven gitignore 和 Java gitignore 作为 Maven 项目。您可能需要使用 Maven 项目中使用的语言对其进行调整。

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?

首先将规则添加到您的 .gitignore 文件中,这使得 Git 可以正确地忽略不需要的文件。理解 Maven standard directory layout 也将帮助您更好地确定哪些是不需要的目录。

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

在提交之前执行mvn clean 根本不切实际。开发人员可以忘记这一点,此外他们应该在每次提交时重建他们的项目。
正确的方法是使用 .gitignore 指定在跟踪中忽略的文件。只需提交并推送到远程分支,所有开发人员都可以使用相同的规则。

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

您想要 commit/push 您想要 version/track 的文件。
但范围很广。你不能只为 Maven 制定规则。 Maven 有一些特殊性(例如 target 您想忽略的文件夹),但您可能还有更多要忽略的东西。
您通常想要 commit/push 源代码和应用程​​序配置文件,例如 pom.xml 或构建中使用的任何配置文件,但您也可以添加任何其他类型的文件。例如,提交更新日志甚至 word 文档(更罕见但可能)也可能有效。
通常您不想提交的文件是:

  • 取决于开发者机器(IDE,自定义文件)
  • 由构建操作创建(Maven 中的 target 文件夹,但根据您的 pom 配置,您也可以有其他文件夹)
  • 在构建、应用程序执行或发布操作期间使用的临时文件。
  • 档案