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

baseline-java-versions allows opting in to --enable-preview #2322

Merged
merged 20 commits into from
Jul 29, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
README
  • Loading branch information
iamdanfox committed Jul 14, 2022
commit 6efb465806756c77a576e0f372367efa7fa2d7e6
61 changes: 27 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -416,40 +416,6 @@ This plugin adds the `-Aimmutables.gradle.incremental` compiler arg to the compi

For more details, see the Immutables incremental compilation [tracking issue](https://github.com/immutables/immutables/issues/804).

## com.palantir.baseline-enable-preview-flag (off by default)

As described in [JEP 12](https://openjdk.java.net/jeps/12), Java allows you to use shiny new syntax features if you add
the `--enable-preview` flag. However, gradle requires you to add it in multiple places. This plugin can be applied to
within an allprojects block and it will automatically ugprade any project which is already using the latest
sourceCompatibility by adding the necessary `--enable-preview` flags to all of the following task types.

_Note, this plugin should be used with **caution** because preview features may change or be removed, and it
is undesirable to deeply couple a repo to a particular Java version as it makes upgrading to a new major Java version harder._

```gradle
// root build.gradle
allprojects {
apply plugin: 'com.palantir.baseline-enable-preview-flag'
}
```

```gradle
// shorthand for the below:
tasks.withType(JavaCompile) {
options.compilerArgs += "--enable-preview"
}
tasks.withType(Test) {
jvmArgs += "--enable-preview"
}
tasks.withType(JavaExec) {
jvmArgs += "--enable-preview"
}
```

If you've explicitly specified a lower sourceCompatibility (e.g. for a published API jar), then this plugin is a no-op.
In fact, Java will actually error if you try to switch on the `--enable-preview` flag to get cutting edge syntax
features but set `sourceCompatibility` (or `--release`) to an older Java version.

## com.palantir.baseline-java-versions

This plugin allows consistent configuration of JDK versions via [Gradle Toolchains](https://docs.gradle.org/current/userguide/toolchains.html).
@@ -482,3 +448,30 @@ javaVersion {
```

The optionally configurable fields of the `javaVersion` extension are `target`, for setting the target version used for compilation and `runtime`, for setting the runtime version used for testing and distributions.

### Opting in to `--enable-preview` flag

As described in [JEP 12](https://openjdk.java.net/jeps/12), Java allows you to use incubating syntax features if you add the `--enable-preview` flag. Gradle requires you to add it in many places (including on JavaCompile, Javadoc tasks, as well as in production and on execution tasks like Test, JavaExec). The baseline-java-versions plugin provides a shorthand way of enabling this:

```gradle
// root build.gradle
apply plugin: 'com.palantir.baseline-java-versions'
javaVersions {
libraryTarget = 11
distributionTarget = '17_PREVIEW'
runtime = '17_PREVIEW'
}

// shorthand for configuring all the tasks individually, e.g.
tasks.withType(JavaCompile) {
options.compilerArgs += "--enable-preview"
}
tasks.withType(Test) {
jvmArgs += "--enable-preview"
}
tasks.withType(JavaExec) {
jvmArgs += "--enable-preview"
}
```

_Note, this plugin should be used with **caution** because preview features may change or be removed, which might make upgrading to a new Java version harder._
Original file line number Diff line number Diff line change
@@ -94,7 +94,7 @@ class BaselineJavaVersionIntegrationTest extends IntegrationSpec {
def setup() {
// Fork needed or build fails on circleci with "SystemInfo is not supported on this operating system."
// Comment out locally in order to get debugging to work
setFork(true)
// setFork(true)

buildFile << standardBuildFile
}