Gradle eclipse plugin – unable to attach source code
The source code is attached to the JAR, but Eclipse is keep showing “Source not found” while I jumped to the source via “F3 – Open Declaration”.
Tools tested :
- Gradle 2.4
- Eclipse 4.4
1. Gradle Eclipse Project
1.1 A web project build with Gradle and development in Eclipse IDE.
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
//...
1.2 Generate the Eclipse project settings.
$ gradle eclipse
2. Problem – Eclipse Classpath
Review the generated Eclipse classpath, Java Build Path. Gradle create another “Web App libraries” and assign the entire dependencies into it, but without the source code.
And the “Web App Libraries” has the top priority to load.
This is the reason that caused the “Source not found” error message.
2. Solution
To fix it, put the “Web App” libraries to the bottom of the build path.
This Gradle script will always reorder the “Web App libraries” to bottom each time you generate the Eclipse classpath file via Gradle
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
eclipse.classpath.file {
withXml { xml ->
def node = xml.asNode()
node.remove( node.find { it.@path == 'org.eclipse.jst.j2ee.internal.web.container' } )
node.appendNode( 'classpathentry', [ kind: 'con', path: 'org.eclipse.jst.j2ee.internal.web.container', exported: 'true'])
}
}
Credit to Andreas Kuhrwahl, see this thread.
I wish that Gradle team would fix this issue soon, a bit funny to put a hack in
build.gradle
to just attach the source code.
Thank You for the hack. Saved a lot of my time.