Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: custom source set as dependency for the main and test ones

I've created custom source set in Gradle project to keep all generated code:

sourceSets {
  generated {
    java {
      srcDir 'src/generated/java'
    }
    resources {
      srcDir 'src/generated/resources'
    }
  }
}

I want to make the result of this source set's code compilation available at compile and run time for main and test source sets.

What's the right semantic way to do it in Gradle?

UPDATE:

As suggested here: How do I add a new sourceset to Gradle? doesn't work for me, I still get java.lang.ClassNotFoundException when I launch my app (though compilation and unit tests run fine). Here is what I tried:

sourceSets {
  main {
    compileClasspath += sourceSets.generated.output
    runtimeClasspath += sourceSets.generated.output
  }

  test {
    compileClasspath += sourceSets.generated.output
    runtimeClasspath += sourceSets.generated.output
  }
}
like image 443
Anton Moiseev Avatar asked Jan 16 '14 12:01

Anton Moiseev


People also ask

What is a Gradle source set?

1.1 What is a Gradle SourceSet ? A SourceSet is a collection of java source files and additional resource files that are compiled and assembled together to be executed. The main idea of sourcesets is to group files with a common meaning for the project, with no need of separate them in another project.

How do I add a dependency in Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.

What is runtimeClasspath in Gradle?

main. runtimeClasspath' contains the compiled classes of the source set, and task autowiring automatically adds the necessary task dependencies. Maybe you want 'sourceSets. main. compileClasspath'.

What is CompileOnly in Gradle?

compileOnly dependencies are available while compiling but not when running them. This is equivalent to the provided scope in maven. It means that everyone who wants to execute it needs to supply a library with all classes of the CompileOnly library.


1 Answers

sourceSets {
    main {
        compileClasspath += generated.output
        runtimeClasspath += generated.output
    }
}

Same for the test source set.

like image 160
Peter Niederwieser Avatar answered Sep 28 '22 00:09

Peter Niederwieser