How to get project.sourceSets.test.runtimeClasspath in custom gradle plugin

I want to create gradle plugin which will add additional task with type JavaExec

I have plugin:

class CustomPlugin implements Plugin<Project> {
  @Override
  void apply(Project project) {

    project.task('generate', type: JavaExec) {
      main = 'some.package.ClassWithMainInTestScope'
      args = ['some', 'arguments']
      classpath project.sourceSets.test.runtimeClasspath
    }
  }
}

And i've got following error

FAILURE: Build failed with an exception.

* Where:
Build file '/home/pompei/IdeaProjects/greetgo.depinject/greetgo.depinject.gradle/build/test_projects/pr-20180720-164840-381-109260619/test/build.gradle' line: 3

* What went wrong:
An exception occurred applying plugin request [id: 'kz.greetgo.depinject.plugin']
> Failed to apply plugin [id 'kz.greetgo.depinject.plugin']
   > Could not get unknown property 'sourceSets' for root project 'test' of type org.gradle.api.Project.

Qustion is: how can i get project.sourceSets in plugin?

Upvotes: 2

Views: 1927

Answers (2)

It works!!!

I do like this:

project.task('generate', type: JavaExec) {
      doFirst {
        def javaPluginConvention = project.getConvention().getPlugin(JavaPluginConvention)
        classpath javaPluginConvention.getSourceSets().findByName("test").getRuntimeClasspath()
      }

      main = 'some.package.ClassWithMainInTestScope'
      args = ['some', 'args']
    }

And all OK!

Upvotes: 1

Maxim Kasyanov
Maxim Kasyanov

Reputation: 1058

Try it:

    final JavaPluginConvention javaPlugin = getProject().getConvention().getPlugin(JavaPluginConvention.class);
    final SourceSetContainer sourceSets = javaPlugin.getSourceSets();
    final SourceSet smoketest = sourceSets.findByName("smoketest");
    this.testClasspath = smoketest.getRuntimeClasspath();

Upvotes: 2

Related Questions