Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » Adding project reference programmatically.
Adding project reference programmatically. [message #227006] Wed, 19 November 2008 04:53 Go to next message
padamev297 is currently offline padamev297
Messages: 43
Registered: July 2009
Member
hi

Can anybody help me on how to add project reference(dependency on another
project) programmatically.

I found out that ICConfigurationDescription.setReferenceInfo() is the way
to do the same, but didn't get any more help.


Thanks in advance
Padam
Re: Adding project reference programmatically. [message #227013 is a reply to message #227006] Wed, 19 November 2008 05:06 Go to previous messageGo to next message
Eclipse User
Originally posted by: subs._nospam_consertum.com

This is how I do it:
private void setDependencies(IProject project, IProject[] dependsOn) {
for (int i = 0; i < dependsOn.length; i++) {
IProject refProj = dependsOn[i];
try {
IProjectDescription desc = project.getDescription();
IProject[] refs = desc.getReferencedProjects();
IProject[] newRefs = new IProject[refs.length + 1];
System.arraycopy(refs, 0, newRefs, 0, refs.length);
newRefs[refs.length] = refProj;
desc.setReferencedProjects(newRefs);
project.setDescription(desc, monitor);
} catch (CoreException e) {
}
}
}

padamev297 wrote:
> hi
>
> Can anybody help me on how to add project reference(dependency on
> another project) programmatically.
>
> I found out that ICConfigurationDescription.setReferenceInfo() is the
> way to do the same, but didn't get any more help.
>
>
> Thanks in advance
> Padam
>


--
Derek
Re: Adding project reference programmatically. [message #227026 is a reply to message #227006] Wed, 19 November 2008 09:26 Go to previous messageGo to next message
padamev297 is currently offline padamev297
Messages: 43
Registered: July 2009
Member
Got the answer:

//Setting project1 as project referenced by project2.
IProject[] IPrjReference={project1};
IProjectDescription prjDescriptor = Project2.getDescription();
prjDescriptor.setReferencedProjects(IPrjReference);
Project2.setDescription(prjDescriptor, null);


Thanks
Padam
Re: Adding project reference programmatically. [message #1101622 is a reply to message #227026] Wed, 04 September 2013 14:25 Go to previous messageGo to next message
Kevin Regan is currently offline Kevin Regan
Messages: 33
Registered: May 2013
Member
I have programmatically created two projects - one which references the other in the way described above - but it does not solve the problem I expected it to solve.

Basically, project #2, which now references project #2 is trying to import and use classes that are defined in project #1. Even with this project reference set up it complains that it cannot find the class. I believe what I need is the equivalent of going to the project #2, right clicking property and going to Java Build Path, clicking on Projects tab and adding project #1.

I cannot figure out how to set this up programmitically however. This is what I have tried:

		IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(m_nodeName);
		
		description.setLocationURI(new File(m_projectLocation).toURI());
		
		if (m_parentProject != null) {
			
			assert (m_parentProject.getProject() != null) : "Cannot build project if parent project is not built";
			
			description.setReferencedProjects(new IProject[] { m_parentProject.getProject() } );	
			
			// try to get the default build configuration
			IBuildConfiguration parentBuildConfig = m_parentProject.getProject().getBuildConfig("");
			description.setBuildConfigReferences("", new IBuildConfiguration[] { parentBuildConfig});
		
		}
				
		description.setNatureIds(new String[] {JavaCore.NATURE_ID, JavaNodeProjectNature.NATURE_ID});
		
		createProject(m_nodeName, description);


createProject function:
protected void createProject(String projectName, IProjectDescription description) throws CoreException, ProjectExistsException { 
	
		m_project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);

		if (m_project.exists()) {
			throw new ProjectExistsException("Project " + projectName + " already exists");
		}
				
		m_project.create(description, EclipseSupport.getProgressMonitor());
		m_project.open(EclipseSupport.getProgressMonitor());		
	}


According to the documentation I believe I am adding the parent projects default build configuration (default build configuration always has empty string name) as a referenced build configuration to the child projects default build configuration.

I have also tried doing it after the project is created (which is done in a separate function the standard way) by doing project.getDescription, adding the build config, and then project.setDescription - but that doesn't work either.

Does anyone know what, if anything, I am doing wrong?

Thanks for your help.
Re: Adding project reference programmatically. [message #1103296 is a reply to message #1101622] Fri, 06 September 2013 11:36 Go to previous message
Kevin Regan is currently offline Kevin Regan
Messages: 33
Registered: May 2013
Member
In case anyone looks here trying to figure out the problem I mentioned above - the build configuration is apparently something else - in order to get a project dependency (i.e. build path in properties for a project as opposed to project references addressed in the first few posts of this thread), you just simply use the old classpath entry method:


  IClasspathEntry projectDependency = JavaCore.newProjectEntry(projectLocation);

  project.setRawClasspath(new IClasspathEntry[] { projectDependency });  // or something like that whatever... 



Previous Topic:/bin/sh: arm-none-linux-gnueabi-g++: not found
Next Topic:Can't Get Include Paths to Work in CDT
Goto Forum:
  


Current Time: Wed Apr 16 12:07:03 EDT 2014

Powered by FUDForum. Page generated in 0.02320 seconds