Home » Eclipse Projects » Eclipse Platform » Building a java project programmatically 
| Building a java project programmatically [message #329335] | 
Thu, 19 June 2008 10:38   | 
 
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 12:49    | 
 
Eclipse User  | 
 | 
 | 
   | 
 
"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 18:21    | 
 
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
 |  
 |  
  |   |   |   |   |   |   
Goto Forum:
 
 Current Time: Tue Nov 04 04:38:24 EST 2025 
 Powered by  FUDForum. Page generated in 0.05876 seconds  
 |