Difference between dependencies within buildscript closure and core

ok can’t really see the difference why we declare a dependency within the buildscript or the other dependency closure. Say my groovy codebase uses SLF4J, i comment out:

@Grab(group=‘org.slf4j’, module=‘slf4j-api’, version=‘1.6.1’) @Grab(group=‘ch.qos.logback’, module=‘logback-classic’, version=‘0.9.28’)

in my groovy code and need to add a dependency in my build.gradle to support that logic - how do i choose where to add them in the build.gradle script ?? thx

Dependencies within the ‘buildscript { }’ block are placed on the classpath of the build script itself. Dependency blocks outside the ‘buildscript { }’ block are project dependencies, that is, they are added to your project code’s classpath. Configurations are also separated this way. If I am only using a dependency in my buildscript (i.e. only from the build.gradle file) then it should be in a ‘buildscript { }’ block. Dependencies used in both project code and build scripts will effectively need to be listed twice.

Additionally, if you are building with Gradle you should not be using Grape for your dependencies. You should declare all dependencies within your Gradle build script.

Ok thanx for that. Then as i understand this, if my groovy script needs to use the Slf4J logging feature, the declaration for it should be in the core gradle dependency closure - not the buildscript {} ?? So essentially any thing my code needs to make it happy dependency-wise, should be there.

and yes i have learned the hard way that i must comment out my groovy @Grape{} stuff or else i get a terse fail about ivy like:

/Users/jim/Dropbox/Asciidoctor/HarvesterProject $ gradlew clean build Parallel execution is an incubating feature. :clean :compileJava UP-TO-DATE :compileGroovy FAILED

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ‘:compileGroovy’. > org/apache/ivy/core/report/ResolveReport

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

BUILD FAILED

To compile a script using @Grab you need to add the ivy dependency to the compile scope. It is bundled in the Groovy distribution, so you don’t have to explicitly add it to the classpath.

That said, I am not sure why are you trying to compile a script - i.e. you either compile and ship a jar with dependencies, or ship a source file with @Grab annotations.

I sometimes do add the dependency, but that is to have it in the generated IDE projects, so I can debug my scripts in IDE. In such cases, I also need to take extra steps to exclude it from my distribution artifacts (provided scope is not available in Gradle).

Then as i understand this, if my groovy script needs to use the Slf4J logging feature, the declaration for it should be in the core gradle dependency closure - not the buildscript {} ??

Correct.

Most excellent !! Do appreciate that news.

Have only just discovered the ‘watch’ feature and it works wonderfully on one of my laptops with jdk1.7 installed though when i use teh same build.gradle script via Dropbox on an older aptop with jdk 1.6 then i am force to comment out the ‘watch’ closure to use the script. Just a bit of a bother :-}

thx