google-java-format-gradle-plugin icon indicating copy to clipboard operation
google-java-format-gradle-plugin copied to clipboard

Nested projects don't play well with default input files

Open JakeWharton opened this issue 5 years ago • 2 comments

By using the project directory as the default input, nested projects are automatically formatted.

For example,

.
├── LICENSE.txt
├── README.md
├── foo
│   ├── bar
│   │   ├── src/main/java
│   │   └── build.gradle
│   ├── src/main/java
│   └── build.gradle
├ build.gradle
└ settings.gradle

This is a basic example. The real project has many more modules. The idea, though, is that the root build.gradle is applying the plugin to all subprojects except for certain ones. In this case, let's say we're excluding "bar".

The problem, though, is that by applying it in "foo" the sources in "bar" will automatically be formatted.

For now, I can configure this myself and have each project specify only its Java source folders as GJF sources, but it would be nice if this was the default. Operating outside of these folders would then become the thing which required explicit configuration.

JakeWharton avatar Jun 26 '19 02:06 JakeWharton

There are arguments against using the project's java sourceSets as default input (see https://github.com/sherter/google-java-format-gradle-plugin/issues/3 and https://github.com/sherter/google-java-format-gradle-plugin/issues/11). I'm not sure if this is still an issue for Android builds.

Adding default excludes for subproject directories might be a solution. Would that solve your problem?

sherter avatar Jun 26 '19 19:06 sherter

Ah, good 'ol Android Gradle plugin. I did search for history on the topic but didn't read past the issue descriptions. The important bits are in the follow-up comments of those.

There's really only two plugins (or at least their supertypes) that cover 99.99% of use cases, right? The Java base plugin and Android base plugin. Detecting these would give you a working plugin without configuration in almost every case. Anyone doing something more bespoke could still do something custom.

Excluding subproject directories would certainly seem to solve it, though.

JakeWharton avatar Jun 26 '19 20:06 JakeWharton