Provided dependency is not added to compile classpath

I am using Gradle 2.2.1.

In order to include a dependency as provided, I have added following configuration to build.gradle file of my root project -

subprojects {
    apply plugin: 'java'
 apply plugin: 'eclipse'
   sourceCompatibility = 1.8
    repositories {
  mavenCentral()
 }
   configurations {
  provided
 }
   sourceSets {
     main {
       compileClasspath += configurations.provided
      }
 }
    dependencies {
  testCompile 'junit:junit:4.11'
 }
  }

Code inside build.gradle file of my java project is as following -

dependencies {
    provided 'org.projectlombok:lombok:1.14.4'
   }

However when I run ‘gradle eclipse’ command, this dependency is not added to compile classpath of my java project for some unknown reason.

Simply configuring the source set is not sufficient enough to inform Eclipse of the additional configuration. You’ll have to configure the ‘eclipse’ plugin. However, you may want to take a look at the Netflix plugin specifically designed for this purpose.

I would like to avoid a third party plugin unless desired behavior can’t be achieved with Gradle provided plugins.

Configuration applied by me has been mentioned as working solutions in various articles for implementing provided dependency. So I was just wondering why its not working in my case.

It seems that I need to add following configuration to my build file -

eclipse.classpath.plusConfigurations += configurations.provided

However when I do so, ‘gradle eclipse’ command does not work -

D:\Shailendra\github-repos>gradle eclipse

FAILURE: Build failed with an exception.

  • Where: Build file ‘D:\Shailendra\github-repos\piston-core\piston-model\build.gradle’ line: 10

  • What went wrong: A problem occurred evaluating project ‘:piston-core:piston-model’. > Cannot change configuration ‘:piston-core:piston-model:provided’ after it has been resolved.

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 6.825 secs

Take a look at the Gradle docs. There is an example there that I think fits your exact need. The only difference I see is that in the documentation example the configuration is being wrapped in a collection ‘[ configurations.provided ]’. I’m not sure how that would affect anything but you might want to give that a try.

I believe not wrapping configuration in collection was causing this problem. It’s working as expected now.

Thank you for your help.

However the new problem is that ‘gradle assemble’ is failing as it is unable to find provided dependencies for compiling source code.

Try having ‘compile’ extend from ‘provided’

configurations {

provided

compile.extendsFrom provided

}

The difference is the way Groovy interprets ‘+= configurations.provided’. There was a change in Groovy 2.x, as described here: http://gradle.org/docs/2.0/release-notes#+=-operator-changes-that-typically-affect-configuration-of-…

Thanks for the explanation, Daz.

“gradle assemble” command is working fine after doing the changes as suggested by you.

Thanks once again.

Well it seems that extending compile from provided is causing another issue now.

In my project I have to create many jars where all these jars should have their compile and runtime dependencies in a lib folder inside the jar. Extending compile from provided is pulling provided dependencies in lib folder which should not happen.

You’ll have to exclude the ‘provided’ dependencies in those cases.

I am not sure how that can be achieved. I am using sync task to copy runtime dependencies to lib folder. I am trying to exclude provided dependencies using following code but still provided dependencies are not excluded -

task copyDependencies(type: Sync) {
 from ([ configurations.runtime ] - [ configurations.provided ])
 into "$buildDir/dependencies/lib"
}

Frankly speaking I am not sure why we are extending compile from provided when these are two different type of dependencies. Also I have following code in my build.gradle script which in my opinion should be sufficient to make provided dependencies available at the time of compiling the code -

sourceSets {
    main {
          compileClasspath += [ configurations.provided ]
    }
}

If this code does not achieve what I have described above then what is the objective behind this code?

The correct way to implement this is to have ‘compile’ extend from ‘provided’. The reason for this is for dependency resolution. Gradle does dependency deconfliction per configuration. That means by simply concatenating two independent configurations you could likely have duplicate, and possibly incompatible, libraries on your classpath.

Thanks you for your response. Can you please suggest what will be proper syntax to exclude provided dependencies in sync task.

As mentioned in one of my previous comments I am already extending compile from provided as suggested by you.

Should be able to do something like this.

task copyDependencies(type: Sync) {

from configurations.compile.minus(configurations.provided)

}

Above code is NOT copying any kind of dependency.

I left out the ‘into’ but I assume you added that?

Yes I had added ‘into’ before testing it.

Can you post what your ‘dependencies {…}’ block looks like?