Chủ đề của ticket là cấu hình một script build Gradle.
Tôi muốn có thể sử dụng một script Groovy mà không cần tham chiếu
vào dự án Gradle mà tôi đã gắn nó vào.
Tôi muốn có khả năng chạy script Groovy này bên ngoài Gradle
mà không có các annotation ivy về khôi phục phụ thuộc bên ngoài
không gây ra vấn đề về hiệu năng hay biên dịch
của tập lệnh build Gradle.
Để minh họa cho cấu hình này, tôi dựa trên hai tập lệnh:
một script build Gradle và một script Groovy.
Script Groovy chứa ở đầu tại dòng 2,
một chỉ dẫn để kéo một dependency bên ngoài
bằng trình quản lý phụ thuộc 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()

Trong tệp build.gradle, bây giờ hãy cùng xem cách để điều chỉnh cho hài hòa
hai trình quản lý phụ thuộc, ivy và Gradle. Sự thỏa thuận giữa hai bên được thực hiện tại các dòng:
26, 30, 37, 38 et 39
Vì vậy, tôi có thể chạy script Groovy của mình một cách thủ công,
và việc biên dịch Gradle không bị ảnh hưởng bởi @Grab.
Các phụ thuộc của script Groovy không cần phải cung cấp trong Gradle.
Ngược lại, tôi không thể chạy script Groovy từ 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
}

mã tham chiếu phiếu trênhttps://stackoverflow.com/a/18174033/837404[stackoverflow]

Bài viết liên quan