2012-09-13 37 views
11

我試圖通過編程方式將項目導入到eclipse。我不想使用UI模式。以編程方式將現有項目導入Eclipse

下面是我用來導入項目代碼:

IProjectDescription description = ResourcesPlugin.getWorkspace().loadProjectDescription( new Path("PROJECT_PATH/.project")); 
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(description.getName()); 
project.create(description, null); 
project.open(null); 

我與.location file相處只有項目文件夾,文件.markers.snap.syncinfo.snap文件,但我沒有得到源文件夾和等

+0

行確定該項目,你是導入已經是一個Java項目? ie-是否存在預先存在的.project文件,並且它是否具有Java自然語言和java構建器? –

+0

是否要使用自己的一組文件創建新項目,還是希望工作空間指向現有項目? –

回答

11

使用org.eclipse.ui.wizards.datatransfer.ImportOperation

嘗試是這樣的:

IOverwriteQuery overwriteQuery = new IOverwriteQuery() { 
     public String queryOverwrite(String file) { return ALL; } 
}; 

String baseDir = // location of files to import 
ImportOperation importOperation = new ImportOperation(project.getFullPath(), 
     new File(baseDir), FileSystemStructureProvider.INSTANCE, overwriteQuery); 
importOperation.setCreateContainerStructure(false); 
importOperation.run(new NullProgressMonitor()); 
0

你的代碼似乎很好。你到底意味着什麼你無法獲得源文件夾?您是否嘗試刷新該項目?

project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());

+0

是的。沒有得到源文件夾 – user1584844

+0

那麼如果你沒有提供進一步的源代碼(獲取源文件夾代碼),我將無法爲你提供幫助。 –

0

你可能缺少與

description.setLocation(new Path("/absolute/path/to/project/folder"));