Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Building a java project programmatically
Building a java project programmatically [message #329335] Thu, 19 June 2008 14:38 Go to next message
Eclipse User
Originally posted by: pschoenb.gmx.de

Hi,

Im am writing some JUnit tests for a plugin. In order to test the
plugin, I am programmatically creating a Java project, specifying
required bundles and stuff in setUp(), and I am deleting the project in
tearDown(), in order to have a clean environment for each test.

Auto building is enabled, however, my tests fail, because building is
not yet finished, and thus, the full classpath is not yet available. If
I remove the project deletion in tearDown() all but the first test
succeed. This shows that the incomplete build causes my tests to fail.

How can I either wait for the automatic build to finish or
programatically force a build?

--
Regards,
Patrick
Re: Building a java project programmatically [message #329339 is a reply to message #329335] Thu, 19 June 2008 16:49 Go to previous messageGo to next message
Walter Harley is currently offline Walter Harley
Messages: 847
Registered: July 2009
Senior Member
"Patrick Schoenbach" <pschoenb@gmx.de> wrote in message
news:umxjusf31s1u$.13ojqkilzynls$.dlg@40tude.net...
> Hi,
>
> Im am writing some JUnit tests for a plugin. In order to test the
> plugin, I am programmatically creating a Java project, specifying
> required bundles and stuff in setUp(), and I am deleting the project in
> tearDown(), in order to have a clean environment for each test.
>
> Auto building is enabled, however, my tests fail, because building is
> not yet finished, and thus, the full classpath is not yet available. If
> I remove the project deletion in tearDown() all but the first test
> succeed. This shows that the incomplete build causes my tests to fail.
>
> How can I either wait for the automatic build to finish or
> programatically force a build?


You may want to disable autobuild, and build explicitly, e.g.
ResourcesPlugin.getWorkspace().build(IncrementalProjectBuild er.FULL_BUILD,
null);

You can look at the JDT tests for examples of what you're trying to do. A
good starting point would be the org.eclipse.jdt.core.tests.builder project;
look for BuilderTests.java and TestingEnvironment.java. There's probably a
lot of code in there you don't need, but you should be able to find examples
of everything you do need.
Re: Building a java project programmatically [message #329341 is a reply to message #329339] Thu, 19 June 2008 22:21 Go to previous messageGo to next message
Eclipse User
Originally posted by: pschoenb.gmx.de

On Thu, 19 Jun 2008 09:49:03 -0700, Walter Harley wrote:

> "Patrick Schoenbach" <pschoenb@gmx.de> wrote in message
>> How can I either wait for the automatic build to finish or
>> programatically force a build?
>
>
> You may want to disable autobuild, and build explicitly, e.g.
> ResourcesPlugin.getWorkspace().build(IncrementalProjectBuild er.FULL_BUILD,
> null);

Doesn't work yet. My relevant code looks like this:

public static IProject createProject(final String projectName,
final List<String> srcFolders,
final List<IProject> referencedProjects,
final Set<String> requiredBundles,
final List<String> exportedPackages,
final IProgressMonitor progressMonitor) {
try {
IProject project = null;
progressMonitor.beginTask("", 10);
progressMonitor.subTask("Creating project " + projectName);
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
project = workspace.getRoot().getProject(projectName);
final IWorkspaceDescription description =
workspace.getDescription();
description.setAutoBuilding(false);
workspace.setDescription(description);

if (project.exists()) {
// Clean up any old project information.
project.delete(true, true, new SubProgressMonitor(
progressMonitor, 1));
}

final IJavaProject javaProject = JavaCore.create(project);
final IProjectDescription projectDescription =
workspace.newProjectDescription(projectName);
projectDescription.setLocation(null);
project.create(projectDescription, new SubProgressMonitor(
progressMonitor, 1));
final List<IClasspathEntry> classpathEntries =
new ArrayList<IClasspathEntry>();
if (referencedProjects.size() != 0) {
projectDescription.setReferencedProjects(referencedProjects
.toArray(new IProject[referencedProjects.size()]));
for (final IProject referencedProject : referencedProjects) {
final IClasspathEntry referencedProjectClasspathEntry =
JavaCore.newProjectEntry(referencedProject
.getFullPath());
classpathEntries.add(referencedProjectClasspathEntry);
}
}

projectDescription.setNatureIds(new String[] { JavaCore.NATURE_ID,
"org.eclipse.pde.PluginNature" });

final ICommand java = projectDescription.newCommand();
java.setBuilderName(JavaCore.BUILDER_ID);

final ICommand manifest = projectDescription.newCommand();
manifest.setBuilderName("org.eclipse.pde.ManifestBuilder");

final ICommand schema = projectDescription.newCommand();
schema.setBuilderName("org.eclipse.pde.SchemaBuilder");

projectDescription.setBuildSpec(new ICommand[] { java, manifest,
schema });

project.open(new SubProgressMonitor(progressMonitor, 1));
project.setDescription(projectDescription, new SubProgressMonitor(
progressMonitor, 1));

Collections.reverse(srcFolders);
for (final String src : srcFolders) {
final IFolder srcContainer = project.getFolder(src);
if (!srcContainer.exists()) {
srcContainer.create(false, true, new SubProgressMonitor(
progressMonitor, 1));
}
final IClasspathEntry srcClasspathEntry =
JavaCore.newSourceEntry(srcContainer.getFullPath());
classpathEntries.add(0, srcClasspathEntry);
}

classpathEntries.add(JavaCore.newContainerEntry(new Path(
"org.eclipse.jdt.launching.JRE_CONTAINER")));
classpathEntries.add(JavaCore.newContainerEntry(new Path(
"org.eclipse.pde.core.requiredPlugins")));

javaProject.setRawClasspath(classpathEntries
.toArray(new IClasspathEntry[classpathEntries.size()]),
new SubProgressMonitor(progressMonitor, 1));

javaProject.setOutputLocation(
new Path("/" + projectName + "/bin"),
new SubProgressMonitor(progressMonitor, 1));
createManifest(projectName, requiredBundles, exportedPackages,
progressMonitor, project);
createBuildProps(progressMonitor, project, srcFolders);
workspace.build(IncrementalProjectBuilder.FULL_BUILD, null);
return javaProject.getProject();
} catch (final JavaModelException e) {
Log.logError("Java Model Exception", e);
} catch (final CoreException e) {
Log.logError("Core Exception", e);
} finally {
progressMonitor.done();
}
return null;
}

What's wrong?

--
Regards,
Patrick
Re: Building a java project programmatically [message #329379 is a reply to message #329341] Sun, 22 June 2008 09:19 Go to previous messageGo to next message
Walter Harley is currently offline Walter Harley
Messages: 847
Registered: July 2009
Senior Member
"Patrick Schoenbach" <pschoenb@gmx.de> wrote in message
news:1ud3gg0ixzol0$.1nf79ch8psg3o$.dlg@40tude.net...
> Doesn't work yet. My relevant code looks like this:
> [...]

Hmm, looks pretty good to me, though I didn't do a line-by-line comparison
against the JDT test stuff.

Can you say more about the failure you're getting?
Re: Building a java project programmatically [message #329390 is a reply to message #329379] Sun, 22 June 2008 17:32 Go to previous messageGo to next message
Eclipse User
Originally posted by: pschoenb.gmx.de

On Sun, 22 Jun 2008 02:19:42 -0700, Walter Harley wrote:

> "Patrick Schoenbach" <pschoenb@gmx.de> wrote in message
> news:1ud3gg0ixzol0$.1nf79ch8psg3o$.dlg@40tude.net...
>> Doesn't work yet. My relevant code looks like this:
>> [...]
>
> Hmm, looks pretty good to me, though I didn't do a line-by-line comparison
> against the JDT test stuff.
>
> Can you say more about the failure you're getting?

Fixed meanwhile.

--
Regards,
Patrick
Re: Building a java project programmatically [message #329392 is a reply to message #329390] Mon, 23 June 2008 00:11 Go to previous messageGo to next message
Walter Harley is currently offline Walter Harley
Messages: 847
Registered: July 2009
Senior Member
"Patrick Schoenbach" <pschoenb@gmx.de> wrote in message
news:182mzdtkigfz8$.1puja60qlboa6.dlg@40tude.net...
> Fixed meanwhile.

For the benefit of others who chance upon this thread, can you say what you
did to fix it? (Was it just a bug in your code, or did you find a
workaround for an Eclipse problem, or ...?)
Re: Building a java project programmatically [message #528850 is a reply to message #329390] Thu, 22 April 2010 07:20 Go to previous messageGo to next message
AJ  is currently offline AJ
Messages: 77
Registered: July 2009
Member
So, how did you fix it?
Re: Building a java project programmatically [message #1067734 is a reply to message #329335] Wed, 10 July 2013 10:56 Go to previous message
Tilak Sharma is currently offline Tilak Sharma
Messages: 48
Registered: July 2009
Member
I also wanted to create a Plugin project programmatically.

I finalized two approaches and took the first one for my case:

1) Create all the contents manually (manifest file, source folders, build.properties, .classpath, etc) following this blog :
I followed these links:
ProjectWizard
and
Eclipse Helper

2) Use Eclipse PDE CORE PROJECT API

Example of this code usage is here:
SampleProjectSupport
SampleProjectGenerator
ProjectGenerationHelper


I did not try the second option yet, but it looks more promising since it is Eclipse API.


Regards,
Tilak
Previous Topic:SWT_AWT new_shell: Cannot type in the Text
Next Topic:org.eclipse.swt.SWTError: XPCOM error -2147024809
Goto Forum:
  


Current Time: Fri Oct 31 22:25:52 GMT 2014

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

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