Git配置Java开发环境的.gitignore文件

2019-11-05  本文已影响0人  和风拂柳花香醉人

一、为什么要配置.gitignore?

我们在使用Java集成开发工具过程中都会产生一些配置文件,并且这些都是一些隐藏文件,一般人都不会去留意它们。开发项目的时候,还会有一些日志文件,target文件,jar包,class文件等等,如果没有做配置,在使用Git去同步代码的时候就会发现这些文件也同步到代码仓库中。不处理虽然不会有什么问题,但是多多少少也会对我们产生一些困惑。例如:

二、如何配置.gitignore?

作为Java开发者,一般使用EclipseIntelij IDEA作为开发工具,Maven作为项目管理工具,Git作为版本管理工具,这些工具在使用过程中都会产生一些文件,我们只要配置.gitignore文件就可以排除这些与源代码无关的文件。
github为开发者提供一个github/gitignore的仓库,里面包含了大部分语言和开发工具的gitignore文件模板,我们只要根据需要下载相关模板进行配置即可。

github/gitignore仓库地址:https://github.com/github/gitignore

由于EclipseIntelij IDEAMaven每个项目都会用到,我们把这几个的配置作为Git的全局配置,这样就可以一劳永逸了,配置一次即可。而与项目相关的,例如JavaJSPython等等其他语言的,只要在项目中按实际使用情况进行配置即可。

以下配置基于Linux平台,Windows同理。

全局配置

1、创建~/.gitignore_global文件

$ touch ~/.gitignore_global 

2、将上面的文件设置为git的全局core.excludesfile文件

$ git config --global core.excludesfile ~/.gitignore_global

3、编辑~/.gitignore_global文件

$ vim ~/.gitignore_global

4、添加排除Linux系统的规则,规则模板地址:https://github.com/github/gitignore/blob/master/Global/Linux.gitignore

#############################################
# Linux.gitignore global
#############################################
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

5、添加Eclipse规则,规则模板地址:https://github.com/github/gitignore/blob/master/Global/Eclipse.gitignore

#############################################
# Eclipse.gitignore global
#############################################                                  
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# PyDev specific (Python IDE for Eclipse)
*.pydevproject

# CDT-specific (C/C++ Development Tooling)
.cproject

# CDT- autotools
.autotools

# Java annotation processor (APT)
.factorypath

# PDT-specific (PHP Development Tools)
.buildpath

# sbteclipse plugin
.target

# Tern plugin
.tern-project

# TeXlipse plugin
.texlipse

# STS (Spring Tool Suite)
.springBeans

# Code Recommenders
.recommenders/

# Annotation Processing
.apt_generated/
.apt_generated_test/

# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet

6、配置Intelij IDEA规则,规则地址:https://github.com/github/gitignore/blob/master/Global/JetBrains.gitignore

#############################################
# JetBrains.gitignore global
#############################################
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn.  Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

7、配置Maven规则,规则地址:https://github.com/github/gitignore/blob/master/Maven.gitignore

#############################################
# Maven.gitignore global
#############################################
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

项目配置

1、在Java项目中添加.gitignore文件

$ touch .gitignore
$ vim .gitignore

2、添加Java规则,规则地址:https://github.com/github/gitignore/blob/master/Java.gitignore

#################################################
# 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*

参考文章:

  1. gitignore docs
  2. Ignoring files
  3. How to gitignore idea files
上一篇下一篇

猜你喜欢

热点阅读