Transition gradle 2.x to 4.1 issue - Copy to classesDir

I’ve just transitioned from gradle 2.x to 4.1 and hit a problem with this custom task:

task copyEcore(type: Copy) {
	from "src-gen/com/magnicomp/model/magnicomp.ecore"
	into "${sourceSets.main.output.classesDir}/com/magnicomp/model"

compileJava.dependsOn copyEcore

I get an error that classesDir is not supported. I do see a new classesDirs variable but I’m not sure if I should use that or not.

The purpose of the task above is to copy the .ecore file that is generated via Eclipse IDE into a directory that is included in the classpath when the jar file is created. The .ecore file is required in the classpath at runtime.

What’s the correct way of doing this with gradle 4.x?

This should work for all versions of Gradle. Copying files into output directories of other tasks can lead to incremental build problems.

You can register src-gen as a resources directory:

sourceSets.main.resources.srcDir "src-gen"

processResources will make sure the files are in the right place for everything else to see those files (e.g., test, jar).

But I assume that src-gen is generated by another task? If so, you can just register it as another output directory:

sourceSets.main.output.dir("src-gen", builtBy: 'srcGeneratorTask')

Currently we have a parent project which contains a bunch of sub projects. In the parent we do this currently to configure the generate source dir:

allprojects {
  ext {
    generatedSrcDir = "src-gen"

So should we replace this in the parent build.gradle with your suggestion (below) and remove our Copy task from each sub project?

sourceSets.main.resources.srcDir "src-gen"

Just to close this issue for others with this problem…

I used Sterling’s suggestion in our rootProject/build.gradle as follows:

subprojects {
  sourceSets.main.resources.srcDir "src-gen"

Then I removed the Copy tasks for the subprojects and everything worked!

Thanks Sterling!