Groovy @Grab syntax equivalent for gradle?


#1

Is there an equivalent syntax for a gradle script that allows something similar to the groovy @Grab convention? eg:

@Grab( 'com.bloidonia:groovy-common-extensions:0.5.3' )

I know I can use this in a groovy class and call that from gradle, but the extra wrapper truly adds nothing, so I’d like to avoid it if possible.


(Peter Niederwieser) #2

The Gradle equivalent is to declare build script dependencies inside the ‘buildscript’ block. For details see the Gradle User Guide.


#3

From the docs it looks like this should work.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath group: 'com.bloidonia', name: 'groovy-common-extensions', version: '0.5.3'
    }
}
  task listcp << {
        println "classpath = ${configurations.compile.collect {File file -> file.name}}"
}

I get:

Execution failed for task ‘:listcp’. > Could not resolve all dependencies for configuration ‘:compile’.

Could not find com.bloidonia:groovy-common-extensions:0.5.3.

Required by:

:work:unspecified


#4

This looks like it should work from the docs:

apply plugin: 'groovy'
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath group: 'com.bloidonia', name: 'groovy-common-extensions', version: '0.5.3'
    }
}
  dependencies {
        compile gradleApi()
        compile localGroovy()
        compile group: 'com.bloidonia', name: 'groovy-common-extensions', version: '0.5.3'
}
  task listcp(dependsOn: configurations.compile) << {
        println "classpath = ${configurations.compile.collect {File file -> file.name}}"
}

but it can’t find groovy-common-extensions

  • What went wrong: Execution failed for task ‘:listcp’. > Could not resolve all dependencies for configuration ‘:compile’.

Could not find com.bloidonia:groovy-common-extensions:0.5.3.

Required by:

:testcp:unspecified

If I remove the dependency I get a classpath dump, but with it I get the above error.


#5

Here’s a working snippet in case anyone else runs into this.

import com.bloidonia.groovy.extensions.FileExtensionMethods
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath group: 'com.bloidonia', name: 'groovy-common-extensions', version: '0.5.3'
    }
}
  task unpack << {
        FileExtensionMethods.unzip(new File('test.zip'), new File('unpackDir'))
//
      File('test.zip').unzip(new File('unpackDir2'))
}

The one remaining quirk is that the groovy-common-extensions library seems not to actually extend File as stated in the docs as the commented out line above does not work:

https://github.com/timyates/groovy-common-extensions

// extracts the files to '/home/bill/'
Collection<File> extractedFiles = new File('/var/tmp.zip').unzip(new File('/home/bill'))

Execution failed for task ‘:unpack’. > Could not find method File() for arguments [test.zip] on root project ‘testzip’.

and I had to resort to the less pleasing syntax of explicitly specifying FileExtensionMethods rather than File.

So it’w working, but I’d like to know why the groovy-common-extensions library’s File extension mechanism is not.


(Tim Yates) #6

I think:

File('test.zip').unzip(new File('unpackDir2'))

Would need to be

new File('test.zip').unzip(new File('unpackDir2'))

And Gradle uses Groovy 1.8.6, which doesn’t have extension methods so even then it wouldn’t work…(as it says in the readme for extension methods: “Obviously requires at least Groovy 2.0.5 (so that the extension system exists)”)