2

Is it possible to use @Grab inside a Gradle build.gradle file?

My understanding is that Gradle build scripts are written in Groovy and Grape/@Grab is built into Groovy.

But if i attempt to add a @Grab annotation into a build.gradle file it doesn't work.

e.g. adding:

@Grab(group='org.springframework', module='spring-orm', version='3.2.5.RELEASE')
import org.springframework.jdbc.core.JdbcTemplate

it gives me the error

org.gradle.groovy.scripts.ScriptCompilationException: Could not compile build file.

Is this possible?

M. Justin
  • 14,487
  • 7
  • 91
  • 130
Boy Pike
  • 45
  • 1
  • 6

1 Answers1

4

According to this post, the answer is no, we can't use @Grab.

However, there is an equivalent way to declare a dependency and add it to the classpath, as shown below (usage for the example: gradle go):

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath group: 'org.springframework', name: 'spring-orm', version: '3.2.5.RELEASE'
    }
}

import org.springframework.jdbc.core.JdbcTemplate

task go() {
    doLast {
        println 'TRACER : ' + JdbcTemplate.class.getSimpleName()
    }
}
Michael Easter
  • 23,733
  • 7
  • 76
  • 107