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

Add baseline-immutables plugin to enable incremental compilation for Immutables #1750

Merged
merged 2 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1750.v2.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Project> {

@Override
public void apply(Project project) {
project.getPluginManager().withPlugin("java", unused -> {
project.afterEvaluate(proj -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tracking down this immutables behaviour and building this plugin! I'm excited for the speedup it should give repos.

Just made a follow up PR to remove this use of afterEvaluate - it's technically deprecated (although I don't think Gradle will ever be able to remove it) and the proliferation of afterEvaluates has caused us issues in the past to do with the order they run in. Luckily, we can use some of the newer lazy APIs to avoid this: #1752

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, thanks for fixing this! Didn't know that afterEvaluate was deprecated. Wasn't sure when this should be applied so I just copied code from an existing plugin.

proj.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().stream()
.filter(sourceSet -> hasImmutablesProcessor(project, sourceSet))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary to avoid compiler warnings about unrecognized annotation processor options.

warning: The following options were not recognized by any processor: '[immutables.gradle.incremental]'
error: warnings found and -Werror specified
1 error
1 warning

.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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implementation-class=com.palantir.baseline.plugins.BaselineImmutables