将 GitHub 项目导入 Eclipse

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6760115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 20:19:29  来源:igfitidea点击:

Importing a GitHub project into Eclipse

eclipsegithub

提问by blue-sky

I've imported a GitHub project but how do I setup the project in my Eclipse project workspace?

我已经导入了一个 GitHub 项目,但如何在我的 Eclipse 项目工作区中设置该项目?

Do I need to set the GitHub working directory(c:\users) to my Eclipse working directory(c:\Projects) prior to checking out the project ?

在签出项目之前,我是否需要将 GitHub 工作目录 (c:\users) 设置为我的 Eclipse 工作目录 (c:\Projects)?

enter image description here

在此处输入图片说明

Edited :

编辑:

My working directory is now c:\users\projectname\.git So then I try to import the project using the eclipse "import" option. When I try to import selecting the option "Use the new projects wizard" the source code is not imported, if I import selecting the option "Import as general project" the source code is imported but the created project created by Eclipse is not a java project. When selecting the option "Use the new projects wizard" and creating a new java project using the wizard should'nt the code be automatically imported ?

我的工作目录现在是 c:\users\projectname\.git 所以然后我尝试使用 eclipse “import” 选项导入项目。当我尝试导入选择“使用新项目向导”选项时,不会导入源代码,如果我选择“导入为常规项目”选项导入,则导入源代码但由 Eclipse 创建的创建项目不是 java项目。When selecting the option "Use the new projects wizard" and creating a new java project using the wizard should'nt the code be automatically imported ?

enter image description here

在此处输入图片说明

采纳答案by VonC

As mentioned in Alain Beauvois's answer, and now (Q4 2013) better explained in

正如Alain Beauvois回答中提到的,现在(2013 年第 4 季度)更好地解释了

Copy the URL from GitHub and select in Eclipse from the menu the

从 GitHub 复制 URL 并在 Eclipse 中从菜单中选择

File → Import → Git → Projects from Git

http://wiki.eclipse.org/images/5/5a/Egit-0.9-import-projects-select-repository.png

http://wiki.eclipse.org/images/5/5a/Egit-0.9-import-projects-select-repository.png



If the Git repo isn't cloned yet:

如果 Git 存储库尚未克隆:

In> order to checkout a remote project, you will have to clone its repository first.
Open the Eclipse Import wizard (e.g. File => Import), select Git => Projectsfrom Git and click Next.
Select “URI” and click Next.
Now you will have to enter the repository's location and connection data. Entering the URI will automatically fill some fields. Complete any other required fields and hit Next. If you use GitHub, you can copy the URI from the web page.

> 为了签出远程项目,您必须先克隆其存储库。
打开 Eclipse 导入向导(例如File => Import),Git => Projects从 Git 中选择并单击Next
选择“ URI”并单击Next
现在您必须输入存储库的位置和连接数据。输入 URI 将自动填充一些字段。填写任何其他必填字段并点击Next。如果您使用 GitHub,则可以从网页复制 URI。

Select all branches you wish to clone and hit Next again.

Hit the Clone… button to open another wizard for cloning Git repositories.

选择您要克隆的所有分支,然后再次点击下一步。

点击 Clone... 按钮打开另一个用于克隆 Git 存储库的向导。

http://eclipsesource.com/blogs/wp-content/uploads/2012/12/14-282x300.png

http://eclipsesource.com/blogs/wp-content/uploads/2012/12/14-282x300.png



Original answer (July 2011)

原始答案(2011 年 7 月)

First, if your "Working Directory" is C:\Users, that is odd, since it would mean you have cloned the GitHub repo directly within C:\Users (i.e. you have a .gitdirectory in C:\Users)

