question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gradle: format generated sources

See original GitHub issue

Spotless 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:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nedtwiggcommented, Nov 16, 2017

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:

  • task generateCode src/main/java -> src/main/generated-unformatted
  • task spotlessApply src/main/generated-unformatted -> src/main/generated
  • task compile (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 the generateCode task will never be up-to-date, thus spotlessApply and compile 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:

  • break-up your code generator so that it doesn’t compile the generated code automatically
  • add support for spotless to slurp from one place and output to another
  • remove the generated code from compilation, and inject the formatted code for compilation

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.

0reactions
rolfwcommented, Nov 17, 2017

Thanks for both your suggestions. I added a feature request for gradle-apt-plugin here.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found