[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.platform] Re: How to create a workspace programmatically?
|
Here is an example:
(for more examples see the automated tests of the eclipse platform project.)
/**
* Creates a IJavaProject.
*/
static IJavaProject createJavaProject(String projectName, String
binFolderName) throws CoreException {
IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
IProject project= root.getProject(projectName);
if (!project.exists()) {
project.create(null);
} else {
project.refreshLocal(IResource.DEPTH_INFINITE, null);
}
if (!project.isOpen()) {
project.open(null);
}
IPath outputLocation;
if (binFolderName != null && binFolderName.length() > 0) {
IFolder binFolder= project.getFolder(binFolderName);
if (!binFolder.exists()) {
createFolder(binFolder, false, true, null);
}
outputLocation= binFolder.getFullPath();
} else {
outputLocation= project.getFullPath();
}
if (!project.hasNature(JavaCore.NATURE_ID)) {
addNatureToProject(project, JavaCore.NATURE_ID, null);
}
IJavaProject jproject= JavaCore.create(project);
jproject.setOutputLocation(outputLocation, null);
jproject.setRawClasspath(new IClasspathEntry[0], null);
return jproject;
}
private static void addNatureToProject(IProject proj, String natureId,
IProgressMonitor monitor) throws CoreException {
IProjectDescription description = proj.getDescription();
String[] prevNatures= description.getNatureIds();
String[] newNatures= new String[prevNatures.length + 1];
System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
newNatures[prevNatures.length]= natureId;
description.setNatureIds(newNatures);
proj.setDescription(description, monitor);
}
/**
* Creates a folder and all parent folders if not existing.
* Project must exist.
* <code>org.eclipse.ui.dialogs.ContainerGenerator</code> is too heavy
* (creates a runnable)
*/
static void createFolder(IFolder folder, boolean force, boolean local,
IProgressMonitor monitor) throws CoreException {
if (!folder.exists()) {
IContainer parent= folder.getParent();
if (parent instanceof IFolder) {
createFolder((IFolder)parent, force, local, null);
}
folder.create(force, local, monitor);
}
}
/**
* @param project the project to delete.
* @throws CoreException
*/
static void delete(final IJavaProject jproject) throws CoreException {
IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor) throws CoreException {
jproject.setRawClasspath(new IClasspathEntry[0],
jproject.getProject().getFullPath(), null);
for (int i= 0; i < MAX_RETRY; i++) {
try {
IProject tmpProject = jproject.getProject();
synchronized (tmpProject) {
if (tmpProject.exists())
tmpProject.delete(true, true, null);
}
i= MAX_RETRY;
} catch (CoreException e) {
if (i == MAX_RETRY - 1) {
EntireXWorkbenchTestPlugin.log(e);
throw e;
}
try {
Thread.sleep(1000); // sleep a second
} catch (InterruptedException e1) {
}
}
}
}
};
ResourcesPlugin.getWorkspace().run(runnable, null);
}
"Karl-Heinz Damm" <Karl-Heinz.Damm@xxxxxxxxxxxxxxx> wrote in message
news:542707156e078bc1f5efe9935d5db189$1@xxxxxxxxxxxxxxxxxx
> Is there a simple way to create a workspace including one (or more)
> projects programmatically using the Eclipse API. By simple I mean
> something like two API function calls, the first call specifying
> essentially only the location for the new workspace, the second call
> specifying essentially only the location of an existing project or
> directory.
>
> The reason why I'm asking this is that we would like to be able to create
> a workspace with projects from one or more simple configuration files,
> e.g. like it is done in Visual Studio with *.dsw and *.dsp files, and we
> want to save these workspace description files within our CVS repository.
>
>