# gradle


06/27/2023, 4:33 AM
How would I go about making a task using kotlin dsl that reads some remote files, and generates kotlin code that can be used by the main module?


06/27/2023, 4:41 AM
mostly the same way you'd do it outside of Gradle
if the remote files can be downloaded by Gradle (through a custom repository) then they can take place in build caching, otherwise you'll have to manage caching yourself (or don't cache)

Adam S

06/27/2023, 6:55 AM
for a task that generates code I’d prefer creating a standalone task in buildSrc. It would be possible with an adhoc task in a gradle.kts file, or to create a class, but a separate file helps with organisation and prevents accidentally using properties from the script within the task (which Gradle will let you do, but it can cause weird issues) Like @ephemient said, downloading a file using a custom repository is best for performance It’s also fine to download it using a Java HTTP client, or to use another Gradle plugin (gradle-download-task for example). I’d recommend splitting up the downloading&unpacking task from the generation task. It helps separate concerns, and Gradle can better cache and/or re-use task outputs. As for being able to use the generated code in the main module - that’s pretty easy! You can map the code generation task to a
, and add that as source directory. Gradle will then understand that it needs to run the generation task in order to build the source set, so the generation task will be triggered automatically Take a look at this answer for a more basic code generation example (it’s only missing the downloading step)


06/28/2023, 6:03 AM
I'd also use onlyForConfigurations on the repository but yes, that's the approach