How to attach gradle-api sources in IntelliJ

Viewed 786

I'm working on a custom Gradle plugin. For some reason IntelliJ is unable to find the sources of the gradle-api artifact and only shows the decompiled .class file. I am already using the -all distribution of the Gradle Wrapper (which includes some sources, but apparently not the ones I need right here). Clicking Download... results in an error:

Sources not found: Sources for 'gradle-api-6.5.1.jar' not found

How do I correctly attach/choose sources for gradle-api in IntelliJ?

IntelliJ

EDIT:

I have a minimal Gradle plugin with code like that (taken from the official samples):

plugins {
    id 'java-gradle-plugin'
}

repositories {
    jcenter()
}

dependencies {
    testImplementation 'junit:junit:4.13'
}

gradlePlugin {
    // ...
}
3 Answers

According to this excellent manual you should add gradleApi() as a runtimeOnly dependency:

dependencies {
   //...
   runtimeOnly(gradleApi())
   

I guess that, the default Intellij config use gradle from gradle-wrapper.properties file will use /gradle/wrapper/gradle-wrapper.jar, but it doesn't contain source code. what you need is a jar like gradle-wrapper-all.jar. But I don't know how to let Gradle redownload that. Just setting Wrapper.DistributionType.ALL is not working.

Solution

  1. set Wrapper.DistributionType.ALL
wrapper {
    jarFile = file(System.getProperty("user.dir") + '/gradle/wrapper/gradle-wrapper.jar')
    gradleVersion = '6.7.1'
    distributionType = Wrapper.DistributionType.ALL
}
  1. I download Gradle, and use it. Set two things here and refresh it.

enter image description here

Here is the source code, the version is right and with all in the name (gradle-6.7.1-all):

enter image description here

  1. delete gradle dir
  2. run "gradle wrapper"
  3. check the suffix "-all" in the file gradle/wrapper/gradle-wrapper.properties sample: distributionUrl=https://services.gradle.org/distributions/gradle-7.5-all.zip
Related