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 :

  1. Gradle 2.4
  2. Eclipse 4.4

1. Gradle Eclipse Project

1.1 A web project build with Gradle and development in Eclipse IDE.

build.gradle

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.

gradle-eclipse-source-not-found

And the “Web App Libraries” has the top priority to load.

gradle-eclipse-source-not-found-1

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

build.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.

gradle-eclipse-source-not-found-2
Note
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.

References

  1. WAR + EclipseWtpPlugin Shadows Source Attachments with Web App Libraries Container
  2. Why is Eclipse not attaching 3rd party libs source?

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Sanjib Pal Recent comment authors
newest oldest most voted
Sanjib Pal
Guest
Sanjib Pal

Thank You for the hack. Saved a lot of my time.