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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With