Question: Suppose I have something like this: ```v...
# plugin-development
k
Question: Suppose I have something like this:
Copy code
val task1 by tasks.registering(Zip::class) {
  from(project.findProperty("sourcePath") as String?)
}

val task2 by tasks.registering(MyOtherTask::class) {
  inputProperty.set(task1.flatMap { it.archiveFile }) // @get:InputFile
}
I'm seeing an error saying that
inputProperty
isn't properly set because the file doesn't exist; is it because of something relating to the configuration of
task1
? Should I work around this issue by subclassing
Zip
for
task1
?
v
I don't think subclassing
Zip
would make any sense or change anything. What is "the error saying" exactly? Lightly quoted error messages are usually just increasing confusion, always provide a verbatim copy. Best would be a build
--scan
URL, even better combined with an MCVE.
k
Copy code
A problem was found with the cnfiguration of task 'task2' (type 'MyOtherTask').
  - In plugin 'myPlugin' type 'MyOtherTask' property 'inputProperty' specifies file '<some file path>' which doesn't exist.
     Reason: An input file was expected to be present but it doesn't exist
v
And was
task1
executing?
k
NO-SOURCE
was reported, which is part of the problem, I suppose.
v
Oh, ok, yes then. Seems the
Zip
task does not want to create an empty
Zip
but skips its work if nothing is to be put inside the
Zip
, which then make
task2
unhappy.
What would you expect / want to happen in that case?
k
Not really sure at this point. I could add an
onlyIf
to try and skip
task2
, that seems sane.
👌 1
v
Tell how it worked. I'm unsure whether the
onlyIf
is checked before that error is reported or after. (If the latter, there are further mitigation strategies)
k
Copy code
onlyIf { task1.flatMap { it.archiveFile }.isPresent }
didn't work. I could try
Copy code
onlyIf { task1.flatMap { it.archiveFile }.get().asFile.exists() }
but that seems wrong somehow.
v
isPresent
is pointless. That checks whether a value is configured and you configured a value. The point is, that the file is not there that is configured.
You don't need to do
flatMap
though, you can directly use
get()
.
onlyIf
is executed at execution time.
Which on the other hand of course means, this will either way not be CC-compatible I think
k
I'm not sure how to make it CC-compatible without possibly rejigging
task1
or somesuch.
v
What is "rejigging"? I don't know that word. Maybe you should not check the
task1
output property, but the own
inputProperty
, that should then probably work.
For CC safety that is. Still curious whether the
onlyIf
is checked first
k
My reference builds suggest that if I add
Copy code
onlyIf { task1.get().didWork }
it looks OK
v
Hm, interesting, maybe because it is a task provider it works
But still checking the own input property makes more sense, because that is what you want to check
If you then ever change where the input is coming from, you cannot forget to update the
onlyIf
check
k
Yeah, I don't know why I thought that the task's own properties were off-limits in an
onlyIf
block.
p
You could also make your input als inputfiles and add skipifempty too
v
I'm not sure that is a too good idea, making the input multiple files just to use skipifempty. Besides that, it will probably not skip as it is not empty, a value is configured, it is just not physically existing.