September 14, 2015

Groovy Goodness: Change Directory For Saving Dependencies Grape

With Grape in Groovy we can add dependency management for our code. Especially the @Grab annotation is very useful to specify dependencies directly in our code. Groovy will download the dependencies if needed and store them in the USER_HOME/.groovy/grapes directory. If we want to change this directory we must set the Java system property grape.root. We specify the new directory to store the downloaded dependencies as a value.

In the following example we have a simple script with a dependency on the Apache Commons library. We use the -Dgrape.root command line option when we run the script and specify the directory deps. After we have run the script we can see the contents of the deps directory to see the downloaded files.

$ groovy -Dgrape.root=deps/ sample.groovy
$ tree deps
└── grapes
    └── org.apache.commons
        └── commons-lang3
            ├── ivy-3.4.xml
            ├── ivy-3.4.xml.original
            ├── ivydata-3.4.properties
            └── jars
                └── commons-lang3-3.4.jar

4 directories, 4 files

Written with Groovy 2.4.4.