'How to attach the source to the jar in the Gradle (Gradle 6.3.) build that can be used in eclipse External Dependencies
Problem: I have a gradle project in which I am using let's say 'pocdemo.jar' as gradle dependency. When I try to navigate through the method call hierarchy, it doesn't show the actual code, instead it shows something like shown in image below

I want to have the source attached for the external dependency, I tried to do that by selecting the jar -> properties -> Java Source Attachment, but that didn't allow me to add the location for the source. Now I want to know is there a way that I can use in build.gradle file that will include the source during the jar creation itself. I have the below gradle file for the project that I use as external dependency in my main project.
buildscript {
repositories {
maven {
url = repoUrl
allowInsecureProtocol = true
metadataSources {
mavenPom()
artifact()
}
}
maven {
url = biappsUrl
allowInsecureProtocol = true
metadataSources {
mavenPom()
artifact()
}
}
}
dependencies {
classpath(group: 'org.springframework.boot', name: 'spring-boot-gradle-plugin', version: '2.1.3.RELEASE')
}
}
apply plugin: 'maven-publish'
apply plugin:'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
configurations.all {
exclude group: "commons-logging", module: "commons-logging"
exclude group: "org.springframework.boot", module: "spring-boot-starter-tomcat"
}
configurations {
javadocJar
}
dependencies {
implementation(group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0')
implementation (group: 'org.springframework.retry', name: 'spring-retry')
implementation(group: 'com.google.guava', name: 'guava', version: '27.0.1-jre')
implementation(group: 'org.springframework.boot', name: 'spring-boot-starter-web')
implementation(group: 'org.springframework.boot', name: 'spring-boot-starter-actuator')
implementation group: 'commons-io', name: 'commons-io', version: '2.5'
implementation group: 'commons-httpclient', name: 'commons-httpclient', version: '3.1'
implementation group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.1'
implementation group: 'commons-codec', name: 'commons-codec', version: '1.2'
implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5'
implementation group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.1'
.
. <Some more dependencies>
.
testImplementation(group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.0-beta.5')
testImplementation(group: 'org.mockito', name: 'mockito-core', version: '2.19.0')
testImplementation(group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.0-beta.5')
}
javadoc {
source = sourceSets.main.allJava
classpath = sourceSets.main.compileClasspath
destinationDir = file("${buildDir}/docs/javadoc")
failOnError = true
}
task javadocJar (type: Jar, dependsOn: javadoc){
from javadoc.destinationDir
archiveFileName = "myapps-pco-demo-doc-${version}.jar"
}
jar {
bootJar.enabled = false
jar.enabled = true
dependsOn 'javadocJar'
archiveFileName = provider {
'pocdemo.jar'
}
enabled = true
manifest{
attributes('Sealed': 'true')
}
}
/* Eclipse config */
eclipse {
classpath {
downloadSources = true
downloadJavadoc = true
}
}
Solution 1:[1]
I have the same issue too when using Spring Tool Suite, and I spent a lot of time investigating but cannot find any solutions. But when I use Intellij, it works fine, I can go into the class file and debug it. So please try it with Intellij.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Duong LV |
