How to copy files from a custom configuration into a particular war directory?


(Ryan Nelson) #1

I have a Gradle build file that looks something like this:

configurations {

otherLibs }

dependencies {

otherLibs(group: ‘mygroup’, name: ‘myname’, version: ‘1.0’) }

war {

from configurations.otherLibs {

into “/otherLibs”

} }

What I want is to have a directory named “otherLibs” in the war root containing the dependencies for “mygroup:myname:1.0”. However, what I end up with is the “otherLibs” directory with ALL of the rest of my war files contained inside of it. It’s as if Gradle is applying the “into” to the entire war, rather than just the dependencies of that specific configuration.

I’ve tried a few different permutations. What am I doing wrong?


(Peter Niederwieser) #2

I’d be surprised if this didn’t give a Groovy syntax error. Try:

war {
    from(configurations.otherLibs) { // notice the parens
        into "otherLibs" // no leading slash
    }
}

(Ryan Nelson) #3

Thanks for the reply, Peter. It was the parentheses that made the difference. After adding those, it worked with or without the leading slash. Do you mind helping me understand why?

That seems like an extremely difficult bug to catch, especially for someone new to Groovy like I am. Groovy does not give any errors or warnings. I adopted the syntax from the Gradle War plugin page, where you can see a custom configuration is included in the classpath without parentheses:

http://gradle.org/docs/current/userguide/war_plugin.html


(Peter Niederwieser) #4

To answer your question, ‘from’ is a method call taking two arguments. You could write it as ‘from(configurations.otherLibs, { into “otherLibs”})’ or ‘from configurations.otherLibs, { into “otherLibs” }’. If the closure is the last argument, Groovy allows you to move it outside the parens: ‘from(configurations.otherLibs) { into “otherLibs” }’. In that case you can’t leave off the parens though.

I think that the version without parens gets parsed as ‘from(configurations.otherLibs({ into “otherLibs” }))’. This will try to configure the configuration, but instead ends up setting the top-level destination directory inside the War (due to the use of the ‘into’ method).

It can be a bit tricky to understand when Groovy allows to leave off parens. If in doubt, leave them in.


(Ryan Nelson) #5

Makes a lot of sense, thank you for the explanation.