From 7892a7b07e0e0b3b50d4ebba4389f80bf8f5557d Mon Sep 17 00:00:00 2001 From: Patrick Koenig Date: Mon, 3 May 2021 09:16:40 -0700 Subject: [PATCH 1/2] Add baseline-immutables plugin to enable incremental compilation for Immutables --- README.md | 9 +++ .../palantir/baseline/plugins/Baseline.java | 1 + .../baseline/plugins/BaselineImmutables.java | 61 +++++++++++++++++++ ...om.palantir.baseline-immutables.properties | 1 + 4 files changed, 72 insertions(+) create mode 100644 gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineImmutables.java create mode 100644 gradle-baseline-java/src/main/resources/META-INF/gradle-plugins/com.palantir.baseline-immutables.properties diff --git a/README.md b/README.md index e2c94bb65..6ce7b20f8 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ _Baseline is a family of Gradle plugins for configuring Java projects with sensi | `com.palantir.baseline-encoding` | Ensures projects use the UTF-8 encoding in compile tasks. | `com.palantir.baseline-release-compatibility` | Ensures projects targetting older JREs only compile against classes and methods available in those JREs. | `com.palantir.baseline-testing` | Configures test tasks to dump heap dumps (hprof files) for convenient debugging +| `com.palantir.baseline-immutables` | Enables incremental compilation for the [Immutables](http://immutables.github.io/) annotation processor. See also the [Baseline Java Style Guide and Best Practices](./docs). @@ -403,6 +404,14 @@ 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-immutables + +This plugin enables incremental compilation for the [Immutables](http://immutables.github.io/) annotation processor. + +This plugin adds the `-Aimmutables.gradle.incremental` compiler arg to the compile Java task for any source set whose annotation processor configuration contains the Immutables annotation processor. + +For more details, see the Immutables incremental compilation [tracking issue](https://github.com/immutables/immutables/issues/804). + ## 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) diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/Baseline.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/Baseline.java index 4f16c51ca..b8fe497b5 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/Baseline.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/Baseline.java @@ -48,6 +48,7 @@ public void apply(Project project) { proj.getPluginManager().apply(BaselineReleaseCompatibility.class); proj.getPluginManager().apply(BaselineTesting.class); proj.getPluginManager().apply(BaselineJavaParameters.class); + proj.getPluginManager().apply(BaselineImmutables.class); // TODO(dsanduleac): enable this when people's idea{} blocks no longer reference things like // configurations.integrationTestCompile diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineImmutables.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineImmutables.java new file mode 100644 index 000000000..bd1c0eef8 --- /dev/null +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineImmutables.java @@ -0,0 +1,61 @@ +/* + * (c) Copyright 2021 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.Objects; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.artifacts.Dependency; +import org.gradle.api.plugins.JavaPluginConvention; +import org.gradle.api.tasks.SourceSet; +import org.gradle.api.tasks.compile.JavaCompile; + +public final class BaselineImmutables implements Plugin { + + @Override + public void apply(Project project) { + project.getPluginManager().withPlugin("java", unused -> { + project.afterEvaluate(proj -> { + proj.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().stream() + .filter(sourceSet -> hasImmutablesProcessor(project, sourceSet)) + .forEach(sourceSet -> addImmutablesIncrementalCompilerArg(project, sourceSet)); + }); + }); + } + + private static boolean hasImmutablesProcessor(Project project, SourceSet sourceSet) { + return project + .getConfigurations() + .getByName(sourceSet.getAnnotationProcessorConfigurationName()) + .getDependencies() + .stream() + .anyMatch(BaselineImmutables::isImmutablesValue); + } + + private static boolean isImmutablesValue(Dependency dep) { + return Objects.equals(dep.getGroup(), "org.immutables") && Objects.equals(dep.getName(), "value"); + } + + private static void addImmutablesIncrementalCompilerArg(Project project, SourceSet sourceSet) { + project.getTasks() + .named(sourceSet.getCompileJavaTaskName(), JavaCompile.class) + .get() + .getOptions() + .getCompilerArgs() + .add("-Aimmutables.gradle.incremental"); + } +} diff --git a/gradle-baseline-java/src/main/resources/META-INF/gradle-plugins/com.palantir.baseline-immutables.properties b/gradle-baseline-java/src/main/resources/META-INF/gradle-plugins/com.palantir.baseline-immutables.properties new file mode 100644 index 000000000..fbeb235aa --- /dev/null +++ b/gradle-baseline-java/src/main/resources/META-INF/gradle-plugins/com.palantir.baseline-immutables.properties @@ -0,0 +1 @@ +implementation-class=com.palantir.baseline.plugins.BaselineImmutables From fc19cc42a182fe229cb53c877ad61e9aa3120be5 Mon Sep 17 00:00:00 2001 From: Patrick Koenig Date: Mon, 3 May 2021 16:16:40 +0000 Subject: [PATCH 2/2] Add generated changelog entries --- changelog/@unreleased/pr-1750.v2.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/@unreleased/pr-1750.v2.yml diff --git a/changelog/@unreleased/pr-1750.v2.yml b/changelog/@unreleased/pr-1750.v2.yml new file mode 100644 index 000000000..13a4a7608 --- /dev/null +++ b/changelog/@unreleased/pr-1750.v2.yml @@ -0,0 +1,5 @@ +type: feature +feature: + description: Adds the `baseline-immutables` plugin to enable incremental compilation for Immutables. + links: + - https://github.com/palantir/gradle-baseline/pull/1750