Skip to content

Commit

Permalink
Inject project dependencies
Browse files Browse the repository at this point in the history
  - Helps to not use Project so there isn't an accidental issue with configuration cache
  • Loading branch information
eygraber committed Nov 8, 2023
1 parent 7a35547 commit 1f159d6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/main/java/org/spdx/sbom/gradle/SpdxSbomPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ private void createTaskForTarget(
.putAll(
pomsConfigProvider.map(
pomsConfig ->
PomResolver.newPomResolver(project).effectivePoms(pomsConfig)));
PomResolver.newPomResolver(
project.getDependencies(),
project.getConfigurations(),
project.getLogger())
.effectivePoms(pomsConfig)));

t.getRootComponents().add(rootComponent);
}
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/org/spdx/sbom/gradle/maven/GradleMavenResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@
import org.apache.maven.model.building.FileModelSource;
import org.apache.maven.model.building.ModelSource2;
import org.apache.maven.model.resolution.ModelResolver;
import org.gradle.api.Project;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.dsl.DependencyHandler;

public class GradleMavenResolver implements ModelResolver {
private final Project project;
private final DependencyHandler dependencies;
private final ConfigurationContainer configurations;

public GradleMavenResolver(Project project) {
this.project = project;
public GradleMavenResolver(
DependencyHandler dependencies, ConfigurationContainer configurations) {
this.dependencies = dependencies;
this.configurations = configurations;
}

@Override
public ModelSource2 resolveModel(String groupId, String artifactId, String version) {
var dep = groupId + ":" + artifactId + ":" + version + "@pom";
var dependency = project.getDependencies().create(dep);
var config = project.getConfigurations().detachedConfiguration(dependency);
var dependency = dependencies.create(dep);
var config = configurations.detachedConfiguration(dependency);

var pomXml = config.getSingleFile();
return new FileModelSource(pomXml);
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/spdx/sbom/gradle/maven/PomResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
import org.apache.maven.model.building.ModelBuildingException;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradle.api.logging.Logger;

/** This needs to be run *before* while configuring the task, so use it in the Plugin. */
Expand All @@ -40,9 +41,12 @@ public class PomResolver {
private final GradleMavenResolver gradleMavenResolver;
private final Logger logger;

public static PomResolver newPomResolver(Project project) {
public static PomResolver newPomResolver(
DependencyHandler dependencies, ConfigurationContainer configurations, Logger logger) {
return new PomResolver(
new GradleMavenResolver(project), new DefaultModelBuilderFactory(), project.getLogger());
new GradleMavenResolver(dependencies, configurations),
new DefaultModelBuilderFactory(),
logger);
}

PomResolver(
Expand Down

0 comments on commit 1f159d6

Please sign in to comment.