Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How to create a workspace programmatically?
How to create a workspace programmatically? [message #330169] Thu, 17 July 2008 18:52 Go to next message
Karl-Heinz Damm is currently offline Karl-Heinz DammFriend
Messages: 14
Registered: July 2009
Junior Member
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.
Re: How to create a workspace programmatically? [message #330180 is a reply to message #330169] Fri, 18 July 2008 11:49 Go to previous messageGo to next message
Martin Skorsky is currently offline Martin SkorskyFriend
Messages: 112
Registered: July 2009
Senior Member
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@bsk-germany.com> wrote in message
news:542707156e078bc1f5efe9935d5db189$1@www.eclipse.org...
> 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.
>
>
Re: How to create a workspace programmatically? [message #330295 is a reply to message #330180] Thu, 24 July 2008 15:18 Go to previous messageGo to next message
Karl-Heinz Damm is currently offline Karl-Heinz DammFriend
Messages: 14
Registered: July 2009
Junior Member
Thank you for the information.

However, I couldn't find a sample of how to create an empty workspace
programmatically, neither in your code sample nor in one of the following
sources:

http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.core.te sts.resources/src/org/eclipse/core/tests/resources/IWorkspac eRootTest.java?view=co
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.core.te sts.resources/src/org/eclipse/core/tests/resources/IWorkspac eTest.java?view=co

http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.ui.ide/ src/org/eclipse/ui/internal/ide/actions/OpenWorkspaceAction. java?view=co
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.ui.ide. application/src/org/eclipse/ui/internal/ide/application/IDEA pplication.java?view=co

Although there are a lot of samples of how to create projects
programmatically, there is no one for the actual workspace creation.

Would anybody please give me another hint.


Martin Skorsky wrote:

> 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@bsk-germany.com> wrote in message
> news:542707156e078bc1f5efe9935d5db189$1@www.eclipse.org...
>> 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.
>>
>>
Re: How to create a workspace programmatically? [message #330320 is a reply to message #330295] Fri, 25 July 2008 09:44 Go to previous message
Mariot Chauvin is currently offline Mariot ChauvinFriend
Messages: 174
Registered: July 2009
Senior Member
Hi Karl,

As Eclipse use a single workspace at runtime, I think you simply need to restart Eclipse with the right option.

from http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse. platform.doc.isv/reference/misc/runtime-options.html "data" option seems the right one.

Regards,

Mariot


Karl-Heinz Damm a écrit :
> Thank you for the information.
>
> However, I couldn't find a sample of how to create an empty workspace
> programmatically, neither in your code sample nor in one of the
> following sources:
>
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.core.te sts.resources/src/org/eclipse/core/tests/resources/IWorkspac eRootTest.java?view=co
>
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.core.te sts.resources/src/org/eclipse/core/tests/resources/IWorkspac eTest.java?view=co
>
>
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.ui.ide/ src/org/eclipse/ui/internal/ide/actions/OpenWorkspaceAction. java?view=co
>
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.ui.ide. application/src/org/eclipse/ui/internal/ide/application/IDEA pplication.java?view=co
>
>
> Although there are a lot of samples of how to create projects
> programmatically, there is no one for the actual workspace creation.
>
> Would anybody please give me another hint.
>
>
> Martin Skorsky wrote:
>
>> 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@bsk-germany.com> wrote in message
>> news:542707156e078bc1f5efe9935d5db189$1@www.eclipse.org...
>>> 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.
>>>
>>>
>
Previous Topic:Change configuration location dynamically? Best practice?
Next Topic:"Resulting configuration does not contain the platform" -- how to debug?
Goto Forum:
  


Current Time: Sat Aug 17 11:07:48 GMT 2024

Powered by FUDForum. Page generated in 0.02907 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top