0

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?

esalomon
  • 85
  • 1
  • 12

1 Answers1

0

You can execute "gradle dependencies" in custom script:

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

and parse output

Evgeny Smirnov
  • 2,886
  • 1
  • 12
  • 22
  • First about all, thanks for your answer. About it, that is the way in which you can call any Gradle task inside a Grails script file, but, it only gives you the dependencies names with out the files names. Maybe my question needs some clarification, because I need the jars file name and its paths. Because that information will be used by another script which uses Proguard for obfuscate the jar file. I updated the question in order to show what is the information which I need. – esalomon Oct 12 '17 at 23:15
  • In this case you may scan working directory grails.util.BuildSettings.TARGET_DIR for .war file and scan it for all jars – Evgeny Smirnov Oct 13 '17 at 08:55