Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Gradle Project Extension Properties In Configuration Phase

When writing a custom Gradle plugin, how is it possible to access the extension properties defined in the consuming build.gradle in the custom plugin’s configuration phase?

Please see the following 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'
}

Wanted behavior: The checkstyle plugin uses the configuration file defined in the build.gradle codechecks extension.

Actual behavior: The default value in the CodechecksPluginExtension is being used because the build.gradle codechecks extension is not yet evaluated.

I already tried putting all uses of the codechecks extension in the plugins into closures but they won’t expand correctly due to class casting issues at execution phase.

Thanks for your help!

like image 556
Patrick Bergner Avatar asked Mar 21 '14 14:03

Patrick Bergner


1 Answers

project.afterEvaluate works for me.
Try:

project.afterEvaluate {
    project.checkstyle {
      configFile = project.codechecks.checkstyleConfig
    }
}
like image 87
janezj Avatar answered Sep 17 '22 14:09

janezj