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

baseline-exact-dependencies stops resolving compileOnly #1260

Merged
merged 6 commits into from
Feb 26, 2020
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
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