Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add source folder from Eclipse plugin

I am developing m2e connector for out maven plugin, which actually generates some sources. I need to add generated sources(folder) to workspace as source folder.

I used JavaCore for edit .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: Path for IClasspathEntry must be absolute

If I tried to use absolute path, it has been written to .classpath but in eclipse it wasn't displayed as source folder.

Have anyone any suggestion? It should be easy task but I cannot figure out how to solve it.

like image 874
Jan Záruba Avatar asked Nov 03 '11 11:11

Jan Záruba


1 Answers

Problem solved...it was easier then I expected...

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);

And this will add source entry to .classpath file:

like image 89
Jan Záruba Avatar answered Jan 22 '23 02:01

Jan Záruba