Programmatically add source folder from Eclipse plugin

I am developing a m2e connector for the maven plugin that actually generates some sources. I need to add the generated sources (folder) to the workspace as the source folder.

I used JavaCore to edit the .classpath file:

    IJavaProject javaProject = JavaCore.create(proj);
    IClasspathEntry[] entries = javaProject.getRawClasspath();

    IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
    System.arraycopy(entries, 0, newEntries, 0, entries.length);

    Path myPath = new Path("target/generated-sources");
    IClasspathEntry myEntry = JavaCore.newSourceEntry(myPath);

    newEntries[entries.length] = JavaCore.newSourceEntry(myEntry.getPath());
    javaProject.setRawClasspath(newEntries, null);

But this code doesn't work, it says: The path for IClasspathEntry must be absolute

If I tried to use the absolute path, it was written in .classpath, but in eclipse it did not appear as the source folder.

Does anyone have any suggestions? It should be an easy task, but I cannot figure out how to solve it.

+5
source share
2 answers

... , ...

IJavaProject javaProject = JavaCore.create(proj);
IClasspathEntry[] entries = javaProject.getRawClasspath();

IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
System.arraycopy(entries, 0, newEntries, 0, entries.length);

IPath srcPath= javaProject.getPath().append("target/generated-sources");
IClasspathEntry srcEntry= JavaCore.newSourceEntry(srcPath, null);

newEntries[entries.length] = JavaCore.newSourceEntry(srcEntry.getPath());
javaProject.setRawClasspath(newEntries, null);

.classpath:    

+5

JavaCore.newSourceEntry(...) JavaCore.newProjectEntry(...).

0

All Articles