How can I throw a build time error given a specifi...
# community-support
z
How can I throw a build time error given a specific combination of two gradle properties only when a specific task is scheduled for execution? Scenario: I have a
Test
task that can run some live network tests, when
-PnetworkTest=true
. If
-PnetworkTest=false
- then regular local tests will be run, not live network tests. For network tasks to run successfully, I need to specify a
myServer.secretToken=XXX
gradle property. By default, to not cause configuration issues for the
Test
task when
-PnetworkTest=false
,
gradle.properties
contains a blank value for `myServer.secretToken`:
Copy code
myServer.secretToken=
This way, the
Provider<String>
that represents it contains a blank value and does not cause issues when devs aren’t running integration tests. However, I would like to throw a build time error BEFORE test execution, ONLY when: • that specific
Test
task is scheduled for execution •
-PnetworkTest=true
myServer.secretToken
is blank What would be the best way to achieve this?
p
IMHO you should not use 1 test task but two. You can still use the same test code. With the jvm test suites plugin, you will create a new target, eg the default named „test“ for local tests and a new target named „networkTest“ with the required property.
t
what phillip says seems like the best solution to me. If not applyable to your usecase: Define a custom task that just checks these 2 flags, test depends on that custom task. If you for some reason 'really' 'really' need an ugly solution at configuration time you may use https://docs.gradle.org/current/kotlin-dsl/gradle/org.gradle.api.execution/-task-execution-graph/when-ready.html
z
unfortunately it’s an android project, or I would agree
t
Could you maybe define the secret token
Property<String>
to an empty string if absent and networkTest=false, and absent if networkTest=true (so it will fail the build)?
Copy code
val networkTestProvider = providers.gradleProperty("networkTest").map(String::toBoolean)
providers.gradleProperty("myServer.secretToken").zip(networkTestvider, (secretToken, networkTest) -> secretToken.takeUnless { it.isBlank() && networkTest == true })