Slackbot
03/22/2023, 8:21 PMAdam
03/22/2023, 9:28 PMdoFirst {}
block. And to be compatible with Config Cache, create a new val
in the configure {}
block.Sergei Ponomarev
03/22/2023, 10:40 PMdoFirst
worked. I’m sorry, what does it mean create a new val in the configure block
? I don’t understand what to do 😞 Could you clarify?Adam
03/22/2023, 10:55 PMjvmArgs
using a dynamic variable? Because of weird config cache reasons it’s a good idea to have that variable defined within the configure {}
block (but it’s not strictly necessary, I’m just sharing this in case it’s relevant in your case because you’re using config cache)
Here’s a simpler example to demonstrate. Here’s myCustomTask
that, like your task, does something in a doFirst {}
block.
val foo = "123"
tasks.register("myCustomTask") {
doFirst {
println("foo is $foo")
}
}
If I run this using ./gradlew myCustomTask --configuration-cache
, I get an error - task actions (that’s the stuff inside the doFirst {}
block) aren’t allowed to reference values that are defined outside of the task. foo
is defined in the script, not in the task.
The simple workaround is to just-redefine foo
inside the task configuration.
val foo = "123"
tasks.register("myCustomTask") {
val foo = foo // redefine the val, so it's local to the task
doFirst {
println("foo is $foo")
}
}
Now ./gradlew myCustomTask --configuration-cache
will work!
Of course it’s possible this isn’t applicable to your situation, but it’s a weird problem that can be tough to understand the first time around, so I wanted to make sure I explained in case you bumped into it.Sergei Ponomarev
03/22/2023, 10:58 PMjvmArgs += createTestJvmArg(paramName, defaultValue)
I guess I better do it this way:
val a = createTestJvmArg(paramName, defaultValue)
jvmArgs += a
The createTestJvmArg()
method is declared in the same build.gradle
do I get it right?Adam
03/22/2023, 11:02 PMAdam
03/22/2023, 11:02 PMtargets {
configureEach {
testTask.configure {
val testJvmArg = createTestJvmArg(paramName, defaultValue)
doFirst {
jvmArgs += testJvmArg
}
}
}
}
Adam
03/22/2023, 11:03 PMtargets {
configureEach {
testTask.configure {
val testJvmArg = createTestJvmArg(paramName, defaultValue)
inputs.property("testJvmArg", testJvmArg)
doFirst {
jvmArgs += testJvmArg
}
}
}
}
that would be important because if Gradle knows about all the inputs and outputs, then it can skip tasks if nothing has changedSergei Ponomarev
03/22/2023, 11:06 PMSergei Ponomarev
03/22/2023, 11:10 PMdoFirst
it is again executed in the configuration phase no matter what task is executed. Nevertheless, I think I understand what it is doing now. I will think more about the behavior I really want to get in the end. Thank you for your help!Adam
03/22/2023, 11:10 PMAdam
03/22/2023, 11:12 PMfun createTestJvmArg(paramName: String, defaultValue: String): Provider<String> {
return providers.provider {
"some value for $paramName, $defaultValue..."
}
}
Adam
03/22/2023, 11:13 PMdoFirst {}
Adam
03/22/2023, 11:13 PMtargets {
configureEach {
testTask.configure {
val testJvmArg: Provider<String> = createTestJvmArg(paramName, defaultValue)
inputs.property("testJvmArg", testJvmArg)
doFirst {
jvmArgs += testJvmArg.get()
}
}
}
}
Sergei Ponomarev
03/22/2023, 11:14 PMAdam
03/22/2023, 11:15 PMVampire
03/22/2023, 11:56 PMSergei Ponomarev
03/23/2023, 12:00 AMVampire
03/23/2023, 12:02 AMVampire
03/23/2023, 12:03 AM.all
on a task collection, or a .withType
with second argument, ...Vampire
03/23/2023, 12:03 AMVampire
03/23/2023, 12:04 AMjvmArgumentProvider
Sergei Ponomarev
03/23/2023, 12:04 AMtesting {
suites {
configureEach {
...
to configure common properties for all test tasksVampire
03/23/2023, 12:04 AMVampire
03/23/2023, 12:05 AMVampire
03/23/2023, 12:05 AMconfigureEach
is a lazy operationVampire
03/23/2023, 12:05 AMVampire
03/23/2023, 12:06 AMVampire
03/23/2023, 12:06 AMSergei Ponomarev
03/23/2023, 12:06 AMall
or withType
in the script. Let me try creating a smaller script to see if it would have the same problem. if not, then I will need to find what’s wrongVampire
03/23/2023, 12:07 AMVampire
03/23/2023, 12:07 AMok, I don’t see anyIt does not, I copied your code 1:1 and it worked perfectly fine lazily as it should beorall
in the script. Let me try creating a smaller script to see if it would have the same problem. if not, then I will need to find what’s wrongwithType
Sergei Ponomarev
03/23/2023, 12:07 AMSergei Ponomarev
03/23/2023, 12:08 AMVampire
03/23/2023, 12:08 AM--scan
to see how many tasks are configured that shouldn'tMarkus Maier
03/23/2023, 7:13 AMgradlew :help -Dorg.gradle.internal.tasks.stats
Vampire
03/23/2023, 6:13 PMgradle.properties
. 🙂