Skip to content

Commit

Permalink
baseline-exact-dependencies stops resolving compileOnly (#1260)
Browse files Browse the repository at this point in the history
Fix baseline-exact-dependencies to no longer resolve compileOnly directly.

Instead, we create another configuration extending from `compileOnly`, which we tell to resolve only compile variants (so they'll be guaranteed to be the same variants as what's resolved by `compileClasspath`).
Also, excluding `annotationProcessor` was bogus because annotationProcessor is entirely independent from the other java configurations and doesn't end up in compileClasspath at all.
  • Loading branch information
dansanduleac authored Feb 26, 2020
1 parent 9fd368b commit d8be9cd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1260.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Fix baseline-exact-dependencies to no longer resolve compileOnly directly.
links:
- https://github.com/palantir/gradle-baseline/pull/1260
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.gradle.api.artifacts.ResolvedDependency;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.artifacts.component.ProjectComponentIdentifier;
import org.gradle.api.attributes.Usage;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
Expand All @@ -65,17 +66,26 @@ public void apply(Project project) {
.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
Configuration compileClasspath =
project.getConfigurations().getByName(JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME);
Configuration compileOnlyClasspath =
Configuration compileOnly =
project.getConfigurations().getByName(JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME);
Configuration annotationProcessorClasspath =
project.getConfigurations().getByName(JavaPlugin.ANNOTATION_PROCESSOR_CONFIGURATION_NAME);
Configuration justCompileOnlyResolvable = project.getConfigurations()
.create("baseline-exact-dependencies-compileOnly", conf -> {
conf.setVisible(false);
conf.setCanBeConsumed(false);
conf.extendsFrom(compileOnly);
// Important! this ensures we resolve 'compile' variants rather than 'runtime'
// This is the same attribute that's being set on compileClasspath
conf.getAttributes()
.attribute(
Usage.USAGE_ATTRIBUTE,
project.getObjects().named(Usage.class, Usage.JAVA_API));
});

project.getTasks().create("checkUnusedDependencies", CheckUnusedDependenciesTask.class, task -> {
task.dependsOn(JavaPlugin.CLASSES_TASK_NAME);
task.setSourceClasses(mainSourceSet.getOutput().getClassesDirs());
task.dependenciesConfiguration(compileClasspath);
task.sourceOnlyConfiguration(compileOnlyClasspath);
task.sourceOnlyConfiguration(annotationProcessorClasspath);
task.sourceOnlyConfiguration(justCompileOnlyResolvable);

// this is liberally applied to ease the Java8 -> 11 transition
task.ignore("javax.annotation", "javax.annotation-api");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ public final void checkUnusedDependencies() {
.collect(Collectors.toSet());
BaselineExactDependencies.INDEXES.populateIndexes(declaredDependencies);

Set<ResolvedArtifact> necessaryArtifacts = Streams.stream(
sourceClasses.get().iterator())
.flatMap(BaselineExactDependencies::referencedClasses)
.map(BaselineExactDependencies.INDEXES::classToDependency)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toSet());
Set<ResolvedArtifact> declaredArtifacts = declaredDependencies.stream()
.flatMap(dependency -> dependency.getModuleArtifacts().stream())
.filter(dependency ->
Expand All @@ -83,6 +76,14 @@ public final void checkUnusedDependencies() {

excludeSourceOnlyDependencies();

Set<ResolvedArtifact> necessaryArtifacts = Streams.stream(
sourceClasses.get().iterator())
.flatMap(BaselineExactDependencies::referencedClasses)
.map(BaselineExactDependencies.INDEXES::classToDependency)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toSet());

Set<ResolvedArtifact> possiblyUnused = Sets.difference(declaredArtifacts, necessaryArtifacts);
getLogger()
.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class BaselineExactDependenciesTest extends AbstractPluginTest {
plugins {
id 'java'
id 'com.palantir.baseline-exact-dependencies'
id 'com.palantir.baseline' apply false
}
'''.stripIndent()

Expand All @@ -43,6 +44,22 @@ class BaselineExactDependenciesTest extends AbstractPluginTest {
with('checkUnusedDependencies', 'checkImplicitDependencies', '--stacktrace').build()
}

def 'both tasks vacuously pass with no dependencies when entire baseline is applied'() {
when:
buildFile << standardBuildFile
buildFile << """
repositories {
jcenter()
mavenLocal() // for baseline-error-prone
}
apply plugin: 'com.palantir.baseline'
""".stripIndent()
file('src/main/java/pkg/Foo.java') << minimalJavaFile

then:
with('checkUnusedDependencies', 'checkImplicitDependencies', '--stacktrace').build()
}

def 'tasks are not run as part of ./gradlew check'() {
when:
buildFile << standardBuildFile
Expand Down

0 comments on commit d8be9cd

Please sign in to comment.