38

Once after I create a Gradle project in IntelliJ using the default gradle wrapper and create directories option I see the project structure gets created with build.gradle file.

IntelliJ tips me to "You can configure Gradle wrapper to use distribution with sources. It will provide IDE with Gradle API/DSL documentation" - but I am not able to attach the sources even after clicking "Ok, apply suggestion". The Gradle project is getting refreshed but the sources are not attached.

We are using a Nexus repository.

andrybak
  • 2,129
  • 2
  • 20
  • 40
John Jai
  • 3,463
  • 6
  • 25
  • 32

3 Answers3

6

To improve on @anon58192932 answer you can use only gradleVersion and distributionType fields of Wrapper task and don't need to manually create distributionUrl which is more error prone since you could change gradle version in one place, but not in the other.

task wrapper(type: Wrapper) {
    gradleVersion = '4.2'
    distributionType = Wrapper.DistributionType.ALL
}

@edit gradle 4.8+ will produce error for above. Use instead

wrapper {
    gradleVersion = '4.2'
    distributionType = Wrapper.DistributionType.ALL
}
Kinmarui
  • 593
  • 7
  • 17
  • This is giving me `No such property: DistributionType for class: org.gradle.api.tasks.wrapper.Wrapper`. Here's my new gradle code: `allprojects { task wrapper(type: Wrapper) { gradleVersion = "4.4" distributionType = Wrapper.DistributionType.ALL } }` my previous code is below. – fIwJlxSzApHEZIl Dec 11 '17 at 23:10
  • 2
    With gradle 4.8+, this will now result in an error, which can be resolved by specifying `wrapper { ... }`. See https://stackoverflow.com/a/54741656/1325508. – LBC Aug 24 '20 at 19:45
4

Sounds like some IntelliJ problem. To do this manually, change gradle-bin to gradle-all in $projectDir/gradle/wrapper/gradle-wrapper.properties.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • 1
    which property should I change? there are 5 properties: `distributionBase, distributionPath, zipStoreBase, zipStorePath and distributionUrl` – John Jai Oct 30 '14 at 20:29
  • Thanks, I don't see any `gradle-bin`. But the `distributionUrl` property contains : `/gradle/2.1-all/gradle-2.1-all.zip` – John Jai Oct 30 '14 at 20:34
  • 2
    Then you already have the distribution with sources. Most likely that change was made by IntelliJ. – Peter Niederwieser Oct 30 '14 at 20:35
  • 4
    When you run the "gradle wrapper" task, it overwrites with "-bin" again. Is there some way that I can specify that I perpetually want the "-all" variant from within build.gradle file? – stolsvik Dec 15 '14 at 10:18
  • See `$projectDir/gradle/wrapper/gradle-wrapper.properties`. – Peter Niederwieser Dec 15 '14 at 13:43
  • @PeterNiederwieser Is there a way to configure the wrapper to use `-all` instead for `-bin`? – neu242 Feb 19 '15 at 09:40
  • That's what my answer is all about. – Peter Niederwieser Feb 19 '15 at 16:04
  • 2
    @stolsvik Use the [`distributionUrl`](http://gradle.org/docs/current/dsl/org.gradle.api.tasks.wrapper.Wrapper.html#org.gradle.api.tasks.wrapper.Wrapper:distributionUrl) property in the wrapper task. – Andrew McKinlay Mar 31 '15 at 20:06
  • Great! Any chance dependent sources could also be included, such as groovy-all? It would help in writing gradle build scripts. – Ivan Balashov Aug 19 '15 at 09:30
  • @PeterNiederwieser - still confused sorry... my gradle-wrapper.properties looks like this:- #Tue Mar 22 23:35:55 GMT 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-bin.zip what do I need to change to enable code completion? – Dave00Galloway Mar 24 '16 at 10:35
  • The confusion here with the `gradle wrapper` task is that it creates the `gradle-wrapper.properties` file. The latter specifies whether to use a gradle distro with or without sources (`-bin` vs `-all`). So either use the `distributionUrl` cli flag on the `gradle wrapper` task or edit it manually in the `gradle-wrapper.properties` file. – Alex Aug 31 '22 at 15:46
  • However, this all won't work for our team where we host our own distributions (with custom init scripts and properties). We don't publish them with sources so I still need a way to attach those in IntelliJ? – Alex Aug 31 '22 at 15:50
4

Peter's answer is not correct. The gradle-wrapper.properties and gradle-wrapper.jar files are generated by the 'wrapper' task which is shown in IntelliJ as a build task to perform.

When wrapper is performed it will build out the .jar and .properties file based on your settings therefore you should NOT be editing these files manually as they are GENERATED.

You can call the wrapper task manually very easily in your project folder: ./gradlew wrapper for *nix platforms. This will generate updated gradle-wrapper.properties and gradle-wrapper.jar files for your project.

If you want to specify to use all sources or not you can easily modify your main build.gradle file with the following:

allprojects {
    task wrapper(type: Wrapper) {
        gradleVersion = '4.1'
        distributionUrl = 'https://services.gradle.org/distributions/gradle-4.1-bin.zip'
    }
}

Substitute gradle-4.1-bin.zip for gradle-4.1-all.zip to include gradle sources, documentation, and examples.

fIwJlxSzApHEZIl
  • 11,861
  • 6
  • 62
  • 71