From 7d40da70f198cb93dc264ff6b749bc894a082599 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 17 Nov 2020 01:16:03 +0000 Subject: [PATCH 1/7] New com.palantir.baseline-enable-preview-flag plugin --- README.md | 25 +++++++++++- gradle-baseline-java/build.gradle | 7 ++++ .../plugins/BaselineEnablePreviewFlag.java | 40 +++++++++++++++++++ ...ir.baseline-enable-preview-flag.properties | 1 + 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlag.java create mode 100644 gradle-baseline-java/src/main/resources/META-INF/gradle-plugins/com.palantir.baseline-enable-preview-flag.properties diff --git a/README.md b/README.md index df71237c7..f58e0f535 100644 --- a/README.md +++ b/README.md @@ -385,7 +385,7 @@ The plugin also adds a `checkJUnitDependencies` to make the migration to JUnit5 3. For repos that use 'snapshot' style testing, it's convenient to have a single command to accept the updated snapshots after a code change. This plugin ensures that if you run tests with `./gradlew test -Drecreate=true`, the system property will be passed down to the running Java process (which can be detected with `Boolean.getBoolean("recreate")`). -## com.palantir.baseline-fix-gradle-java +## com.palantir.baseline-fix-gradle-java (off by default) Fixes up all Java [SourceSets](https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_source_sets) by marking their deprecated [configurations](https://docs.gradle.org/current/userguide/java_plugin.html#tab:configurations) @@ -398,3 +398,26 @@ now fulfil the "Bucket of dependencies" role described in that document, as they This will become the default in Gradle 7 and leaving these as they currently are can cause both unnecessary confusion (users looking in `compile` instead of `compileClasspath`) and [random crashes](https://github.com/gradle/gradle/issues/11844#issuecomment-585219427). + + +## 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. Sadly gradle requires you to add it in multiple places. This plugin is a short-hand for the +below: + +```gradle +tasks.withType(JavaCompile) { + options.compilerArgs += "--enable-preview" +} +tasks.withType(Test) { + jvmArgs += "--enable-preview" +} +tasks.withType(JavaExec) { + jvmArgs += "--enable-preview" +} +``` + +_Note that you must dial up `sourceCompatibility` to the current JVM version in order for this to work, as if you're +compiling using a java 15 installation, it doesn't make sense to try to lock sourceCompatibility to 11 but also enable +bleeding edge features that haven't yet stabilised in java 15._ diff --git a/gradle-baseline-java/build.gradle b/gradle-baseline-java/build.gradle index 1c4331d53..eb0c34004 100644 --- a/gradle-baseline-java/build.gradle +++ b/gradle-baseline-java/build.gradle @@ -81,6 +81,10 @@ gradlePlugin { id = 'com.palantir.baseline-release-compatibility' implementationClass = 'com.palantir.baseline.plugins.BaselineReleaseCompatibility' } + baselineEnablePreviewFlag { + id = 'com.palantir.baseline-enable-preview-flag' + implementationClass = 'com.palantir.baseline.plugins.BaselineEnablePreviewFlag' + } } } @@ -118,6 +122,9 @@ pluginBundle { baselineReleaseCompatibility { displayName = 'Palantir Baseline Release Compatibility Plugin' } + baselineEnablePreviewFlag { + displayName = 'Palantir Baseline --enable-preview Flag Plugin' + } } } diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlag.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlag.java new file mode 100644 index 000000000..5f85fb9d5 --- /dev/null +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlag.java @@ -0,0 +1,40 @@ +/* + * (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.baseline.plugins; + +import java.util.List; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.tasks.JavaExec; +import org.gradle.api.tasks.compile.JavaCompile; +import org.gradle.api.tasks.testing.Test; + +public final class BaselineEnablePreviewFlag implements Plugin { + @Override + public void apply(Project project) { + project.getTasks().withType(JavaCompile.class, t -> { + List args = t.getOptions().getCompilerArgs(); + args.add("--enable-preview"); // mutation is gross, but it's the gradle convention + }); + project.getTasks().withType(Test.class, t -> { + t.jvmArgs("--enable-preview"); + }); + project.getTasks().withType(JavaExec.class, t -> { + t.jvmArgs("--enable-preview"); + }); + } +} diff --git a/gradle-baseline-java/src/main/resources/META-INF/gradle-plugins/com.palantir.baseline-enable-preview-flag.properties b/gradle-baseline-java/src/main/resources/META-INF/gradle-plugins/com.palantir.baseline-enable-preview-flag.properties new file mode 100644 index 000000000..6ea86cf75 --- /dev/null +++ b/gradle-baseline-java/src/main/resources/META-INF/gradle-plugins/com.palantir.baseline-enable-preview-flag.properties @@ -0,0 +1 @@ +implementation-class=com.palantir.baseline.plugins.BaselineEnablePreviewFlag From c9067acf4d5cbefb2e6a06e8c6c80e0acb0811b6 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 17 Nov 2020 01:47:34 +0000 Subject: [PATCH 2/7] IntegrationSpec --- .../BaselineEnablePreviewFlagTest.groovy | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy new file mode 100644 index 000000000..4ee9ce1a2 --- /dev/null +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy @@ -0,0 +1,100 @@ +/* + * (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.baseline.plugins + +import nebula.test.IntegrationSpec +import nebula.test.functional.ExecutionResult +import spock.lang.IgnoreIf + +@IgnoreIf({ Integer.parseInt(jvm.javaSpecificationVersion) < 14 }) +class BaselineEnablePreviewFlagTest extends IntegrationSpec { + + def setup() { + buildFile << ''' + apply plugin: 'java-library' + apply plugin: 'com.palantir.baseline-enable-preview-flag' + apply plugin: 'application' + + application { + mainClass = 'foo.Foo' + } + '''.stripIndent() + + file('src/main/java/foo/Foo.java') << ''' + package foo; + public class Foo { + public record Coordinate(int x, int y) {} + + public static void main(String... args) { + System.out.println("Hello, world: " + new Coordinate(1, 2)); + } + } + '''.stripIndent() + } + + def 'compiles'() { + when: + buildFile << ''' + tasks.classes.doLast { + println "COMPILED:" + new File(sourceSets.main.java.outputDir, "foo").list() + } + ''' + ExecutionResult executionResult = runTasks('classes', '-is') + + then: + executionResult.getStandardOutput().contains('Foo$Coordinate.class') + executionResult.getStandardOutput().contains('Foo.class') + } + + def 'runs'() { + when: + ExecutionResult executionResult = runTasks('run', '-is') + + then: + assert executionResult.getStandardOutput().contains("Hello, world: Coordinate[x=1, y=2]") + } + + def 'testing works'() { + when: + buildFile << ''' + repositories { mavenCentral() } + dependencies { + testImplementation 'junit:junit:4.13.1' + } + ''' + + file('src/test/java/foo/FooTest.java') << ''' + package foo; + public final class FooTest { + @org.junit.Ignore("silly junit4 thinks this 'class' actually contains tests") + record Whatever(int x, int y) {} + + @org.junit.Test + public void whatever() { + Foo.main(); + System.out.println("Hello, world: " + new Whatever(1, 2)); + } + } + '''.stripIndent() + ExecutionResult executionResult = runTasks('test', '-is') + + then: + executionResult.getStandardOutput().contains("Hello, world: Coordinate[x=1, y=2]") + executionResult.getStandardOutput().contains("Hello, world: Whatever[x=1, y=2]") + } +} + From 522d43a150693e7dd7b4f46320b94ac38e91d7d8 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 17 Nov 2020 02:11:30 +0000 Subject: [PATCH 3/7] multiproject breaks --- .../BaselineEnablePreviewFlagTest.groovy | 57 +++++++++++++++++-- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy index 4ee9ce1a2..b6dc47bd2 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy @@ -16,15 +16,17 @@ package com.palantir.baseline.plugins + import nebula.test.IntegrationSpec import nebula.test.functional.ExecutionResult +import org.gradle.api.JavaVersion import spock.lang.IgnoreIf -@IgnoreIf({ Integer.parseInt(jvm.javaSpecificationVersion) < 14 }) +@IgnoreIf({JavaVersion.current() < JavaVersion.VERSION_14}) class BaselineEnablePreviewFlagTest extends IntegrationSpec { - def setup() { - buildFile << ''' + def setupSingleProject(File dir) { + new File(dir, "build.gradle") << ''' apply plugin: 'java-library' apply plugin: 'com.palantir.baseline-enable-preview-flag' apply plugin: 'application' @@ -34,7 +36,11 @@ class BaselineEnablePreviewFlagTest extends IntegrationSpec { } '''.stripIndent() - file('src/main/java/foo/Foo.java') << ''' + writeSourceFileContainingRecord(dir); + } + + private writeSourceFileContainingRecord(File dir) { + writeJavaSourceFile(''' package foo; public class Foo { public record Coordinate(int x, int y) {} @@ -43,11 +49,12 @@ class BaselineEnablePreviewFlagTest extends IntegrationSpec { System.out.println("Hello, world: " + new Coordinate(1, 2)); } } - '''.stripIndent() + '''.stripIndent(), dir) } def 'compiles'() { when: + setupSingleProject(projectDir) buildFile << ''' tasks.classes.doLast { println "COMPILED:" + new File(sourceSets.main.java.outputDir, "foo").list() @@ -62,6 +69,7 @@ class BaselineEnablePreviewFlagTest extends IntegrationSpec { def 'runs'() { when: + setupSingleProject(projectDir) ExecutionResult executionResult = runTasks('run', '-is') then: @@ -70,6 +78,7 @@ class BaselineEnablePreviewFlagTest extends IntegrationSpec { def 'testing works'() { when: + setupSingleProject(projectDir) buildFile << ''' repositories { mavenCentral() } dependencies { @@ -96,5 +105,43 @@ class BaselineEnablePreviewFlagTest extends IntegrationSpec { executionResult.getStandardOutput().contains("Hello, world: Coordinate[x=1, y=2]") executionResult.getStandardOutput().contains("Hello, world: Whatever[x=1, y=2]") } + + def 'multiproject'() { + when: + File java14Dir = addSubproject("my-java-14", ''' + apply plugin: 'java-library' + apply plugin: 'application' + + sourceCompatibility = 14 + application { + mainClass = 'foo.Foo' + } + dependencies { + implementation project(':my-java-14-preview') + } + ''') + + writeJavaSourceFile(''' + package bar; + public class Bar { + public static void main(String... args) { + foo.Foo.main(); + } + } + ''', java14Dir); + + File java14PreviewDir = addSubproject("my-java-14-preview", ''' + apply plugin: 'java-library' + apply plugin: 'com.palantir.baseline-enable-preview-flag' + + sourceCompatibility = 14 + ''') + + writeSourceFileContainingRecord(java14PreviewDir) + + then: + ExecutionResult executionResult = runTasks('run', '-is') + println executionResult.getStandardError() + } } From 098f435bc08c231e5a5296a6ff506932e5972881 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 17 Nov 2020 02:46:45 +0000 Subject: [PATCH 4/7] Safe to put it inside an allprojects block --- README.md | 22 +++++++--- .../plugins/BaselineEnablePreviewFlag.java | 43 +++++++++++++++++-- .../BaselineEnablePreviewFlagTest.groovy | 40 +++++++++++------ 3 files changed, 84 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index f58e0f535..5013827e5 100644 --- a/README.md +++ b/README.md @@ -403,10 +403,22 @@ This will become the default in Gradle 7 and leaving these as they currently are ## 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. Sadly gradle requires you to add it in multiple places. This plugin is a short-hand for the -below: +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.enable-preview-flag' +} +``` ```gradle +// shorthand for the below: tasks.withType(JavaCompile) { options.compilerArgs += "--enable-preview" } @@ -418,6 +430,6 @@ tasks.withType(JavaExec) { } ``` -_Note that you must dial up `sourceCompatibility` to the current JVM version in order for this to work, as if you're -compiling using a java 15 installation, it doesn't make sense to try to lock sourceCompatibility to 11 but also enable -bleeding edge features that haven't yet stabilised in java 15._ +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. diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlag.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlag.java index 5f85fb9d5..b80ef07a8 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlag.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlag.java @@ -16,25 +16,60 @@ package com.palantir.baseline.plugins; +import java.util.Collections; import java.util.List; +import org.gradle.api.JavaVersion; import org.gradle.api.Plugin; import org.gradle.api.Project; +import org.gradle.api.plugins.JavaPluginConvention; +import org.gradle.api.provider.Provider; import org.gradle.api.tasks.JavaExec; import org.gradle.api.tasks.compile.JavaCompile; import org.gradle.api.tasks.testing.Test; +import org.gradle.process.CommandLineArgumentProvider; public final class BaselineEnablePreviewFlag implements Plugin { + + private static final String FLAG = "--enable-preview"; + @Override public void apply(Project project) { + // The idea behind registering a single 'extra property' is that other plugins (like + // sls-packaging) can easily detect this and also also add the --enable-preview jvm arg + Provider enablePreview = project.provider(() -> { + JavaVersion jvmExecutingGradle = JavaVersion.current(); + JavaPluginConvention javaConvention = project.getConvention().findPlugin(JavaPluginConvention.class); + if (javaConvention == null) { + return false; + } + + return javaConvention.getSourceCompatibility() == jvmExecutingGradle; + }); + + project.getExtensions().getExtraProperties().set("enablePreview", enablePreview); + project.getTasks().withType(JavaCompile.class, t -> { - List args = t.getOptions().getCompilerArgs(); - args.add("--enable-preview"); // mutation is gross, but it's the gradle convention + List args = t.getOptions().getCompilerArgumentProviders(); + args.add(new MaybeEnablePreview(enablePreview)); // mutation is gross, but it's the gradle convention }); project.getTasks().withType(Test.class, t -> { - t.jvmArgs("--enable-preview"); + t.getJvmArgumentProviders().add(new MaybeEnablePreview(enablePreview)); }); project.getTasks().withType(JavaExec.class, t -> { - t.jvmArgs("--enable-preview"); + t.getJvmArgumentProviders().add(new MaybeEnablePreview(enablePreview)); }); } + + private static class MaybeEnablePreview implements CommandLineArgumentProvider { + private final Provider shouldEnable; + + MaybeEnablePreview(Provider shouldEnable) { + this.shouldEnable = shouldEnable; + } + + @Override + public Iterable asArguments() { + return shouldEnable.get() ? Collections.singletonList(FLAG) : Collections.emptyList(); + } + } } diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy index b6dc47bd2..d22c33733 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy @@ -22,7 +22,7 @@ import nebula.test.functional.ExecutionResult import org.gradle.api.JavaVersion import spock.lang.IgnoreIf -@IgnoreIf({JavaVersion.current() < JavaVersion.VERSION_14}) +@IgnoreIf({JavaVersion.current() < JavaVersion.VERSION_14}) // this class uses records, which were introduced in java 14 class BaselineEnablePreviewFlagTest extends IntegrationSpec { def setupSingleProject(File dir) { @@ -73,7 +73,7 @@ class BaselineEnablePreviewFlagTest extends IntegrationSpec { ExecutionResult executionResult = runTasks('run', '-is') then: - assert executionResult.getStandardOutput().contains("Hello, world: Coordinate[x=1, y=2]") + executionResult.getStandardOutput().contains("Hello, world: Coordinate[x=1, y=2]") } def 'testing works'() { @@ -108,11 +108,16 @@ class BaselineEnablePreviewFlagTest extends IntegrationSpec { def 'multiproject'() { when: - File java14Dir = addSubproject("my-java-14", ''' + buildFile << ''' + subprojects { apply plugin: 'java-library' + apply plugin: 'com.palantir.baseline-enable-preview-flag' + } + ''' + + File java14Dir = addSubproject("my-java-14", ''' apply plugin: 'application' - sourceCompatibility = 14 application { mainClass = 'foo.Foo' } @@ -120,7 +125,6 @@ class BaselineEnablePreviewFlagTest extends IntegrationSpec { implementation project(':my-java-14-preview') } ''') - writeJavaSourceFile(''' package bar; public class Bar { @@ -130,18 +134,30 @@ class BaselineEnablePreviewFlagTest extends IntegrationSpec { } ''', java14Dir); - File java14PreviewDir = addSubproject("my-java-14-preview", ''' + File java14PreviewDir = addSubproject("my-java-14-preview") + writeSourceFileContainingRecord(java14PreviewDir) + + + then: + ExecutionResult executionResult = runTasks('run', '-is') + executionResult.getStandardOutput().contains 'Hello, world: Coordinate[x=1, y=2]' + } + + def 'does not always apply'() { + when: + File java8Dir = addSubproject('my-java-8-api', "sourceCompatibility = 8") + buildFile << ''' + subprojects { apply plugin: 'java-library' apply plugin: 'com.palantir.baseline-enable-preview-flag' - - sourceCompatibility = 14 - ''') + } + ''' + writeSourceFileContainingRecord(java8Dir) - writeSourceFileContainingRecord(java14PreviewDir) + ExecutionResult result = runTasks('my-java-8-api:classes', '-is') then: - ExecutionResult executionResult = runTasks('run', '-is') - println executionResult.getStandardError() + result.getStandardError().contains("use --enable-preview to enable records") } } From b89897d4640f950736625c56dc5d83f905bbf1bf Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 17 Nov 2020 02:55:18 +0000 Subject: [PATCH 5/7] typo --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5013827e5..646b1e822 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,7 @@ Safe Logging can be found at [github.com/palantir/safe-logging](https://github.c - `LogsafeArgName`: Prevent certain named arguments as being logged as safe. Specify unsafe argument names using `LogsafeArgName:UnsafeArgNames` errorProne flag. - `ImplicitPublicBuilderConstructor`: Prevent builders from unintentionally leaking public constructors. - `ImmutablesBuilderMissingInitialization`: Prevent building Immutables.org builders when not all fields have been populated. -- `UnnecessarilyQualified`: Types should not be qualified if they are also imported. +- `UnnecessarilyQualified`: Types should not be qualified if they are also imported. - `DeprecatedGuavaObjects`: `com.google.common.base.Objects` has been obviated by `java.util.Objects`. - `JavaTimeSystemDefaultTimeZone`: Avoid using the system default time zone. @@ -389,15 +389,15 @@ This plugin ensures that if you run tests with `./gradlew test -Drecreate=true`, Fixes up all Java [SourceSets](https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_source_sets) by marking their deprecated [configurations](https://docs.gradle.org/current/userguide/java_plugin.html#tab:configurations) -- `compile` and `runtime` - as well as the `compileOnly` configuration as not resolvable +- `compile` and `runtime` - as well as the `compileOnly` configuration as not resolvable (can't call resolve on them) and not consumable (can't be depended on from other projects). See [here](https://docs.gradle.org/current/userguide/declaring_dependencies.html#sec:resolvable-consumable-configs) -for a more in-depth discussion on what these terms mean. By configuring them thusly, we are saying that these configurations +for a more in-depth discussion on what these terms mean. By configuring them thusly, we are saying that these configurations now fulfil the "Bucket of dependencies" role described in that document, as they should. This will become the default in Gradle 7 and leaving these as they currently are can cause both unnecessary confusion -(users looking in `compile` instead of `compileClasspath`) and [random crashes](https://github.com/gradle/gradle/issues/11844#issuecomment-585219427). +(users looking in `compile` instead of `compileClasspath`) and [random crashes](https://github.com/gradle/gradle/issues/11844#issuecomment-585219427). ## com.palantir.baseline-enable-preview-flag (off by default) @@ -413,7 +413,7 @@ is undesirable to deeply couple a repo to a particular Java version as it makes ```gradle // root build.gradle allprojects { - apply plugin: 'com.palantir.enable-preview-flag' + apply plugin: 'com.palantir.baseline-enable-preview-flag' } ``` From 71baf048f976eedf295d471fa75a656334e7587d Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 17 Nov 2020 02:55:18 +0000 Subject: [PATCH 6/7] Add generated changelog entries --- changelog/@unreleased/pr-1549.v2.yml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 changelog/@unreleased/pr-1549.v2.yml diff --git a/changelog/@unreleased/pr-1549.v2.yml b/changelog/@unreleased/pr-1549.v2.yml new file mode 100644 index 000000000..9507ce681 --- /dev/null +++ b/changelog/@unreleased/pr-1549.v2.yml @@ -0,0 +1,8 @@ +type: feature +feature: + description: |- + Add the `apply plugin: 'com.palantir.baseline-enable-preview-flag'` to your subprojects block to enable the usage of unreleased java features (e.g. records, switch expressions, var keyword etc). + + Note, this plugin is a no-op on any project where you have a low sourceCompatibility. + links: + - https://github.com/palantir/gradle-baseline/pull/1549 From ad738aa8f8f42e7a91f9daabee2d36c8f59b7f29 Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 17 Nov 2020 14:31:27 +0000 Subject: [PATCH 7/7] javadoc --- .../plugins/BaselineEnablePreviewFlag.java | 40 ++++++++++++++----- .../BaselineEnablePreviewFlagTest.groovy | 13 +++++- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlag.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlag.java index b80ef07a8..ad6c10856 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlag.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlag.java @@ -25,7 +25,9 @@ import org.gradle.api.provider.Provider; import org.gradle.api.tasks.JavaExec; import org.gradle.api.tasks.compile.JavaCompile; +import org.gradle.api.tasks.javadoc.Javadoc; import org.gradle.api.tasks.testing.Test; +import org.gradle.external.javadoc.CoreJavadocOptions; import org.gradle.process.CommandLineArgumentProvider; public final class BaselineEnablePreviewFlag implements Plugin { @@ -48,15 +50,35 @@ public void apply(Project project) { project.getExtensions().getExtraProperties().set("enablePreview", enablePreview); - project.getTasks().withType(JavaCompile.class, t -> { - List args = t.getOptions().getCompilerArgumentProviders(); - args.add(new MaybeEnablePreview(enablePreview)); // mutation is gross, but it's the gradle convention - }); - project.getTasks().withType(Test.class, t -> { - t.getJvmArgumentProviders().add(new MaybeEnablePreview(enablePreview)); - }); - project.getTasks().withType(JavaExec.class, t -> { - t.getJvmArgumentProviders().add(new MaybeEnablePreview(enablePreview)); + project.getPlugins().withId("java", _unused -> { + project.getTasks().withType(JavaCompile.class, t -> { + List args = t.getOptions().getCompilerArgumentProviders(); + args.add(new MaybeEnablePreview(enablePreview)); // mutation is gross, but it's the gradle convention + }); + project.getTasks().withType(Test.class, t -> { + t.getJvmArgumentProviders().add(new MaybeEnablePreview(enablePreview)); + }); + project.getTasks().withType(JavaExec.class, t -> { + t.getJvmArgumentProviders().add(new MaybeEnablePreview(enablePreview)); + }); + + // sadly we have to use afterEvaluate because the Javadoc task doesn't support passing in providers + project.afterEvaluate(_unused2 -> { + if (enablePreview.get()) { + JavaVersion sourceCompat = project.getConvention() + .getPlugin(JavaPluginConvention.class) + .getSourceCompatibility(); + project.getTasks().withType(Javadoc.class, t -> { + CoreJavadocOptions options = (CoreJavadocOptions) t.getOptions(); + + // Yes truly javadoc wants a single leading dash, other javac wants a double leading dash. + // We also have to use these manual string options because they don't have first-class methods + // yet (e.g. https://github.com/gradle/gradle/issues/12898) + options.addBooleanOption("-enable-preview", true); + options.addStringOption("source", sourceCompat.getMajorVersion()); + }); + } + }); }); } diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy index d22c33733..b4bd1f417 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy @@ -22,7 +22,8 @@ import nebula.test.functional.ExecutionResult import org.gradle.api.JavaVersion import spock.lang.IgnoreIf -@IgnoreIf({JavaVersion.current() < JavaVersion.VERSION_14}) // this class uses records, which were introduced in java 14 +@IgnoreIf({JavaVersion.current() < JavaVersion.VERSION_14}) +// this class uses records, which were introduced in java 14 class BaselineEnablePreviewFlagTest extends IntegrationSpec { def setupSingleProject(File dir) { @@ -43,6 +44,7 @@ class BaselineEnablePreviewFlagTest extends IntegrationSpec { writeJavaSourceFile(''' package foo; public class Foo { + /** Hello this is some javadoc. */ public record Coordinate(int x, int y) {} public static void main(String... args) { @@ -67,6 +69,15 @@ class BaselineEnablePreviewFlagTest extends IntegrationSpec { executionResult.getStandardOutput().contains('Foo.class') } + def 'javadoc'() { + when: + setupSingleProject(projectDir) + + then: + ExecutionResult executionResult = runTasks('javadoc', '-is') + assert executionResult.getSuccess() ?: executionResult.getStandardOutput() + } + def 'runs'() { when: setupSingleProject(projectDir)