3.7.1 Configurations and Dependencies - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: 2.0.4
3.7.1 Configurations and Dependencies
Grails features five dependency resolution configurations (or 'scopes'):-
build: Dependencies for the build system only -
compile: Dependencies for the compile step -
runtime: Dependencies needed at runtime but not for compilation (see above) -
test: Dependencies needed for testing but not at runtime (see above) -
provided: Dependencies needed at development time, but not during WAR deployment
dependencies block you can specify a dependency that falls into one of these configurations by calling the equivalent method. For example if your application requires the MySQL driver to function at runtime you can specify that like this:runtime 'com.mysql:mysql-connector-java:5.1.16'
group:name:version. You can also use a Map-based syntax:runtime group: 'com.mysql',
name: 'mysql-connector-java',
version: '5.1.16'group corresponds to an artifact's groupId and name corresponds to its artifactId.Multiple dependencies can be specified by passing multiple arguments:runtime 'com.mysql:mysql-connector-java:5.1.16',
'net.sf.ehcache:ehcache:1.6.1'// Orruntime(
[group:'com.mysql', name:'mysql-connector-java', version:'5.1.16'],
[group:'net.sf.ehcache', name:'ehcache', version:'1.6.1']
)Disabling transitive dependency resolution
By default, Grails will not only get the JARs and plugins that you declare, but it will also get their transitive dependencies. This is usually what you want, but there are occasions where you want a dependency without all its baggage. In such cases, you can disable transitive dependency resolution on a case-by-case basis:runtime('com.mysql:mysql-connector-java:5.1.16',
'net.sf.ehcache:ehcache:1.6.1') {
transitive = false
}// Or
runtime group:'com.mysql',
name:'mysql-connector-java',
version:'5.1.16',
transitive:falseExcluding specific transitive dependencies
A far more common scenario is where you want the transitive dependencies, but some of them cause issues with your own dependencies or are unnecessary. For example, many Apache projects have 'commons-logging' as a transitive dependency, but it shouldn't be included in a Grails project (we use SLF4J). That's where theexcludes option comes in:runtime('com.mysql:mysql-connector-java:5.1.16',
'net.sf.ehcache:ehcache:1.6.1') {
excludes "xml-apis", "commons-logging"
}// Or
runtime(group:'com.mysql', name:'mysql-connector-java', version:'5.1.16') {
excludes([ group: 'xml-apis', name: 'xml-apis'],
[ group: 'org.apache.httpcomponents' ],
[ name: 'commons-logging' ])exclude as well, but that can only accept a single string or Map:runtime('com.mysql:mysql-connector-java:5.1.16',
'net.sf.ehcache:ehcache:1.6.1') {
exclude "xml-apis"
}Using Ivy module configurations
If you use Ivy module configurations and wish to depend on a specific configuration of a module, you can use thedependencyConfiguration method to specify the configuration to use.provided("my.org:web-service:1.0") { dependencyConfiguration "api" }
"default" will be used (which is also the correct value for dependencies coming from Maven style repositories).Where are the JARs?
With all these declarative dependencies, you may wonder where all the JARs end up. They have to go somewhere after all. By default Grails puts them into a directory, called the dependency cache, that resides on your local file system atuser.home/.grails/ivy-cache. You can change this either via the settings.groovy file:grails.dependency.cache.dir = "${userHome}/.my-dependency-cache"grails.project.dependency.resolution = {
…
cacheDir "target/ivy-cache"
…
}settings.groovy option applies to all projects, so it's the preferred approach.

