Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support java { toolchain { languageVersion #724

Open
anuraaga opened this issue Oct 22, 2020 · 7 comments
Open

Support java { toolchain { languageVersion #724

anuraaga opened this issue Oct 22, 2020 · 7 comments

Comments

@anuraaga
Copy link
Contributor

Currently, the spotless invocation is inlined into the task execution itself.

https://github.com/diffplug/spotless/blob/main/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java#L50

Is it possible to instead run a separate main file in JavaExec? Gradle has introduced toolchain feature to allow build to control the Java version used for compile and execution which works very well to allow any user's Java version to run the build with a specific one. But unfortunately, this currently doesn't affect Spotless which runs in Gradle itself - for example, this means that a build must become unbuildable on Java 8 to upgrade to the latest googleJavaFormat since it requires higher Java version. It would be nice if this could be avoided.

@nedtwigg nedtwigg changed the title Use JavaExec to run spotless in Gradle instead of directly within Gradle Support `java Oct 22, 2020
@nedtwigg
Copy link
Member

I think it's a great idea to support the toolchain feature. For now, it would be very difficult, and is not likely to happen soon.

@tbroyer
Copy link
Contributor

tbroyer commented Mar 31, 2021

I wonder if that's not going to be required (forking, not necessarily using toolchains) to support JDK 16 for Google Java Format:
https://github.com/google/google-java-format/releases/tag/v1.10.0

@nedtwigg
Copy link
Member

nedtwigg commented Apr 1, 2021

I wonder if Gradle might be setting some of those same flags for itself already

@tbroyer
Copy link
Contributor

tbroyer commented Apr 1, 2021

It doesn't:

BTW, to support Java Toolchains, I believe you'd have to start by using the Worker API, with process isolation (possibly conditionally) so you can setExecutable() to the toolchain's executable (https://docs.gradle.org/current/userguide/toolchains.html#sec:plugins).
Using the Worker API, with process isolation (for forking), even without specifying an executable, would be a required first step for those Google Java Format --add-exports JVM args.

@nedtwigg
Copy link
Member

@aciccarello I'm still happy to merge your proposal, but it might be worth reading this before investing too much time: https://jakewharton.com/gradle-toolchains-are-rarely-a-good-idea/

@aciccarello
Copy link

Did you mean to ping @anuraaga?

Regarding that article, it brings up some good points about java compatibility and managing version but I don't completely agree with the conclusions. I think toolchains are still a helpful way to select an installed JDK. I also thought that this section (under "Not all bad") was relevant.

Some tools that dip into JDK internals regularly break on newer versions of the compiler because they rely on unstable APIs. I’m thinking about things like Google Java Format or Error-Prone. No need to hold the rest of your project from enjoying the latest JDK, if those tools are run via a JavaExec task you can use a toolchain to keep them on an older JDK until a newer version is available.

@anuraaga
Copy link
Contributor Author

I've been out of the Java world for some time so don't have much stake in this anymore. But do still remember those tough issues with Java versions and tools.

I agree completely with Jake, using toolchains to pick an old version of Java only for generating old bytecode is a bad idea, indeed javadoc is an obvious reason. I would personally still use toolchains to compile code in my projects to ensure contributors use a consistent version - but that would be the latest (maybe LTS) Java.

But as @aciccarello quotes nicely, when running code that requires an old version of Java, toolchains can make unrunnable, runnable and are very convenient for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants