dgs-codegen icon indicating copy to clipboard operation
dgs-codegen copied to clipboard

All of `build/generated` is added to main sourceSet.

Open mlewe opened this issue 4 years ago • 3 comments

The plugin in it's default configuration adds the directory build/generated to the main sourceSet.

However, this is problematic, since this directory is already used as the output directory for other methods of code generation, like annotation processors.

I ran into this problem in a project where an annotation processor is used for the test sourceSet for which the generated code is placed in build/generated/sources/annotationProcessor.java/test. Since these generated classes have dependencies which are only available in the test classpath, adding build/generated to the main sourceSet breaks the build process.

I'm able to work around this by changing the output directory for this plugin and manipulating the main sourceSet like so:

tasks.withType(com.netflix.graphql.dgs.codegen.gradle.GenerateJavaTask) {
    generatedSourcesDir = "${project.buildDir.absolutePath}/dgs-codegen"
}

sourceSets {
    main {
        java {
            def dirs = srcDirs
            dirs.remove(file("${project.buildDir.absolutePath}/generated"))
            srcDirs = dirs
            srcDir "${project.buildDir.absolutePath}/dgs-codegen/generated"
        }
    }
}

But I believe that it would be better if this plugin would put its generated sources in some sub directory of build/generated instead, where they don't interfere with other generated sources.

mlewe avatar Feb 16 '21 17:02 mlewe

I hope to add a subfolder for the generated codes by this codegen.

For example, for Java , add build/generated/java.

And for Kotlin, add build/generated/kotlin instead.

hantsy avatar Jun 14 '21 07:06 hantsy

@mlewe You save my life! Thank you.

I have same problem.

https://netflix.github.io/dgs/generating-code-from-schema/#configuring-code-generation I tried generatedSourcesDir and outputDir, but neither worked.

outputDir

generateJava {
    // Error: Cannot set the value of read-only property 'outputDir' for task ':generateJava' of type com.netflix.graphql.dgs.codegen.gradle.GenerateJavaTask.
    outputDir = file('generated/sources/annotationProcessor/java/main')
    ...
}

generatedSourcesDir

generateJava {
    // I don't know why, but the source was not generated.
    generatedSourcesDir = 'build/dgs'
    ...
}

Eventually I was able to get DGS to coexist with other libraries that use the annotation processor with the following configuration. Originally, gradle build was successful, but I was suffering from compilation errors in Visual Studio Code.

tasks.withType(com.netflix.graphql.dgs.codegen.gradle.GenerateJavaTask) {
    generatedSourcesDir = "${project.buildDir.absolutePath}/dgs-codegen"
}

sourceSets {
    main {
        java {
            def dirs = srcDirs
            dirs.remove(file("${project.buildDir.absolutePath}/generated"))
            dirs.add(file("${project.buildDir.absolutePath}/generated/sources/annotationProcessor/java/main"))
            srcDirs = dirs
            srcDir "${project.buildDir.absolutePath}/dgs-codegen/generated"
        }
    }
}

convcha avatar Jun 14 '21 13:06 convcha

I've tried the workaround.

In our project the "build/generated" folder is still a source set. Which versions are you using?

BerniBurnsen avatar Jul 06 '21 09:07 BerniBurnsen