Does anyone have practical examples for using `add...
# community-support
y
Does anyone have practical examples for using
addLater
or
addAllLater
on a domain collection especially on a named collection?
p
What do you mean? I often use
NDOC.addAllLater(NDOC.elements.map { it.map { objectFactory.newInstance(Foo::class, it.name).apply {} }}
But it really depends on the use-case
I need this code for lazily adding elements from one NDOC to another
y
The generic example makes sense. Have you ever tried adding tasks in this way if you have an NDOC?
p
You cannot add tasks via addLater to the TaskContainer
And you must register tasks eager
y
Well
TaskContainer
does extend
DomainObjectCollection
which provides the
addAllLater
method. I've never tried it, but it is there on paper.
p
y
Thanks for that.
v
Well, depending on situation you might be able to abuse a task rule
Copy code
val foo = providers.gradleProperty("foo")
tasks.addRule("Task 'bar' if project property 'foo' is set") {
    if (foo.isPresent && (this == "bar")) {
        tasks.register(this) {
            doLast {
                println(this)
            }
        }
    }
}
y
Yip that is a goto for this specific use-case. I was interested in the usage in general, rather than a specific personal use case.
v
Copy code
$ gw -q bar -P foo && echo ============= && gw -q bar
task ':bar'
=============

FAILURE: Build failed with an exception.

* What went wrong:
No matches
  Task 'bar' not found in root project 'showcase'. Some candidates are: 'jar'.

* Try:
> Run gradlew tasks to get a list of available tasks.
> For more on name expansion, please refer to <https://docs.gradle.org/9.7.1/userguide/command_line_interface.html#sec:name_abbreviation> in the Gradle documentation.
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights from a Build Scan (powered by Develocity).
> Get more help at <https://help.gradle.org>.

BUILD FAILED in 1s
For the general use-case, yeah, not supported by
DefaultTaskContainer
There often another alternative is to always register the task and use
onlyIf
to skip it conditionally