How to lazily configure the Gradle task?

I am trying to configure the following custom task:

task antecedeRelease(type: AntecedeReleaseTask) {
  antecedeWithVersion = project.'antecede-with-version'
  antecedeToVersion = project.'antecede-to-version'
}

The problem is that the properties antecede-with-versionand antecede-to-versionmust be set via the command line with the option -P. If they are not installed and are antecedeReleasenot called, this should not be the cause of the error:

$ ./gradlew tasks
org.gradle.api.GradleScriptException: A problem occurred evaluating project ...
Caused by: groovy.lang.MissingPropertyException: Could not find property 'antecede-with-version' on project ...

I could conditionally define a task antecedeReleaseso that it is defined only if these properties are defined, but I would like to keep the file build.gradleas clean as possible.

+4
source share
2 answers

If you need a task antecedeReleaseto run lazily at the end, at the end of the configuration phase, or at the beginning of the execution phase, it is bestdoFirst

task antecedeRelease(type: AntecedeReleaseTask) {
  doFirst {
    antecedeWithVersion = project.'antecede-with-version'
    antecedeToVersion = project.'antecede-to-version'
  }
}
+1

Groovy elvis :

task antecedeRelease(type: AntecedeReleaseTask) {
  antecedeWithVersion = project.ext.get('antecede-with-version') ?: 'unused'
  antecedeToVersion = project.ext.get('antecede-with-version') ?: 'unused'
}

, project.ext.has('property') .

0

Source: https://habr.com/ru/post/1620626/


All Articles