How do I "compile" generated resources (i.e include in runtime classpath and jar/war)


(Martin Ahrer) #1

In some earlier attempt I created generated resource files using a fairly simple approach:

def generatedResources = "$buildDir/generated-resources/main"
    sourceSets {
        main {
            output.dir(generatedResources, builtBy: 'generateArtifactInfo')
        }
    }
    task generateArtifactInfo {
        doLast {
            def directory = new File(generatedResources, "META-INF/${project.group}/${project.name}")
          // stripped off code
          }
    }

Now I also need that resources in the IDE for running local tests etc. So instead of adding the files into a build output folder I added the generated sources nicely into the project source structure. I chose this directory layout as I have other plugins also generating code such as QueryDSL.

def generatedResourcesFolder = "src/main/generated/resources"
    task generateArtifactInfo {
        doLast {
            def directory = new File(generatedResourcesFolder, "META-INF/${project.group}/${project.name}")
            // stripped off code
        }
    }
    sourceSets {
        generated {
            resources {
                srcDirs += generatedResourcesFolder
            }
        }
    }
    idea {
        module {
            sourceDirs += file(generatedResourcesFolder)
        }
    }

Unfortunately now the sources are no longer included in the runtime nor are they included in the jar - the generated resources exist on disk in the folder ‘’‘build/resources/main’’’ though.

I tried adding for example:

processResources {
        dependsOn generateArtifactInfo
        from generatedResourcesFolder
        include '**/*'
    }

But still no resources are added. What is the best way first to express the dependency (on task level of file level) to the generated resources so they are included. I found this very elegant in my first attempt.


(Peter Niederwieser) #2

I’d stick to your initial solution. If the problem is that the IDE ignores the generated resources because it ignores everything under ‘build’, I’d switch to some other directory outside the source tree (e.g. ‘$projectDir/generated-resources’), and would let the ‘clean’ task know about it (‘clean.delete generatedResources’). Alternatively, you could reconfigure the IDE (via the Gradle Eclipse/IntelliJ plugin) to exclude all subdirectories of ‘build’ except ‘build/generated-resources’.


(Martin Ahrer) #3

I think I have been able to find a solution that is taking a mix of both approaches. Here is the working code (IDE + console):

def generatedResourcesFolder = "$projectDir/src/main/generated/resources"
    sourceSets {
        main {
            output.dir(generatedResourcesFolder, builtBy: 'generateArtifactInfo')
            resources.srcDirs generatedResourcesFolder
        }
    }
    task generateArtifactInfo {
        doLast {
            def directory = new File(generatedResourcesFolder, "META-INF/${project.group}/${project.name}")
            //stripped code
        }
    }
    clean {
        delete generatedResourcesFolder
    }
    idea {
        module {
            sourceDirs += file(generatedResourcesFolder)
        }
    }

So this leaves me with the question if it is possible to completely separate the generated resources using an isolated source set so I don’t mess up the main source set as I did with my earlier attempt.

sourceSets {
        generated {
            resources {
                srcDirs += generatedResourcesFolder
            }
        }
    }

(Peter Niederwieser) #4

You shouldn’t have to add the generated resources dir both as ‘output.dir’ and ‘resources.srcDir’. If you want to have generated resources further processed by the ‘processResources’ task, use ‘resources.srcDir’ (and add a task dependency), otherwise ‘output.dir’ (together with ‘builtBy’). If you choose ‘resources.srcDir’, the dir should show up in intellij without any further configuration.

Using a separate source set only complicates matters, and I fail to see a benefit.

‘src/main/generated/’ is typically used for files that are generated once and then put under source control. I’d either use ‘build/generated-resources/’ and reconfigure excluded intellij dirs (feels right but can be brittle because you’ll have to list subdirectories of ‘build/’ one-by-one), or I’d use ‘generated-resources/’ and reconfigure the ‘clean’ task.


(Martin Ahrer) #5

Thanks for your support. I will try to get that straigth!