0

语境

我使用下一个命令创建了一个 Grails 3.2.11 脚本:

grails create-script script-test

上面的命令生成了文件:script-test.groovy.

然后,我需要验证当前 grails 项目中是否存在 jar 文件依赖项。

在 Grails 2.4 中,您可以通过调用来做到这一点grailsSettings.runtimeDependencies

def verifyJarDependency(String dependencyName) {

    //Gets all Grails Application dependencies.
    def dependenciesInstance = grailsSettings.runtimeDependencies

    //Defines the result variable
    def result = false

    //Adds references to all classes used in Grails Application.
    dependenciesInstance?.each { dependencyFile ->

        //Gets the file name
        String fileName = dependencyFile.name

        //Verifies if the actual file contains the Dependency file string
        if (fileName.contains(dependencyName)) {

            //There is a Jar file with the Dependency string.
            println "The Dependency file found is: $dependencyFile"
            result = true
        }
    }

    //If there is no Dependency jar file it returns false.
    return result
}

例如,如果您在 grails 2.4 脚本文件中执行下一段代码:

target(buildPlugin: "Retrieves all the jar files which this instance is using") {

    //Gets all Grails Application dependencies.
    def dependenciesInstance = grailsSettings.runtimeDependencies

    dependenciesInstance?.each { dependencyFile ->

        //There is a Jar file with the Dependency string.
        println dependencyFile
    }
}

setDefaultTarget(buildPlugin)

您将获得如下输出:

/home/username/.sdkman/candidates/grails/2.4.3/lib/org.grails/grails-datastore-simple/jars/grails-datastore-simple-3.1.2.RELEASE.jar
/home/username/.sdkman/candidates/grails/2.4.3/lib/org.grails/grails-datastore-gorm/jars/grails-datastore-gorm-3.1.2.RELEASE.jar
/home/username/.sdkman/candidates/grails/2.4.3/dist/grails-plugin-converters-2.4.3.jar
/home/username/.sdkman/candidates/grails/2.4.3/dist/grails-plugin-mimetypes-2.4.3.jar
/home/username/.sdkman/candidates/grails/2.4.3/lib/com.h2database/h2/jars/h2-1.3.176.jar
/home/username/.sdkman/candidates/grails/2.4.3/lib/log4j/log4j/jars/log4j-1.2.17.jar
/home/username/.sdkman/candidates/grails/2.4.3/dist/grails-resources-2.4.3.jar

此信息稍后将用于 Proguard 以混淆 jar 文件。

问题

如何在自定义脚本中检索当前 Grails 3.2 项目依赖项?

4

1 回答 1

0

您可以在自定义脚本中执行“gradle dependencies”:

description "Project dependencies", "grails list-dependencies"
println gradle.dependencies()

并解析输出

于 2017-10-12T07:04:17.300 回答