Skip to content

Commit

Permalink
Add baseline-immutables plugin to enable incremental compilation for …
Browse files Browse the repository at this point in the history
…Immutables
  • Loading branch information
pkoenig10 committed May 3, 2021
1 parent 231ed03 commit 7892a7b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
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
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 -> {
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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implementation-class=com.palantir.baseline.plugins.BaselineImmutables

0 comments on commit 7892a7b

Please sign in to comment.