Search code examples
variablesgradlefiletree

gradle fileTree with variable dir


I am not sure if this is supported by fileTree. I searched the document and all examples used concrete (absolute or relative) paths. This is what I want to do.

ext {
    externalProjectRoot "c:/AnotherProject"
}

implementation fileTree(dir: ${externalProjectRoot}/lib', include: '*.jar')

If I do the following, it works.

implementation fileTree(dir: 'c:/AnotherProject/lib', include: '*.jar')

So I suspect that the fileTree function doesn't support variable for the dir attribute. If not, what other options do I have?

Thanks!


Solution

  • You need to use " instead of ' :

    replace

    implementation fileTree(dir: '${externalProjectRoot}/lib', include: '*.jar')
    

    with

    implementation fileTree(dir: "${externalProjectRoot}/lib", include: '*.jar')
    

    See more explanation about difference between use of ' and " in this answer: Gradle Single vs Double Quotes :

    Gradle build scripts are written in Groovy. Groovy has both double-quoted and single-quoted String literals. The main difference is that double-quoted String literals support String interpolation