Gradle: format generated sources
See original GitHub issueSpotless works great and I’m very happy with it. I’d like the plugin to format generated sources as well, so that all source code is consistently formatted when debugging. I haven’t been able to get them formatted though;
Attempt: changing the order of tasks
I initially thought sources are being generated in the compileJava
task and spotless formats before this task is executed (which would make sense because then .class files and .java files are consistent). However, when I add a dependency to javaCompile
the generated sources are still not formatted:
project.afterEvaluate{
compileJava.dependsOn("licenseFormat")
spotlessApply.dependsOn("compileJava")
}
How to format generated sources?
What is the recommended way to format sources? My sources are generated in build/generated/source/apt
and this is my current spotless config:
spotless {
enforceCheck = false
format 'misc', {
target '**/*.gradle', '**/*.md', '**/.gitignore'
trimTrailingWhitespace()
indentWithTabs()
endWithNewline()
paddedCell() /* Makes sure build succeeds, even if formatting fails. See https://github.com/diffplug/spotless/blob/master/PADDEDCELL.md */
}
java {
target fileTree(rootDir) {
include '**/*.java'
}
removeUnusedImports() // removes any unused imports
eclipse().configFile 'src/build/resources/eclipse-java-formatter-config.xml'
paddedCell() /* Makes sure build succeeds, even if formatting fails. See https://github.com/diffplug/spotless/blob/master/PADDEDCELL.md */
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Right way to generate sources in Gradle 7 - Help/Discuss
We place generated sources in a folder below the target build folder; e.g., build/generated/main/java . You will easily get used to that if...
Read more >How can I add a generated Source Folder to my Source Path ...
It generates new java sources in build/source/apt . Here is my build.gradle: apply plugin: 'java' apply plugin: 'eclipse' apply plugin: ...
Read more >Generated sources are not picked up anymore in a Gradle build
I have a Gradle-based project in which we generate a QueryDSL class for each class that was annotated with javax.persistence.Entity. Up till IntelliJ...
Read more >How to generate Java sources using buildSrc Gradle project ...
Generate Java sources using buildSrc Gradle project and Codemodel. Check out these helpful tips.
Read more >Plugins - OpenAPI Generator
... build->plugins section (default phase is generate-sources phase) ... classpath "org.openapitools:openapi-generator-gradle-plugin:5.0.0".
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you wanted to generate sources, then format those sources, and maintain up-to-date support (not just incremental, but any kind of up-to-date), then you need a structure like this:
src/main/java -> src/main/generated-unformatted
src/main/generated-unformatted -> src/main/generated
(src/main/java, src/main/generated) -> build/classes
Unless you have that intermediary
generated-unformatted
dir, then you’re mashing the results of a previous task, which means that thegenerateCode
task will never be up-to-date, thusspotlessApply
andcompile
will never be up-to-date either.Spotless does not support this mode of operation right now. I’m a big fan of code-generation, but I personally don’t care how it’s formatted any more than how my
.class
files are formatted. If you wanted to format the generated code, you’d need to:Those tasks seem like a lot of work for little benefit to me, but I’m happy to merge PR’s for it if they don’t add too much complexity to the docs or implementation.
Thanks for both your suggestions. I added a feature request for
gradle-apt-plugin
here.