This message was deleted.
# docs
s
This message was deleted.
t
Let say I have task that produces
@get:OutputFile abstract val outArtifact: RegularFileProperty
then I want to consume it as input in other place. I do set the
outArtifact
following way
Copy code
outArtifact.set(layout.buildDirectory.file("native-config-plugin/configuration.json"))
What should I use for the consumer task? Judging by docs it should be
RELATIVE
, though, I am not sure.
Copy code
@get:InputFile
    @get:PathSensitive(PathSensitivity.RELATIVE)
    abstract val input: RegularFileProperty
Yes, you are correct. The
PathSensitivity
strategy for the consumer task should be
RELATIVE
.
The reason for this is that the
outArtifact
property is set to a file that is relative to the build directory. When the consumer task consumes the
input
property, it needs to know the relative path of the file, not the absolute path.
If the
PathSensitivity
strategy for the consumer task was
ABSOLUTE
, then the consumer task would always use the absolute path of the
outArtifact
file, even if the file was moved to a different location. This would make the consumer task brittle and less reusable.
By using
PathSensitivity.RELATIVE
, the consumer task will always use the relative path of the
outArtifact
file, which is more flexible and robust.
Google Bard points out that
RELATIVE
is a correct value in this case.
Also asked about the valid case of using
ABSOLUTE
strategy
Here are some examples when you might want to use
ABSOLUTE
for the
PathSensitivity
strategy:
• When the file or directory is not relative to the build directory. For example, if the file or directory is located in the user’s home directory, you would want to use
ABSOLUTE
to ensure that the consumer task can always find the file or directory, no matter where the user’s home directory is located.
• When the file or directory is a symbolic link. Symbolic links are files that point to other files or directories. If you use
RELATIVE
for the
PathSensitivity
strategy, Gradle will follow the symbolic link and use the path of the file or directory that the symbolic link points to. However, if you use
ABSOLUTE
for the
PathSensitivity
strategy, Gradle will use the absolute path of the symbolic link, even if the symbolic link points to a different file or directory.
• When you want to prevent the task’s outputs from being shared across different workspaces via the build cache. If you use
RELATIVE
for the
PathSensitivity
strategy, Gradle will share the task’s outputs across different workspaces, even if the files or directories are located in different places. However, if you use
ABSOLUTE
for the
PathSensitivity
strategy, Gradle will not share the task’s outputs, even if the files or directories are located in the same place.
For
NAME_ONLY
it shared the following examples:
• When you only care about the name of the file or directory, not the path. For example, if you are creating a task that copies a file, you might only care about the name of the file that you are copying, not the path to the file.
• When you are using a file or directory as a unique identifier. For example, if you are using a file or directory to store the configuration for a task, you might want to use
NAME_ONLY
to ensure that the task can always find the configuration file, no matter where it is located.
• When you want to prevent the task’s outputs from being shared across different workspaces via the build cache. If you use
RELATIVE
or
ABSOLUTE
for the
PathSensitivity
strategy, Gradle will share the task’s outputs across different workspaces, even if the files or directories are located in different places. However, if you use
NAME_ONLY
, Gradle will not share the task’s outputs, even if the files or directories are located in the same place.
I can see that the 3-d statement conflicts with the 3-d statement about
ABSOLUTE
path usage. First it says that
ABSOLUTE
makes the file not shared by cache then mentions here that
ABSOLUTE
will make task output shareable to cache 🤦 So confusing 😞
And the last one about
NONE
Here are some examples when you might want to use
NONE
for the
PathSensitivity
strategy:
• When you only care about the contents of the file or directory, not the path. For example, if you are creating a task that parses a configuration file, you might only care about the contents of the file, not the path to the file.
• When you are using a file or directory as a temporary file or directory. For example, if you are creating a task that generates a temporary file, you might want to use
NONE
to ensure that the task always deletes the temporary file, even if the temporary file is moved to a different location.
• When you want to prevent the task’s outputs from being shared across different workspaces via the build cache. If you use
RELATIVE
,
ABSOLUTE
, or
NAME_ONLY
for the
PathSensitivity
strategy, Gradle will share the task’s outputs across different workspaces, even if the files or directories are located in different places. However, if you use
NONE
, Gradle will not share the task’s outputs, even if the files or directories are located in the same place.
It is important to note that using
NONE
for the
PathSensitivity
strategy can make your build less efficient, as Gradle will have to re-evaluate the inputs of the task every time it is executed. Therefore, you should only use
NONE
when it is absolutely necessary.