java maven原理_浅论Maven和Git的原理及展示其与Eclipse的集成

重点补充:

Git和Eclipse的EGit插件对于关键词Repository的理解不一致,需要重点强调。在Git中,Repository的定义就是工作目录下的.git目录,工作目录就是是一个Project的根目录。Repository是工作目录的子目录。

为了证明不是我对Repository理解错误,下面是《Pro Git》中的部分原文:

Getting a Git Repository

You can get a Git project using two main approaches. The first takes an existing project or directory and imports it into Git. The second clones an existing Git repository from another server.

Initializing a Repository in an Existing Directory

If you’re starting to track an existing project in Git,you need to go to the project’s directory and type

$ git init

This creates a new subdirectory named .git that contains all of your necessary repository files — a Git repository skeleton.

而Eclipse的EGit插件则不一样。在Eclipse的世界里,Project是一个比较小的概念,而且不能嵌套,否则IDE会出问题,再加上Eclipse世界的观点认为我们经常需要在一个workspace中开多个Project。所以Git中的working directory不是一个Project的工作目录,而是一个workspace的工作目录.而Project是workspace的子目录,所以.git目录不应该放到Project的根目录下,而应该放到workspace的根目录下。在workspace目录下放一个.git目录有很多好处,比如可以同时追踪多个项目的源代码,比如可以在删除workspace时同时删除.git目录。但是在Eclipse遍历workspace的时候,.git目录的存在会不会出现问题还说不准。所以最好的办法是把.git目录、Project目录放到workspace之外。如上所述,按Eclipse的哲学,.git目录不应该是Project的子目录,而是Project的平级目录,那么当.git目录和Project目录都放到workspace之外的话,总得有一个目录来存放它们吧,Eclipse就把这个存放它们的那个目录叫Repository。其概念和Git中原有的Repository概念不一样,Eclipse的Repository不是指.git目录,而是Project目录的父目录,在Eclipse中,.git目录叫metadata folder,如果和Git中的Repository是Project的子目录的概念相比较,这两个Repository的概念差了两个辈份。

如下图,将Project纳入Git管理,选择快捷菜单Team->Share Project命令弹出的对话框:

7aa5524d57e8bf07e3b0c6d5c886d4e4.png

在这个对话框,有一个Use or create repository in parent folder of project的单选框,这里的repository就是指.git目录的祖父目录,所以选中这个单选框,则workspace就相当于是repository,而.git目录就会放到Project目录下。这和我们平时单独使用Git的习惯是一致的,比较容易理解,但是却是Eclipse不推荐的。

按照Eclipse的哲学,我们应该点这个对话框中的Create按钮,这样会弹出如下对话框:

3c4c6a24493c45bcfa05abae31f7c0e2.png

按照我上面这么填写后,相当于创建了一个Repository,其路径为/home/youxia/git/examples,.git目录就会创建在这个目录下,Project目录也会移动到这个目录下,而workspace依然不变。点Finish,会返回上一个对话框,可以看到下面的表格中,显示该项目的current location为/home/youxia/workspace/rcp-example,而创建Repository后,将会将该项目移动到/home/youxia/git/examples/rcp-example,.git目录和rcp-example的项目目录是平级的,都是examples的子目录,examples就是Repository。而且使用这一个.git目录,可以追踪多个项目,也就是说,以后凡是我为了做示范写的代码,不管是swt-example也好,还是springside-example也好,都可以放到examples下,用这一个.git目录保管他们的版本库。

4427499c87db5e4aacc7d52b3b99e594.png

为了证明我没有误解Eclipse的哲学,下面是EGit/User Guide(http://wiki.eclipse.org/EGit/User_Guide)的部分原文:

Eclipse Workspace and Repository working directory

Git Repositories can be created in different ways,for example by cloning from an existing Repository,by creating one from scratch,or by using the EGit Sharing wizard.

In any case (unless you create a"bare"Repository,but that's not discussed here),the new Repository is essentially a folder on the local hard disk which contains the"working directory"and the metadata folder. The metadata folder is a dedicated child folder named".git"and often referred to as".git-folder". It contains the actual repository (i.e. the Commits,the References,the logs and such).

The metadata folder is totally transparent to the Git client,while the working directory is used to expose the currently checked out Repository content as files for tools and editors.

另一段,关于.git目录不应该放到Project目录下,而应该和Project目录平级,并放到workspace之外的:

It is probably not a good idea to make a project the root folder of your Repository

The reason is that you will never be able to add another project to this Repository,as the .project file will occupy the root folder;you could still add projects as sub-folders, but this kind of project nesting is known to cause lots of problems all over the place. In order to add another project, you would have to move the project to a sub-folder in the Repository and add the second project as another sub-folder before you could commit this change.It is a good idea to keep your Repository outside of your Eclipse Workspace

There are several reasons for this: The new Repository will consider the complete folder structure of the Eclipse workspace as (potential) content. This can result in performance issues,for example when calculating the changes before committing (which will scan the complete .metadata folder,for example);more often than not, the workspace will contain dead folders (e.g. deleted projects) which semantically are not relevant for EGit but can not be excluded easily.The metadata (.git-) folder will be a child of the Eclipse Workspace. It is unclear whether this might cause unwanted folder traversals by Eclipse.

You can easily destroy your Repository by destroying your Eclipse Workspace

这里面的细微差别可能会在使用EGit的过程中产生极大困惑。我将其作为重要内容补充道博文末尾,希望能对大家有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值