Loading...

September 14, 2015

Groovy Goodness: See More Info About Downloading With Grape

Groovy has a advanced feature to define and download dependencies automatically for our code: grape. To get more information about the progress of the dependency resolution and downloading of the dependencies we must use the Java system property groovy.grape.report.downloads and set it to true. Groovy uses Ivy under the hood to handle the dependency management. We can get Ivy logging messages by setting the system property ivy.message.logger.level to a numeric value. The value 4 gives the most logging and value 0 only shows error messages.

In the following example code we use -Dgroovy.grape.report.downloads=true when we invoke a simple Groovy script with a dependency on Apache Commons library:

import org.apache.commons.lang3.SystemUtils
import static org.apache.commons.lang3.JavaVersion.JAVA_1_8 as Java8

@Grab(group='org.apache.commons', module='commons-lang3', version='3.4')
def printInfo() {
    if (SystemUtils.isJavaVersionAtLeast(Java8)) {
        println 'We are ready to use the Stream API in our code.'
    } else {
        println 'We cannot use the Stram API in our code.'
    }
}

printInfo()

Now we run the script with extra logging information:

$ groovy -Dgroovy.grape.report.downloads=true sample.groovy
Resolving dependency: org.apache.commons#commons-lang3;3.4 {default=[default]}
Preparing to download artifact org.apache.commons#commons-lang3;3.4!commons-lang3.jar
Downloaded 424 Kbytes in 414ms:
  [SUCCESSFUL ] org.apache.commons#commons-lang3;3.4!commons-lang3.jar (408ms)
We are ready to use the Stream API in our code.
$ groovy -Dgroovy.grape.report.downloads=true sample.groovy
Resolving dependency: org.apache.commons#commons-lang3;3.4 {default=[default]}
Preparing to download artifact org.apache.commons#commons-lang3;3.4!commons-lang3.jar
We are ready to use the Stream API in our code.

Notice that the second time we run the script the logging shows the JAR file is not downloaded. That is because grape use the downloaded file from the USER_HOME/.groovy/grapes directory.

Written with Groovy 2.4.4.