Skip to content

Commit 4cc51a9

Browse files
authored
baseline-java-versions: javaCompiler property to compile Java code with a certain JDK version (#3342)
baseline-java-versions: Introduce an optional javaCompiler property to javaVersions which will determine which JDK is used to run the Java compiler. In this mode, --release is now used to target lower Java language versions/class file bytecode than the compiler.
1 parent 0e77ae6 commit 4cc51a9

22 files changed

+891
-349
lines changed

README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ _Baseline is a family of Gradle plugins for configuring Java projects with sensi
2121
| `com.palantir.baseline-reproducibility` | Sensible defaults to ensure Jar, Tar and Zip tasks can be reproduced
2222
| `com.palantir.baseline-exact-dependencies` | Ensures projects explicitly declare all the dependencies they rely on, no more and no less
2323
| `com.palantir.baseline-encoding` | Ensures projects use the UTF-8 encoding in compile tasks.
24-
| `com.palantir.baseline-release-compatibility` | Ensures projects targeting older JREs only compile against classes and methods available in those JREs.
2524
| `com.palantir.baseline-testing` | Configures test tasks to dump heap dumps (hprof files) for convenient debugging
2625
| `com.palantir.baseline-test-heap` | Increases the default Test task heap from 512m to 2g.
2726
| `com.palantir.baseline-java-compiler-diagnostics` | Applies the `-Xmaxwarns` and `-Xmaxwarns` compiler options with a very large limit to avoid truncating failure info.
@@ -242,12 +241,6 @@ checkImplicitDependencies {
242241

243242
This plugin sets the encoding for JavaCompile tasks to `UTF-8`.
244243

245-
## com.palantir.baseline-release-compatibility
246-
247-
This plugin adds the `--release <number>` flag to JavaCompile tasks (when the compiler [supports it](https://openjdk.java.net/jeps/247)), so that published jars will only use methods available in the target JRE. Relying on `sourceCompatibility = 1.8` and `targetCompatibility = 1.8` is insufficient because you run the risk of using method that have been added in newer JREs, e.g. `Optional#isEmpty`.
248-
249-
This plugin may become redundant if this functionality is implemented upstream [in Gradle](https://github.com/gradle/gradle/issues/2510).
250-
251244
## com.palantir.baseline-testing
252245

253246
Configures some sensible defaults:
@@ -289,15 +282,17 @@ The plugin is currently used on an opt-in basis. To use it, apply the plugin and
289282
apply plugin: 'com.palantir.baseline-java-versions'
290283
291284
javaVersions {
285+
javaCompiler = 25
292286
libraryTarget = 11
293287
distributionTarget = 17
294288
runtime = 21
295289
}
296290
```
297291

298292
The configurable fields of the `javaVersions` extension are:
299-
* `libraryTarget`: (required) The Java version used for compilation of libraries that are published.
300-
* `distributionTarget`: (optional) The Java version used for compilation of code used within distributions, but not published externally. Defaults to the `libraryTarget` version.
293+
* `javaCompiler`: (optional) The version of the Java compiler used. If not set, the appropriate `target` will be used. The `--release`, `--target` and `--source` compiler args are used to enable compiling code at a lower target than the compiler used. However, if `baseline-module-jvm-args`/`moduleJvmArgs` are used, `--release` will not be set as it is not compatible with exporting/opening system modules, meaning compilation will not fail if JDK APIs are used which are higher than the requested target.
294+
* `libraryTarget`: (required) The Java version targeted for compilation of libraries that are published.
295+
* `distributionTarget`: (optional) The Java version targeted for compilation of code used within distributions, but not published externally. Defaults to the `libraryTarget` version.
301296
* `runtime`: (optional) Runtime Java version for testing and packaging distributions. Defaults to the `distributionTarget` version.
302297

303298
The configured Java versions are used as defaults for all projects.
@@ -326,12 +321,14 @@ A sub-project can also explicitly override the default Java versions, but doing
326321
```gradle
327322
// In a sub-project's build.gradle
328323
javaVersion {
324+
javaCompiler = 17
329325
target = 11
330326
runtime = 11
331327
}
332328
```
333329

334330
The optionally configurable fields of the `javaVersion` extension are:
331+
* `javaCompiler`: The version of the Java compiler used. If not set, `target` will be used. See note in above `javaVersions` extension regarding `javaCompiler`.
335332
* `target`: The target version used for compilation.
336333
* `runtime`: The runtime version used for testing and distributions.
337334

gradle-baseline-java/build.gradle

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ dependencies {
6969
compileOnly 'org.immutables:value::annotations'
7070
}
7171

72+
// Used in BaselineJavaVersionIntegrationTest
73+
tasks.test.dependsOn ':gradle-baseline-java:test-compiler-plugins:publishToMavenLocal'
7274
// Used in BaselineConfigIntegrationTest
7375
tasks.test.dependsOn ':gradle-baseline-java-config:publishToMavenLocal'
7476
tasks.test.dependsOn tasks.named('publishToMavenLocal')
@@ -79,6 +81,8 @@ tasks.checkJUnitDependencies.enabled = false
7981
test {
8082
environment 'CIRCLE_ARTIFACTS', "${buildDir}/artifacts"
8183
environment 'CIRCLE_TEST_REPORTS', "${buildDir}/circle-reports"
84+
85+
environment 'ORG_GRADLE_PROJECT_baselineTestCompilerPluginsVersion', project.version
8286
// TEMPHACK(okelvin): remove this to decouple baseline-error-prone
8387
environment 'ORG_GRADLE_PROJECT_baselineErrorProneVersion', "0.2.0"
8488

@@ -146,13 +150,6 @@ gradlePlugin {
146150
description = 'Baseline Java is a collection of Gradle plugins for configuring code quality tools in builds.'
147151
tags.set(['java', 'checkstyle', 'code quality'])
148152
}
149-
baselineReleaseCompatibility {
150-
id = 'com.palantir.baseline-release-compatibility'
151-
displayName = 'Palantir Baseline Release Compatibility Plugin'
152-
implementationClass = 'com.palantir.baseline.plugins.BaselineReleaseCompatibility'
153-
description = 'Baseline Java is a collection of Gradle plugins for configuring code quality tools in builds.'
154-
tags.set(['java', 'checkstyle', 'code quality'])
155-
}
156153
baselinePreferProjectModules {
157154
id = 'com.palantir.baseline-prefer-project-modules'
158155
displayName = 'Palantir Baseline Prefer Project Modules Plugin'

gradle-baseline-java/src/main/java/com/palantir/baseline/plugins/Baseline.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public void apply(Project project) {
5757
proj.getPluginManager().apply(BaselineReproducibility.class);
5858
proj.getPluginManager().apply(BaselineClassUniquenessPlugin.class);
5959
proj.getPluginManager().apply(BaselineExactDependencies.class);
60-
proj.getPluginManager().apply(BaselineReleaseCompatibility.class);
6160
proj.getPluginManager().apply(BaselineTesting.class);
6261
proj.getPluginManager().apply(BaselineTestHeap.class);
6362
proj.getPluginManager().apply(BaselineJavaCompilerDiagnostics.class);

gradle-baseline-java/src/main/java/com/palantir/baseline/plugins/BaselineReleaseCompatibility.java

Lines changed: 0 additions & 131 deletions
This file was deleted.

0 commit comments

Comments
 (0)