首先,如果您的“工作目录”是C:\Users,那很奇怪,因为这意味着您直接在 C:\Users 中克隆了 GitHub 存储库(即您在 中有一个.git目录C:\Users

Usually, you would clone a GitHub repo in "any directory of your choice\theGitHubRepoName".

通常,您会在“ any directory of your choice\theGitHubRepoName”中克隆一个 GitHub 存储库。

As described in the EGit user Manual page:

EGit 用户手册页所述

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.

Typically, if these files are to be used in Eclipse, they must be imported into the Eclipse workspace in one way or another. In order to do so, the easiest way would be to check in .project files from which the "Import Existing Projects" wizard can create the projects easily. Thus in most cases, the structure of a Repository containing Eclipse projects would look similar to something like this:

在任何情况下(除非您创建一个“裸”存储库,但此处未讨论),新存储库本质上是本地硬盘上的一个文件夹,其中包含“工作目录”和元数据文件夹。
元数据文件夹是一个名为“ .git”的专用子文件夹,通常称为“ .git-folder”。它包含实际的存储库(即提交、引用、日志等)。

元数据文件夹对 Git 客户端完全透明,而工作目录用于将当前检出的 Repository 内容公开为工具和编辑器的文件。

通常,如果要在 Eclipse 中使用这些文件,则必须以一种或另一种方式将它们导入 Eclipse 工作区。为此,最简单的方法是签入 .project 文件,“导入现有项目”向导可以从中轻松创建项目。因此,在大多数情况下,包含 Eclipse 项目的存储库的结构看起来类似于以下内容:

See also the Using EGit with Githubsection.

另请参阅在Github 中使用 EGit部分。



My working directory is now c:\users\projectname\.git

我的工作目录现在是 c:\users\projectname\.git

You should have the content of that repo checked out in c:\users\projectname(in other words, you should have more than just the .git).

您应该签入该存储库的内容c:\users\projectname(换句话说,您应该拥有的不仅仅是.git)。

So then I try to import the project using the eclipse "import" option.
When I try to import selecting the option "Use the new projects wizard", the source code is not imported.

那么我尝试使用 eclipse 的“导入”选项导入项目。
当我尝试导入选择“使用新项目向导”选项时,未导入源代码。

That is normal.

这是正常的。

If I import selecting the option "Import as general project" the source code is imported but the created project created by Eclipse is not a java project.

如果我选择“作为常规项目导入”选项导入,则源代码将被导入,但由 Eclipse 创建的项目不是 Java 项目。

Again normal.

又正常了。

When selecting the option "Use the new projects wizard" and creating a new java project using the wizard should'nt the code be automatically imported ?

When selecting the option "Use the new projects wizard" and creating a new java project using the wizard should'nt the code be automatically imported ?

No, that would only create an empty project.
If that project is created in c:\users\projectname, you can then declare the eisting source directory in that project.
Since it is defined in the same working directory than the Git repo, that project should then appear as "versioned".

不,那只会创建一个空项目。
如果该项目是在 中创建的c:\users\projectname,则您可以在该项目中声明 eiting 源目录。
由于它定义在与 Git 存储库相同的工作目录中,因此该项目应该显示为“版本化”。

You could also use the "Import existing project" option, if your GitHub repo had versioned the .projectand .classpathfile, but that may not be the case here.

您还可以使用“导入现有项目”选项,如果您的 GitHub 存储库已对.project.classpath文件进行了版本控制,但此处可能并非如此。

回答by Alain Beauvois

With the last ADT, you can import Github project using Eclipse :

使用最后一个 ADT,您可以使用 Eclipse 导入 Github 项目:

  1. File -> Import -> Git -> Projects From Git > URI

  2. Enter the Github repository url

  3. Select the branch

  1. 文件 -> 导入 -> Git -> 来自 Git 的项目> URI

  2. 输入 Github 仓库 url

  3. 选择分支

回答by bomb zj

I encountered the same problem, and finally found a way:

遇到了同样的问题,终于找到了办法:

  1. Add or create git repository in git view.
  2. Click new java project menu, uncheck "use default location", select the git repo location, click finish.
  1. 在 git 视图中添加或创建 git 存储库。
  2. 点击新建java项目菜单,取消勾选“使用默认位置”,选择git repo位置,点击完成。

Ecplise Version: Mars.2 Release (4.5.2)

Ecplise 版本:Mars.2 发布 (4.5.2)

回答by phil294

unanswered core problem persists:

未解决的核心问题仍然存在:

My working directory is now c:\users\projectname.git So then I try to import the project using the eclipse "import" option. When I try to import selecting the option "Use the new projects wizard" the source code is not imported, if I import selecting the option "Import as general project" the source code is imported but the created project created by Eclipse is not a java project. When selecting the option "Use the new projects wizard" and creating a new java project using the wizard should'nt the code be automatically imported ?

我的工作目录现在是 c:\users\projectname.git 所以然后我尝试使用 eclipse 的“导入”选项导入项目。当我尝试导入选择“使用新项目向导”选项时,不会导入源代码,如果我选择“导入为常规项目”选项导入,则导入源代码但由 Eclipse 创建的创建项目不是 java项目。When selecting the option "Use the new projects wizard" and creating a new java project using the wizard should'nt the code be automatically imported ?

Yes it should.

是的,应该。

It's a bug. Reported here.

这是一个错误。报到这里

Here is a workaround:

这是一个解决方法:

  • Import as general project
  • Notice the imported data is no valid Eclipse project (no build path available)
  • Open the .projectxml file in the project folder in Eclipse. If you can't see this file, see How can I get Eclipse to show .* files?.
  • Go to sourcetab
  • Search for <natures></natures>and change it to <natures><nature>org.eclipse.jdt.core.javanature</nature></natures>and save the file

    (idea comes from here)

  • Right click the srcfolder, go to Build Path...and click Use as Source Folder
  • 作为一般项目导入
  • 注意导入的数据不是有效的 Eclipse 项目(没有可用的构建路径)
  • .project在 Eclipse 中打开项目文件夹中的xml 文件。如果您看不到此文件,请参阅如何让 Eclipse 显示 .* 文件?.
  • 转到source选项卡
  • 搜索<natures></natures>并将其更改为<natures><nature>org.eclipse.jdt.core.javanature</nature></natures>并保存文件

    (想法来自这里

  • 右键单击src文件夹,转到Build Path...并单击Use as Source Folder

After that, you should be able to run & debug the project, and also use team actions via right-click in the package explorer.

之后,您应该能够运行和调试项目,还可以通过在包资源管理器中右键单击来使用团队操作。

If you still have troubles running the project (something like "main class not found"), make sure the <buildSpec>inside the .projectfile is set (as described here):

如果您仍然有运行项目(类似于“找不到主类”)的烦恼,确保<buildSpec>内部.project设置文件(如描述这里):

<buildSpec>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
</buildSpec>

回答by Brian Redd

Using the command line is an option, and would remove the need for an Eclipse Plugin. First, create a directory to hold the project.

使用命令行是一种选择,并且不需要 Eclipse 插件。首先,创建一个目录来保存项目。

mkdir myGitRepo
cd myGitRepo

Clone the desired repository in the directory you just created.

在您刚刚创建的目录中克隆所需的存储库。

git clone https://github.com/JonasHelming/gitTutorial.git

Then open Eclipse and select the directory you created (myGitRepo) as the Eclipse Workspace.

然后打开 Eclipse 并选择您创建的目录 (myGitRepo) 作为 Eclipse 工作区。

Don't worry that the Project Explorer is empty, Eclipse can't recognize the source files yet.

不要担心 Project Explorer 是空的,Eclipse 还不能识别源文件。

Lastly, create a new Java project with the exact same name as the project you pulled. In this case, it was 'gitTutorial'.

最后,创建一个与您拉取的项目同名的新 Java 项目。在这种情况下,它是“gitTutorial”。

File -> New -> Java Project

At this point, the project's sub directories should contain the files pulled from Github. Take a look at the following post in my blog for a more detailed explanation.

此时,项目的子目录应该包含从 Github 拉取的文件。查看我博客中的以下帖子以获得更详细的解释。

http://brianredd.com/application/pull-java-project-from-github

http://brianredd.com/application/pull-java-project-from-github

回答by M.Usman

It can be done in two ways:

它可以通过两种方式完成:

1.Use clone Git

1.使用克隆Git

2.You can set it up manually by rearranging folders given in it. make a two separate folder 'src' and 'res' and place appropriate classes and xml file given by library. and then import project from eclipse and make it as library, that's it.

2.您可以通过重新排列其中给定的文件夹来手动设置。创建两个单独的文件夹 'src' 和 'res' 并放置库提供的适当类和 xml 文件。然后从eclipse导入项目并将其作为库,就是这样。

回答by Nate Sun

I think you need to create a branch before you can import into your local Eclipse, otherwise, there is an error leading to incapable of importing repository from Github or Bitbucket.

我认为您需要先创建一个分支,然后才能导入本地 Eclipse,否则会出现错误,导致无法从 Github 或 Bitbucket 导入存储库。

回答by yazhi

When the local git projects are cloned in eclipse and are viewable in gitperspective but not in package explorer(workspace), the following steps worked for me:

当本地 git 项目在 eclipse 中克隆并且在git透视中可见但在package explorer(工作区)中不可见时,以下步骤对我有用:

  • Select the repository in gitperspective
  • Right click and select import projects
  • git透视图中选择存储库
  • 右键单击并选择 import projects