File Utilities
The FileSystemOperations object has many methods to work with files and directories
An instance of it can be obtained via ConfigCacheSafeOptions.fsOperations()
or ProjectOperations.getFsOperations()
.
This instance is configuration cache compatible and can be called from within a task action.
Some conversion methods take an Object
instance of a collection of Object
as inputs.
They can recursively unpack Callable
, Closure
, Optional
, Provider
and Supplier
until it gets to something it can evaluate.
Conversions
You are probably familiar with project.file
to convert a single object to a file.
Grolifant offers similar, but more powerful methods.
There are also methods that take a collection of objects and convert them to a list of files.
-
Any
CharSequence
-
java.io.File
andjava.nio.file.Path
-
URIs of scheme
file:
. -
org.gradle.api.file.Directory
-
org.gradle.api.file.RegularFile
-
org.gradle.api.resources.TextResource
-
org.gradle.api.file.FileCollection
-
Any wrapper described aboce such as
Provider
-
file(Object)
- Converts a single object to a file. -
fileOrNull(Object)
- Similar tofile
, but allows fornull
to be passed. -
files(Collection<Object>)>
- Take a collection of objects and convert them to files. Any item in the collection that is anull
will result in an exception being emitted. -
filesDropNull(Collection<Object>)
- Take a collection of objects and convert them to files. Anynull
entries will be removed from the final result. -
provideFile(Object)
- Create aFile
provider to a lazy-evaluated object.
File collections
-
emptyFileCollection
- Return a configurable empty file collection. -
fileTree
- ACondifugrableFileTree
instance staring at the provided starting point. Anything convertible withfile(Object)
will suffice as input.
CopySpec(s)
CopySpec
Use copySpec
to create an empty CopySpec
.
CopySpec
to filesSometimes one wants to use a org.gradle.api.file.CopySpec
as part of a DSL, but resolve it to a file collection without performing a copy or synchronisation.
There is one method available to do so - resolveFilesFromCopySpec
.
It returns a FileTree
.
Readable compression resources
If you have a file that is BZIP2 or GZ comrpessed, you can easily create a org.gradle.api.resources.ReadableResource
from it.
-
bzip2Resource(Object) - BZIP2 resources
-
gzipResource(Object) - GZIP resources
Anything convertible to a file using the earlier mentioned conversion methods, can be passed as parameter.
Creating safe filenames
Sometimes you might want to use entities such as task names in file names. Use the toSafeFilename method to obtain a string that can be used as part of a filename.
In similar fashion you can concatenate a number of subpaths and ensure that the resulting path only consists of safe file names. There are two methods for this.
-
toSafePath(String…) - Creates a
Path
instance. -
toSafeFile(String…) - Creates a
File
instance.
Listing child directories of a directory
listDirs provides a list of child directories of a directory.
Resolving the location of a class
For some cases it is handy to resolve the location of a class, so that it can be added to a classpath.
One use case is the for javaexec
processes and Gradle workers.
Use
resolveClassLocation to obtain a File
object to the class.
If the class is located in a JAR it path to the JAR will be returned.
If the class is directly on the filesystem, the toplevel directory for the package hierarchy that the class belongs to, will be returned.
Copy, delete and sync operations
These ensure the old projext.cop
and project.sync
works across Gradle API releases.
They can be called from within task actions.
Working with resources
There are a number of methods to simplify loading content from resource files.
-
copyResourceToFile
- Copy a file at a resource path to a destination file. -
loadPropertiesFromResource
- Load the properties file at the given resource path. -
providePropertiesFromResource
- Load the properties file at the given resource path only on-demand.
There a versions of the above which also allow for specifying the class loader. |
Temporary directories
createTempDirectory
will create a temporary directory within the build directory.
Updating a File property
You can also update any Property<File>
using the updateFileProperty
utility method.
File locations
Obtaining a subfolder of the build directory
Use the buildDirDescendant
method, which returns a provider to a File
.
Anything that can be evaluated to a string using StringTools
can be passed as a parameter.
fsOperations().buildDirDescendant('docs/asciidoc')
Project cache directory
If a plugin might need to cache information in the local cache directory it is important that it determines this folder correctly.
You can call getProjectCacheDir
to achieve this.
You can also use it in provider form - provideProjectCacheDir
to achieve this.
If you want to get a directory below the cache directory then use provideProjectCacheDirDescendant
.
Gradle distribution directory
getGradleHomeDir
will provide the home of the currently active Gradle distribution.
Gradle user home directory
getGradleUserHomeDir
will provide the currently active home directory for the user.
Project directory
Use getProjectDir
to obtain the directory of the current (sub)project.
You can also get it in Provider
form - provideProjectDir
.
Project root directory
Use getProjectRootDir
to obtain the root directory of the current (sub)project.
You can also get it in Provider
form - provideRootDir
Relative paths
Relative paths can be very useful in many cases.
-
relativePath
- The relative path FROM the project directory to the given path -
relativePathNotEmpty
- Similar torelativePath
, but if the two entities are siblings, return.
rather than an empty string. -
relativeRootPath
- The relative path FROM the root project directory to the given path. -
relativePathToProjectDir
- The relative path from the given path to the project directory. -
relativePathToRootDir
- The relative path from the given path to the root project directory. -
relativize
- For two instances return the relative path of a target from a base i.e. the path to traverse starting at the base file. One version of this method takes twoFile
instances and another twoPath
instances.