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

1.1k Views Asked by At

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?

1

There are 1 best solutions below

0
Michael Easter On

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()
    }
}