如何在Grails 3.2脚本

时间:2017-09-29 22:21:22

标签: grails grails3 grails-3.2

上下文

我使用下一个命令创建了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项目依赖项?

1 个答案:

答案 0 :(得分:0)

你可以执行" gradle依赖"在自定义脚本中:

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

并解析输出