Consider this simplified copy instruction, which will copy all files form say the compile
configuration into some directory.
project.copy {
from configurations.compile, {
eachFile { fcd ->
println fcd.sourcePath
}
}
}
Typically the source of such files will be ~/.gradle/caches//modules-2/files-2.1/organisation/artifact/version/HASH/artifact-version.jar
However in the above code it will simply print artifact-version.jar
, whereas I would have expected at least a part of the path. Is this specific behaviour to configurations?
P.S. the only way of obtaining the full path is to use fcd.file.path
instead.
Hi Schalk,
as the Javadoc of getSourcePath
states, the source path is relative to the root containing the file tree. In your case, the jar file is the root, and therefore only the name is returned. So this behaviour is not specific to configurations.
Cheers,
Stefan
Thanks Stefan,
It is as per the Jaavdoc that got me wondering what the root of the file tree would be. Whay the decision to make the Jar the root when the file would be in Gradle’s artifact cache? Is it because the artifact would not have been resolved earlier or another reason?
Hi Schalk,
A FileCollection
can contain root elements. For example, an element of a FileCollection
can be a FileTree
, a directory or a file. Those elements are the root elements of the FileCollection
. For a configuration, all the elements in the configuration are root elements (it is just a list of files with no hierarchy). That is the root which is referenced in the Javadoc.
Now, there is the special case that for a regular root file in a file collection, we report the file name as the relative path.
I hope that helps. The documentation for “relative path” is not very good and I am happy for help to improve it.
Cheers,
Stefan