I've written a java program that writes another java project. However, I want to add a piece of specific code that will import the project into the workspace. Can this be done?
+2
A:
You have here the same idea expressed by Liran Orevi but with some more details and a code example:
/**
* Imports the given path into the workspace as a project. Returns true if the
* operation succeeded, false if it failed to import due to an overlap.
*
* @param projectPath
* @return
* @throws CoreException if operation fails catastrophically
*/
private boolean importExisitingProject(IPath projectPath) throws CoreException {
// Load the project description file
final IProjectDescription description = workspace.loadProjectDescription(
projectPath.append(IPath.SEPARATOR + IProjectDescription.DESCRIPTION_FILE_NAME));
final IProject project = workspace.getRoot().getProject(description.getName());
// Only import the project if it doesn't appear to already exist. If it looks like it
// exists, tell the user about it.
if (project.exists()) {
System.err.println(SKTBuildPlugin.getFormattedMessage(
"Build.commandLine.projectExists", //$NON-NLS-1$
project.getName()));
return false;
}
IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor) throws CoreException {
project.create(description, monitor);
project.open(IResource.NONE, monitor);
}
};
workspace.run(runnable,
workspace.getRuleFactory().modifyRule(workspace.getRoot()),
IResource.NONE, null);
return true;
}
In this thread, you can also import projects which are zipped, with some code inspired from mostly scraped from org.eclipse.ui.internal.wizards.datatransfer.WizardProjectsImportPage.java
.
VonC
2009-12-28 08:21:48
ok, so the code is a little complicated (I'm cooking dinner too). Can I simply call the existing ImportWizard and save myself writing this?
piggles
2009-12-28 08:48:59
@Mechko: I am not sure, since I have not tested directly that code, but hopefully this can give you enough material for trying it out.
VonC
2009-12-28 10:01:31
It seems to be correct, though I couldn't get it to work in the limited time that I looked at it because I don't know how to get the workspace instance variable. I patched together a bash script that will do what I need for now (I need to run a java project that my java project generates.) But I will need this eventually, so thanks a lot
piggles
2009-12-28 10:07:46
You can get the workspace using ResourcesPlugin.getWorkspace()(Your plugin will need to depend on package org.eclipse.core.resources or plugin org.eclipse.core.resources, depending on how you like to specify dependencies)
Scott Stanchfield
2009-12-28 16:42:56