Skip to content

Commit bf567c5

Browse files
committed
Fix dependency reporting
This has been broken since 9.0.3. likely due to some swallowed api change in Gradle. This fixes and adds test coverage to dependency reporting used in DRA artifacts building
1 parent ebd868b commit bf567c5

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/BuildPluginFuncTest.groovy

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class BuildPluginFuncTest extends AbstractGradleFuncTest {
5353
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.""".stripIndent()
5454

5555
def setup() {
56-
configurationCacheCompatible = false
5756
buildFile << """
5857
plugins {
5958
id 'java'
@@ -167,6 +166,25 @@ class BuildPluginFuncTest extends AbstractGradleFuncTest {
167166
result.task(":loggerUsageCheck").outcome == TaskOutcome.SKIPPED
168167
}
169168

169+
def "can generate dependency infos file"() {
170+
given:
171+
repository.generateJar("junit", "junit", "4.12", 'org.acme.JunitMock')
172+
repository.configureBuild(buildFile)
173+
file("licenses/junit-4.12.jar.sha1").text = "2973d150c0dc1fefe998f834810d68f278ea58ec"
174+
file("licenses/junit-LICENSE.txt").text = EXAMPLE_LICENSE
175+
file("licenses/junit-NOTICE.txt").text = "mock notice"
176+
buildFile << """
177+
dependencies {
178+
api "junit:junit:4.12"
179+
}
180+
"""
181+
when:
182+
def result = gradleRunner("dependenciesInfo").build()
183+
then:
184+
result.task(":dependenciesInfo").outcome == TaskOutcome.SUCCESS
185+
file("build/reports/dependencies/dependencies.csv").text == "junit:junit,4.12,https://repo1.maven.org/maven2/junit/junit/4.12,BSD-3-Clause,\n"
186+
}
187+
170188
def assertValidJar(File jar) {
171189
try (ZipFile zipFile = new ZipFile(jar)) {
172190
ZipEntry licenseEntry = zipFile.getEntry("META-INF/LICENSE.txt")

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/DependenciesInfoPlugin.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.gradle.api.attributes.Usage;
1818
import org.gradle.api.plugins.JavaPlugin;
1919

20+
import static org.elasticsearch.gradle.internal.util.DependenciesUtils.createNonTransitiveArtifactsView;
21+
2022
public class DependenciesInfoPlugin implements Plugin<Project> {
2123

2224
public static String USAGE_ATTRIBUTE = "DependenciesInfo";
@@ -25,16 +27,15 @@ public class DependenciesInfoPlugin implements Plugin<Project> {
2527
public void apply(final Project project) {
2628
project.getPlugins().apply(CompileOnlyResolvePlugin.class);
2729
var depsInfo = project.getTasks().register("dependenciesInfo", DependenciesInfoTask.class);
28-
2930
depsInfo.configure(t -> {
3031
var runtimeConfiguration = project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME);
31-
t.getRuntimeArtifacts().set(project.getProviders().provider(() -> runtimeConfiguration.getIncoming().getArtifacts()));
32+
t.getRuntimeArtifacts()
33+
.set(project.getProviders().provider(() -> createNonTransitiveArtifactsView(runtimeConfiguration).getArtifacts()));
3234
t.getClasspath().from(runtimeConfiguration);
3335
var compileOnlyConfiguration = project.getConfigurations()
3436
.getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME);
3537
t.getCompileOnlyArtifacts().set(project.getProviders().provider(() -> compileOnlyConfiguration.getIncoming().getArtifacts()));
3638
t.getClasspath().from(compileOnlyConfiguration);
37-
3839
});
3940
Configuration dependenciesInfoFilesConfiguration = project.getConfigurations().create("dependenciesInfoFiles");
4041
dependenciesInfoFilesConfiguration.setCanBeResolved(false);

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/DependenciesInfoTask.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.gradle.api.tasks.Optional;
3131
import org.gradle.api.tasks.OutputFile;
3232
import org.gradle.api.tasks.TaskAction;
33+
import org.gradle.internal.component.external.model.ModuleComponentArtifactIdentifier;
3334

3435
import java.io.File;
3536
import java.io.IOException;
@@ -94,8 +95,9 @@ private Provider<Set<ModuleComponentIdentifier>> mapToModuleComponentIdentifiers
9495
() -> artifacts.getArtifacts()
9596
.stream()
9697
.map(r -> r.getId())
97-
.filter(id -> id instanceof ModuleComponentIdentifier)
98-
.map(id -> (ModuleComponentIdentifier) id)
98+
.filter(mcaId -> mcaId instanceof ModuleComponentArtifactIdentifier)
99+
.map(mcaId -> (ModuleComponentArtifactIdentifier) mcaId)
100+
.map(it -> it.getComponentIdentifier())
99101
.collect(Collectors.toSet())
100102
);
101103
}
@@ -143,7 +145,6 @@ public DependenciesInfoTask(ProjectLayout projectLayout, ObjectFactory objectFac
143145

144146
@TaskAction
145147
public void generateDependenciesInfo() throws IOException {
146-
147148
final Set<String> compileOnlyIds = getCompileOnlyModules().map(
148149
set -> set.stream()
149150
.map(id -> id.getModuleIdentifier().getGroup() + ":" + id.getModuleIdentifier().getName() + ":" + id.getVersion())

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/util/DependenciesUtils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.github.jengelman.gradle.plugins.shadow.ShadowBasePlugin;
1313

14+
import org.gradle.api.artifacts.ArtifactView;
1415
import org.gradle.api.artifacts.Configuration;
1516
import org.gradle.api.artifacts.ResolvableDependencies;
1617
import org.gradle.api.artifacts.component.ComponentIdentifier;
@@ -31,6 +32,14 @@ public static FileCollection createFileCollectionFromNonTransitiveArtifactsView(
3132
Configuration configuration,
3233
Spec<ComponentIdentifier> componentFilter
3334
) {
35+
return createNonTransitiveArtifactsView(configuration, componentFilter).getFiles();
36+
}
37+
38+
public static ArtifactView createNonTransitiveArtifactsView(Configuration configuration) {
39+
return createNonTransitiveArtifactsView(configuration, identifier -> true);
40+
}
41+
42+
public static ArtifactView createNonTransitiveArtifactsView(Configuration configuration, Spec<ComponentIdentifier> componentFilter) {
3443
ResolvableDependencies incoming = configuration.getIncoming();
3544
return incoming.artifactView(viewConfiguration -> {
3645
Provider<Set<ComponentIdentifier>> firstLevelDependencyComponents = incoming.getResolutionResult()
@@ -47,7 +56,7 @@ public static FileCollection createFileCollectionFromNonTransitiveArtifactsView(
4756
viewConfiguration.componentFilter(
4857
new AndSpec<>(identifier -> firstLevelDependencyComponents.get().contains(identifier), componentFilter)
4958
);
50-
}).getFiles();
59+
});
5160
}
5261

5362
/**

0 commit comments

Comments
 (0)