-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expressing optional @InputFile
and @InputDirectory
properties
#2016
Comments
Just ran into this today and I think this would be a nice improvement |
Yeah... I have to write a lot of code to get around this. I write getters that go through the logic of which parameters to use. It's really nasty. |
…t exist It's perfectly valid for one of them to not exist; we'll just generate it. It isn't valid for the apply- or generate- tasks, though, so I've left them unchanged. The optional annotation doesn't work for this, see gradle/gradle#2016. This issue was noted in Pokechu22/WorldDownloader#75.
This is required due to missing @optional support for directories see gradle/gradle#2016
See gradle/gradle#2016 Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
* Release notes fixes and other annotation improvements - Make release notes dir optional (aka don't crash if it isn't there) - Remove pointless network call updating tracks - Update out-of-date credentials annotations Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix much deeper problem due to poor Gradle APIs See gradle/gradle#2016 Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add comment Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Make root creds file optional Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
* Add support for Gradle cache Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix crash if not all listing details are provided Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add incremental compilation support Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Finalize multidimensional resource merge support Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Tidy Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * More tests and cleanup Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add support for Android App Bundles Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Docs Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Minor tidy Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Default to APKs since bundles require app signing Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Make validation incremental Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Small tidy Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Refactor release notes out of listings folder Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Slight perf optimization Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Narrow cache invalidation Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix broken release notes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Remove unnecessary comment Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix test naming Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Support applying plugin to dynamic-feature modules Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Update Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix more merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add progress logger Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * More merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Final merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Downgrade AGP Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Remove `All` suffix Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * publishBundleRelease -> publishReleaseBundle Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add CLI option Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add changelog Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix broken warning logs Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Release notes fixes and other annotation improvements - Make release notes dir optional (aka don't crash if it isn't there) - Remove pointless network call updating tracks - Update out-of-date credentials annotations Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix much deeper problem due to poor Gradle APIs See gradle/gradle#2016 Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add comment Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Upgrade to AGP 3.2 beta+ bundle naming conventions Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Prep for review Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Review feedback Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
* Add support for Gradle cache Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix crash if not all listing details are provided Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add incremental compilation support Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Finalize multidimensional resource merge support Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Tidy Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * More tests and cleanup Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add support for Android App Bundles Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Docs Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Minor tidy Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Default to APKs since bundles require app signing Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Make validation incremental Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Small tidy Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Refactor release notes out of listings folder Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Slight perf optimization Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Narrow cache invalidation Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix broken release notes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Remove unnecessary comment Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix test naming Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Support applying plugin to dynamic-feature modules Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Update Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix more merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add progress logger Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * More merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Final merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Downgrade AGP Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Remove `All` suffix Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * publishBundleRelease -> publishReleaseBundle Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add CLI option Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add changelog Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix broken warning logs Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Release notes fixes and other annotation improvements - Make release notes dir optional (aka don't crash if it isn't there) - Remove pointless network call updating tracks - Update out-of-date credentials annotations Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix much deeper problem due to poor Gradle APIs See gradle/gradle#2016 Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add comment Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Upgrade to AGP 3.2 beta+ bundle naming conventions Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Prep for review Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Review feedback Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Don't upload 0 byte mapping files Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
Until we get this cool feature, I've been working around it like so:
Not pretty, but is a one-liner and does the job as far as I can tell. |
I think that the current Gradle behaviour is consistent also with respect to properties - @optional means null for them. There is an easy workaround for returning null for a non-existant input file. It seems to be different to not specify an input file and specifying an input file which is not there. I'll close this issue now since we don't want to change the meaning of @optional for file inputs. If you want an easier way to specify that missing input files are OK, a simple method on RegularFileProperty should be enough, right? That would instruct the RegularFileProperty to return null if the file does not exist. WDYT? |
@wolfs That sounds reasonable. And actually, the bug I was trying to solve was related to my property being a singleton: val releaseNotesDir by lazy {
File(resDir, RELEASE_NOTES_PATH).orNull()
} That doesn't work because Gradle resolves properties multiple times. BTW, is that documented somewhere? Instead, this is correct: val releaseNotesDir
get() = File(resDir, RELEASE_NOTES_PATH).orNull() |
* Release notes fixes and other annotation improvements - Make release notes dir optional (aka don't crash if it isn't there) - Remove pointless network call updating tracks - Update out-of-date credentials annotations Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix much deeper problem due to poor Gradle APIs See gradle/gradle#2016 Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add comment Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Minimize mutability Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Small tidy Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Small fix Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
* Add support for Gradle cache Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix crash if not all listing details are provided Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add incremental compilation support Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Finalize multidimensional resource merge support Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Tidy Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * More tests and cleanup Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Minor tidy Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Support in-app products Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Make validation incremental Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Small tidy Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Refactor release notes out of listings folder Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Slight perf optimization Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Narrow cache invalidation Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix broken release notes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix parsing Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Rename for consistency Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix test naming Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Address review feedback Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Rename files Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Determine winner of conflicting flavor by order of dimension Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add tests Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Tidy Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Massively simplify IAP parsing Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Tidy Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add changelog Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Release notes fixes and other annotation improvements - Make release notes dir optional (aka don't crash if it isn't there) - Remove pointless network call updating tracks - Update out-of-date credentials annotations Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix much deeper problem due to poor Gradle APIs See gradle/gradle#2016 Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Add comment Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Small fixes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Support global CLI options Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Remove release notes, will update later to not conflict Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Fix merge mistakes Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> * Move product dir input initialization to task instead of plugin Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
How do you express this for My use case is to provide a default directory for the plugin users to write to, but if they don't need it, it shouldn't even be created, and the task can handle its absence just fine. If I just use I guess it might be possible to use a special provider which would return a null value if the directory does not exist, but it is really hacky if you want to rely on another property to compute the default value: someDirectory.set(project.layout.buildDirectory("some-directory").flatMap {
project.provider { if (it.asFile.exists()) it else null }
}) |
We should reopen this issue, as the original use case hasn't been addressed. "optional" in the title doesn't mean I should be able to do the same for the other kinds of inputs, such directories, collections of regular files, etc. This way, I can apply a convention to each task for things where it's ok for the thing to not exist, or I can wire in an output from some other task where it's ok for the task to not produce anything. I'd also like a way to say "the file that this property refers to represents the 'source' or primary input of this task, so skip the task and remove old outputs if the file does not exist". This isn't quite the same as the behaviour of |
Yes, please! |
I reopen the issue to avoid being ignored. We can summarize the work left to do in another issue and close this one later. |
Just for the people who stumble upon this and need to know how the "current" way of doing things there looks like, it's this: open class SomeTask @Inject constructor(objects: ObjectFactory) : DefaultTask() {
@InputFile
@Optional
val someOptionalFile: RegularFileProperty = objects.fileProperty()
@TaskAction
fun run() {
val optionalFile: File? = someOptionalFile.orNull?.asFile
// do your stuff here
}
} So, this means that if |
@bamboo this solution does not work for me because if |
@pun-ky 's solution was the best workaround (as it allows you to continue to use providers on your APIs between tasks. But it shouldn't be needed! I also used a noOp dir @Internal
val compiledKotlinSource = objects.property(FileSystemLocation::class.java)
.convention(layout.emptyDir)
@InputFiles
val _compiledKotlinSource = objects
.fileCollection()
.from(compiledKotlinSource)
private val ProjectLayout.emptyDir: Provider<Directory>
get() = buildDir
.map { it.dir("noOp") }
.map {
it.asFile.mkdirs()
it
} |
@ZakTaccardi thanks for the confirmation |
All the workarounds presented here are either Groovy or Kotlin. Is there an idiomatic alternative in plain Java? |
Regarding all the workarounds above, I realized that you can add the
That means we have a way to express an input file which doesn't need to exist: Use |
I worked around this using onlyIf: onlyIf { inputFile.orNull?.asFile?.exists() ?: false } Because the onlyIf block runs at execution time it is safe to get() or getOrNull() on the file property. |
For Kotlin: you can also pass a sealed class without the nullable/optional types with more specific details on everything that is needed. The classes need to implement |
I'd like to express "this is a directory/file that is an input to my task, if it exists" with default values. This means that if the input exists, my task will use it. If it doesn't exist, that's OK. If the input is the wrong type (directory instead of a file or vice versa), I'd like to get an error.
Due to the way we validate
@InputFile
and@InputDirectory
, it's not simple to define a property this way because we require that the file or directory exists. If a plugin doesn't define a default value, you can use@Optional
and a null value for the property.Expected Behavior
I should be able to have a property with a default value that is only validated if it exists.
Current Behavior
Property validation fails before the task executes when the directory/file does not exist.
Context
I can work around this by using
@InputFiles
or conditionally returning null if the path does not exist. If I use@InputFiles
, it's inconvenient to useFile
as my property type because it makes it easy for someone to pass the wrong thing (a file vs a directory) and it looks a little odd to have@InputFiles File someProp
. I could also use two properties. One that's just theFile
that's ignored by up-to-date checks and another that's a@InputFiles FileCollection
. This still has the problem that the wrong thing could be used (file vs directory) and it's strange that I would need to use two properties.Steps to Reproduce (for bugs)
Here's an example task that fails:
The same would apply for
@InputFile
.Your Environment
This always happens.
The text was updated successfully, but these errors were encountered: