The ticket subject is the configuration of a Gradle build script.
I want to be able to use a Groovy script without making reference
in the Gradle project to which I attached it.
I want to be able to run this Groovy script outside of Gradle
without the Ivy annotations for external dependency retrieval
do not cause performance or compilation problems
of the Gradle build script.
To illustrate the configuration I rely on two scripts:
a Gradle build script and a Groovy script.
The Groovy script contains at the beginning at line 2,
an instruction to pull an external dependency
using the dependency manager ivy.

#!/usr/bin/env groovy
@Grab("commons-io:commons-io:2.6")

import org.apache.commons.io.FileUtils

static String getSeparator() {
    System.getProperty("file.separator")
}

String from = "${System.getProperty("user.home")}${separator}.config${separator}transmission${separator}torrents"
String to = "${System.getProperty("user.home")}${separator}Documents${separator}torrents_completed"

File fromDlDir = new File("${System.getProperty("user.home")}${separator}Téléchargements")
File fromDir = new File(from)
File toDir = new File(to)

Collection<File> torrentFiles = FileUtils.listFiles(
        fromDir,
        ["torrent"] as String[],
        false)
torrentFiles.addAll(FileUtils.listFiles(
        fromDlDir,
        ["torrent", "torrent.added"] as String[],
        false))


torrentFiles.empty ?: torrentFiles.each { it ->
    FileUtils.copyFileToDirectory(it, toDir)
}
println torrentFiles.size()

In the build.gradle file, now let’s see how to harmonize
the two dependency managers, ivy and Gradle. The agreement between the two is made on the lines:
26, 30, 37, 38 et 39
Thus I can run my Groovy script manually,
and the Gradle compilation is not hindered by the @Grab.
The dependencies of the Groovy script are not to be provided in Gradle.
However, I cannot run the Groovy script from Gradle.

plugins {
    id "java"
    id "groovy"
}

repositories {
    mavenCentral()
    jcenter()
}

sourceSets {
    main {
        java { srcDirs = [] }
        groovy { srcDirs =["src/main/java", "src/main/groovy"] }
        resources {
            srcDirs =["src/main/resources"]
        }
    }
    test {
        java { srcDirs = [] }
        groovy { srcDirs =["src/test/java", "src/test/groovy"] }
    }
}

configurations {
    ivy
}

dependencies {
    ivy "org.apache.ivy:ivy:$ivy_version"
    implementation "org.codehaus.groovy:groovy-all:$groovy_version"
}



tasks.withType(GroovyCompile) {
    groovyClasspath += configurations.ivy
}

reference of the ticket onhttps://stackoverflow.com/a/18174033/837404[stackoverflow]

Related articles