Skip to content

Commit

Permalink
Use packaging from POMs to drive artifactViews
Browse files Browse the repository at this point in the history
  • Loading branch information
eygraber committed Nov 30, 2023
1 parent 5da5fee commit 5130e94
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.junit.jupiter.api.io.TempDir;
import org.spdx.tools.SpdxToolsHelper.SerFileType;
import org.spdx.tools.SpdxVerificationException;
Expand Down Expand Up @@ -229,6 +230,7 @@ public void canRunOnPluginProject() throws IOException, SpdxVerificationExceptio
}

@SuppressWarnings("ResultOfMethodCallIgnored")
@EnabledIfEnvironmentVariable(named = "CI", matches = "true")
@Test
public void canResolveVariantDependencies() throws IOException {
File app = new File(projectDir, "app");
Expand Down
80 changes: 60 additions & 20 deletions src/main/java/org/spdx/sbom/gradle/SpdxSbomPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
import org.gradle.api.artifacts.result.ArtifactResult;
import org.gradle.api.artifacts.result.ResolvedArtifactResult;
import org.gradle.api.artifacts.result.ResolvedComponentResult;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.artifacts.type.ArtifactTypeDefinition;
import org.gradle.api.internal.GradleInternal;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.internal.component.local.model.OpaqueComponentIdentifier;
import org.spdx.sbom.gradle.SpdxSbomExtension.Target;
import org.spdx.sbom.gradle.maven.PomInfo;
import org.spdx.sbom.gradle.maven.PomResolver;
import org.spdx.sbom.gradle.project.DocumentInfo;
import org.spdx.sbom.gradle.project.ProjectInfo;
Expand Down Expand Up @@ -127,31 +128,38 @@ private void createTaskForTarget(
t.getSpdxKnownLicensesService().set(knownLicenseServiceProvider);
t.usesService(knownLicenseServiceProvider);

final String artifactType;
if (project.getPlugins().hasPlugin("com.android.base")) {
artifactType = "android-aar-or-jar";
} else {
artifactType = "jar";
}
boolean hasAndroidPlugin = project.getPlugins().hasPlugin("com.android.base");

List<String> configurationNames = target.getConfigurations().get();
var pomPackagingsProperty = project.getObjects().setProperty(String.class);
var rootComponentsProperty =
project.getObjects().listProperty(ResolvedComponentResult.class);
for (var configurationName : configurationNames) {
var incoming =
project.getConfigurations().getByName(configurationName).getIncoming();

Provider<Set<ResolvedArtifactResult>> artifacts =
project
.getConfigurations()
.getByName(configurationName)
.getIncoming()
.artifactView(
viewConfiguration ->
viewConfiguration.attributes(
attributes ->
attributes.attribute(
Attribute.of("artifactType", String.class),
artifactType)))
.getArtifacts()
.getResolvedArtifacts();
pomPackagingsProperty.flatMap(
packagings -> {
var resolvedArtifactsProperty =
project.getObjects().setProperty(ResolvedArtifactResult.class);
for (String packaging : packagings) {
resolvedArtifactsProperty.addAll(
incoming
.artifactView(
viewConfiguration ->
viewConfiguration.attributes(
attributes ->
attributes.attribute(
ArtifactTypeDefinition
.ARTIFACT_TYPE_ATTRIBUTE,
packaging)))
.getArtifacts()
.getResolvedArtifacts());
}

return resolvedArtifactsProperty;
});
t.getResolvedArtifacts().putAll(artifacts.map(new ArtifactTransformer()));

Provider<ResolvedComponentResult> rootComponent =
Expand All @@ -166,6 +174,38 @@ private void createTaskForTarget(
}
t.getRootComponents().addAll(rootComponentsProperty);

var pomInfo =
rootComponentsProperty.map(
rootComponents -> {
PomResolver pomResolver =
PomResolver.newPomResolver(
project.getDependencies(),
project.getConfigurations(),
project.getLogger());

var resolvedPomArtifacts =
pomResolver.resolvePomArtifacts(rootComponents);
return pomResolver.effectivePoms(resolvedPomArtifacts);
});

pomPackagingsProperty.addAll(
pomInfo.map(
poms ->
poms.values().stream()
.map(PomInfo::getPackaging)
.map(
packaging -> {
boolean isAarOrJar = packaging.equals("jar") || packaging.equals("aar");
if(hasAndroidPlugin && isAarOrJar) {
return "android-aar-or-jar";
}
else {
return packaging;
}
}
)
.collect(Collectors.toSet())));

t.getPoms()
.putAll(
rootComponentsProperty.map(
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/spdx/sbom/gradle/maven/PomInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public interface PomInfo {

List<DeveloperInfo> getDevelopers();

String getPackaging();

@Immutable
interface LicenseInfo {
String getUrl();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/spdx/sbom/gradle/maven/PomResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public Map<String, PomInfo> effectivePoms(List<ResolvedArtifactResult> resolvedP
.organization(Optional.ofNullable(d.getOrganization()))
.build())
.collect(Collectors.toList()))
.packaging(model.getPackaging())
.build());
}
return effectivePoms;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void useThePomOrganizationIfPresent() {
.homepage(URI.create("https://example.com"))
.organization(pomOrganization)
.addDevelopers(ImmutableDeveloperInfo.builder().name("Eli Graber").build())
.packaging("jar")
.build();

Assertions.assertEquals(
Expand All @@ -46,6 +47,7 @@ void useTheDevelopersNameIfPresent() {
ImmutablePomInfo.builder()
.homepage(URI.create("https://example.com"))
.addDevelopers(ImmutableDeveloperInfo.builder().name("Eli Graber").build())
.packaging("jar")
.build();

Assertions.assertEquals(
Expand All @@ -62,6 +64,7 @@ void useTheDevelopersNameAndEmailIfPresent() {
.name("Eli Graber")
.email("[email protected]")
.build())
.packaging("jar")
.build();

Assertions.assertEquals(
Expand All @@ -79,6 +82,7 @@ void useTheDevelopersOrganizationIfPresentAndNameIsNotPresent() {
.organization("ACME")
.email("[email protected]")
.build())
.packaging("jar")
.build();

Assertions.assertEquals(
Expand All @@ -91,6 +95,7 @@ void otherwiseUseOrganizationNoAssertion() {
ImmutablePomInfo.builder()
.homepage(URI.create("https://example.com"))
.addDevelopers(ImmutableDeveloperInfo.builder().email("[email protected]").build())
.packaging("jar")
.build();

Assertions.assertEquals(
Expand Down

0 comments on commit 5130e94

Please sign in to comment.