Use Gradle Project Extension Properties at Configuration Step

When creating a custom Gradle plugin, how can I access the extension properties defined in the consuming build.gradleone in the custom plugins setup phase?

See the next MWE.

build.gradle

apply plugin: 'codechecks'

codechecks {
  checkstyleConfig = '/home/user/checkstyle.xml'
}

CodechecksPlugin.groovy

class CodechecksPlugin implements Plugin<Project> {

  void apply(Project project) {
    project.extensions.create('codechecks', CodechecksPluginExtension)
    project.apply( [ plugin: 'checkstyle' ] )

    project.checkstyle {
      configFile = project.codechecks.checkstyleConfig
    }
  }
}

CodechecksPluginExtension.groovy

class CodechecksPluginExtension {
  def checkstyleConfig = 'config/checkstyle/checkstyle.xml'
}

Required Behavior: The plugin checkstyleuses the configuration file defined in the extension build.gradle codechecks.

Actual behavior: The default value of is used CodechecksPluginExtensionbecause the extension build.gradle codecheckshas not yet been evaluated.

I have already tried using all extension extensions codechecksin plugins in closure, but they will not expand correctly due to problems with releasing the class at runtime.

Thank you for your help!

+4
1

project.afterEvaluate .
:

project.afterEvaluate {
    project.checkstyle {
      configFile = project.codechecks.checkstyleConfig
    }
}
+5

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


All Articles