jacocoTestReport task fails if JaCoCo plugin is applied in afterEvaluate

Hi,

I’m creating a plugin for my company which should automatically apply different code quality ensurance plugins and configure useful defaults. The user should be able to configure which plugins are applied via a plugin extension. Currently, the plugin uses ‘afterEvaluate’ to apply the plugins according to the plugin extension configuration. If JaCoCo is applied in afterEvaluate running the jacocoTestReport task yields this error:

Caused by: org.gradle.api.tasks.TaskValidationException: Some problems were found with the configuration of task ':jacocoTestReport'.
 at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:55)
 at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:42)
 at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
 at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
 at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
 at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:296)
 at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.executeTask(AbstractTaskPlanExecutor.java:79)
 at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:63)
 at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:51)
 at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:23)
 at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:86)
 at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:29)
 at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:61)
 at org.gradle.execution.DefaultBuildExecuter.access$200(DefaultBuildExecuter.java:23)
 at org.gradle.execution.DefaultBuildExecuter$2.proceed(DefaultBuildExecuter.java:67)
 at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
 at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:61)
 at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:54)
 at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:148)
 at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:105)
 ... 44 more
Caused by: org.gradle.api.InvalidUserDataException: No value has been specified for property 'classDirectories'.
 at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:47)
 ... 63 more

Using project.configure() instead of project.afterEvaluate() fixes the problem but than the plugin extension values are still set to default even if the user has specified non default values.

apply plugin: 'java'
apply plugin: ExampleGradlePlugin
   testExtension {
 applyJacoco = true
}
class TestExtension {
  boolean applyJacoco = false
}
   class ExampleGradlePlugin implements Plugin<Project> {
      @Override
  void apply(Project project) {
         // create 'testExtension' project extension
    TestExtension testExt = project.extensions.create('testExtension', TestExtension)
         // Code below does not apply the plugin even if specified by the user testExt is still false
    project.configure(project) {
      if(testExt.applyJacoco ){
        project.apply plugin: JacocoPlugin
      }
    }
          // Code below applies the plugin if specified by the user but jacocoTestReport yields an error
    project.afterEvaluate {
      if(testExt.applyJacoco ){
        project.apply plugin: JacocoPlugin
      }
    }
  }
}

How can I configure the project with the plugin extension being filled with user specified values?

Thanks!

1 Like