How to retrieve the current dependencies in a Grails 3.2 script

337 Views Asked by At

Context

I created a Grails 3.2.11 script with the next command:

grails create-script script-test

The above command generated the file: script-test.groovy.

Then, I need to verify if a jar file dependency is present at the current grails project.

At Grails 2.4 you could do that with a grailsSettings.runtimeDependencies call:

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
}

For example if you execute the next piece of code inside a grails 2.4 script file:

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)

You will get an output like next:

/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

This information will be used later for Proguard in order to obfuscate the jar file.

Question

How I can retrieve the current Grails 3.2 project dependencies inside a custom script?

1

There are 1 best solutions below

2
On

You can execute "gradle dependencies" in custom script:

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

and parse output