1

We follow the below steps to import a Local Git project in Eclipse:

File -> Import -> Git -> Projects From Git > Existing local repository

How can we achieve the same using a Java program?

I used org.eclipse.core.resources to import the project by giving the path of a .project file but it doesn't give Git connectivity.

IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject project = root.getProject("myProject/.project"); // path to .project file
project.create(new NullProgressMonitor());
project.open(null);

I also went through JGit Docs, but didn't find any solution

1
  • If you feel an answer solved the problem, please mark it as 'accepted' by clicking the green check mark. This helps keep the focus on older posts which still don't have answers. Commented Sep 13, 2017 at 15:14

1 Answer 1

1

In order to load a project into the Eclipse workspace, you need to load the project description (stored in the .project file) first. Once you have an IProjectDescription instance, you can use it to create it, i.e. make it known to the Eclipse workspace.

For example:

IProjectDescription description = workspace.loadProjectDescription( "path/to/.project" );
IProject project = workspace.getRoot().getProject( description.getName() );
project.create( description, new NullProgressMonitor() );
project.open( new NullProgressMonitor() );

In order to check whether a project of the same name is already known to the workspace, use this code:

workspace.getRoot().getProject( description.getName() );

If the repository to which the project to be imported belongs is not yet known to Egit, you need to register the repository first:

RepositoryUtil.addConfiguredRepository( new File( "path/to/.git-dir" ) );

Then you can use Egit's ConnectProviderOperation to mark the workspace project as part of the registered repository:

IEGitOperation operation = new ConnectProviderOperation( project, "path/to/.git-dir" );
operation.execute( new NullProgressMonitor() );

All Egit types discussed here can be found in the org.eclipse.egit.core plug-in.

Sign up to request clarification or add additional context in comments.

4 Comments

Your first code for project creation helped me. After that I need to modify the workspace rulefactory to get project content and git connectivity using workspace.getRuleFactory().modifyRule()
Don't understand what 'need to modify the workspace rulefactory' means. Are you asking which rule to use to connect a project to a repository?
I mean, I was able to solve the problem by calling workspace.getRuleFactory().modifyRule() after project.open
modifyRule() just returns a rule to modify the given resource. It is unlikely that this method call solves anything to do with creating a project. If you are interested in a reliable solution, you should remove the modifyRule() call and investigate why creating the project does not work (if so).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.