Question: Suppose I have this: ```val container: N...
# plugin-development
k
Question: Suppose I have this:
Copy code
val container: NamedDomainObjectContainer<MyType>

container.configureEach { ... } // 1
container.all { ... } // 2

container.register("myItem") { ... } // 3
Is there an guaranteed order in which a new
MyType
is run? I'm experiencing a bug where people are expecting the order to be 1,3,2, but it isn't acting that way. (The reason for the order is because block 2 contains a task registration, which requires something set in block 3)
v
1 and 2 are definitely run before 3.
Among 1 and 2 I don't remember
register
creates the new instance, then runs all already registered configuration actions against it and only after that the configuration action given to the
register
call
Or rather,
register
does not actually create the instance, it just registers its creation with a configuration action
But the previously registered configuration actions will still run before when the item is created iirc
k
Oof. I think the problem I have is that I have a block 2 that has a task registration that involves a
get()
on the value set in block 3. eg.
Copy code
interface MyType {
  val myProperty: Property<String>
}

container.configureEach { // 1
  myProperty.convention(...)
}

container.all { // 2
  tasks.register("$name${myProperty.get()}")
}

container.register("myItem") { // 3
  myProperty.set(...)
}
Because 2 runs before 3, the tasks have names that are not what is expected; people might be under the impression that 3 runs before 2.
v
Ok, quickly tested, even across
all
and
confiugreEach
it works like expected.
All actions are executed in the order they are registered
So yeah, you cannot do that if 3 is registered after 2, you cannot use in 2 what is confiugred in 3
Even if you do nothing eager but fully lazy, with a
confiugreEach
before, then a
register
and then a
configureEach
after, as soon as the task is realized, for example by an
all
or by executing the task in question when talking about a task container, the actions are executed in exactly that registered order
k
The question is, how would I get something like a "3 before 2" effect, if 3 is in a build script and 2 is in a plugin?
v
The bad way,
afterEvaluate
The question probably more is, why do you need to resolve some property in 2?
k
The domain object
MyType
has both information and metadata about the tasks being registered. 2 would be "resolving the metadata". Of course, 2 would, in theory, be available eagerly too.
v
Which metadata that you need it at configuration time?
k
It's user supplied metadata, meant to be a part of a task name. Then some other code would parse the name to recover the value or somesuch.
It's conceivable that such metadata is unnecessary given that names are unique in a container, but "age of code" and all that.
v
The best would probably be to not depend on this metadata for the task naming 😄
If the task name really has to contain user-supplied information, maybe you could instead let the user add a full configured
MyType
to the container instead of registering. Or he could instead put the information into the name of the registered item.