Best practices surrounding generating sources (or resource files)?

I’d like to generate some resource files and have those be included in jars, wars, etc (assuming that jar, war, etc. depend on the task that generates the resources). Creating a dir (inside project.buildDir) and adding it to the main sourceSet (via project.sourceSets.main.resources.srcDir “${project.buildDir/some/src/dir”) didn’t lead to the resources being included in the resulting jar or war files.

What should I be doing instead? I’m looking for something that doesn’t require me to configure the inputs to jar, war, etc. individually.

Adding a resource dir, either to ‘sourceSets.main.resources’ (if resources should still be processed) or to ‘sourceSets.main.output’, is the right approach. Maybe you didn’t establish the necessary task dependencies? ‘processResources.dependsOn(taskThatGeneratesResources)’ in the former case, ‘classes.dependsOn(taskThatGeneratesResources)’ in the latter.

I added it as a dependency to the war task; I’ll try adding it to processResources instead. (How should I be finding out about this stuff? I didn’t even know about the processResources task.)

I’m generating a plain text resource that simply needs to be included in the artifact with no further processing. Should that go in resources or output?

Should that go in resources or output?

This should be added with ‘sourceSets.main.output.dir …’. You can also add it directly to the War (‘war.from …’), but the former is somewhat cleaner (e.g. the dir will show up in IDEs).

How should I be finding out about this stuff?

Studying the relevant chapters of the user guide is a good start to learn about these things. The many samples in the full Gradle distribution are another valuable source of information.

It does appear to be working using ‘sourceSets.main.output.dir’, thanks!

My question about documentation was more on why adding a dependency to the ‘war’ task didn’t work but ‘processResources’ / ‘classes’ (as applicable) would work.

‘classes’ is a lifecycle task responsible for generating classes and resources, so it’s the right hook. However, ‘war.dependsOn’ should work as long as the ‘war’ task is configured correctly to include the resource. Actually, even ‘war.from generatorTask’ (potentially combined with ‘into’) should work, provided ‘generatorTask’ declares its outputs.