How to import a GIT non-Eclipse Java project into Eclipse?

Robert picture Robert · Nov 9, 2011 · Viewed 39.1k times · Source

I have some problems importing a Java project into my workspace. I am following this tutorial - however I can not use the final Import existing projects step because the GIT repository I use does not include the Eclipse specific .project and .classpath files.

Use the New Projects wizard

Therefore the project is not recognizes as project and hence can not be imported. Therefore I tried my luck using the option Use the New Projects wizard and select "Java Project" in the next dialog. The problem is that this creates a new Java project without any content! The project is also not connected to the GIT repository.

Edit: This is a known bug of eGIT: Bug 324145 - Project import doesn't work for abitary project types - if you want this problem fixed vote for it...

Import as general Project

If I use Import as general Project Eclipse always wants to use the external repository directory as project directory which is not what I want and additionally the created Project is not Java-enabled.

Therefore I am asking why it is so complicated to import a Java project into Eclipse using eGIT?

Answer

Lycha picture Lycha · Nov 9, 2011

It is possible by first cloning the repository and then creating a General project based on that. Then you can convert it to Java project. Here is how:

  • First go to File>Import...>Projects from GIT.
  • In the Select a Git Repository view you first press Clone. And follow instructions. This will create a local "checkout" of the repository to your computer. You can set the folder to be your workspace so it looks like any other of your eclipse projects.
  • After you have cloned the repository you get back to Import-view. Now you can select the repository you just cloned from the list.
  • Click Next and select Import as General Project. Now you have a git repository to eclipse.
  • Convert it to Java project: Add nature and buildCommand elements from other Java project to your .project file:

Relevant sections from .project:

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

Then from Project>Properties>Java Build Path>Source add your source folders (and possible libraries).

Edit: Added the conversion to Java project.