Skip to content

Commit

Permalink
fix: Configure the project for the ProjectBuildingRequest being used.
Browse files Browse the repository at this point in the history
The `ProjectBuildingRequest` requires a project to be configured. This project is not available by default. The `ProjectBuildingRequest` is mutable, so I did not use the exiting class variable. Instead, I suggest not storing the request but create one as needed. For that purpose, the Maven session is now kept around.

When requiring a request with a project, the existing copy-constructor is new used and a project is configured as needed.

This change also fixes the tests (expecting 0 instead of 1 dependencies). This change has little effect though, as the `MavenProjectLicensesIT` does not actually run.

This fixes #377.
  • Loading branch information
michael-simons committed Feb 13, 2023
1 parent fe350ff commit ac9a205
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.mycila.maven.plugin.license.dependencies;

import org.apache.maven.Maven;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter;
Expand Down Expand Up @@ -49,10 +50,10 @@
*/
public class MavenProjectLicenses implements LicenseMap, LicenseMessage {

private MavenSession session;
private Set<MavenProject> projects;
private DependencyGraphBuilder graph;
private ProjectBuilder projectBuilder;
private ProjectBuildingRequest buildingRequest;
private ArtifactFilter filter;
private Log log;

Expand All @@ -62,11 +63,11 @@ public class MavenProjectLicenses implements LicenseMap, LicenseMessage {
* @param projectBuilder the maven {@link ProjectBuilder} implementation
* @param log the log to sync to
*/
public MavenProjectLicenses(final Set<MavenProject> projects, final DependencyGraphBuilder graph,
final ProjectBuilder projectBuilder, final ProjectBuildingRequest buildingRequest,
MavenProjectLicenses(final MavenSession session, final Set<MavenProject> projects, final DependencyGraphBuilder graph,
final ProjectBuilder projectBuilder,
final ArtifactFilter filter, final Log log) {
this.setSession(session);
this.setProjects(projects);
this.setBuildingRequest(buildingRequest);
this.setGraph(graph);
this.setFilter(filter);
this.setProjectBuilder(projectBuilder);
Expand All @@ -82,18 +83,7 @@ public MavenProjectLicenses(final Set<MavenProject> projects, final DependencyGr
*/
public MavenProjectLicenses(final MavenSession session, MavenProject project, final DependencyGraphBuilder graph,
final ProjectBuilder projectBuilder, final List<String> scopes, final Log log) {
this(Collections.singleton(project), graph, projectBuilder, getBuildingRequestWithDefaults(session),
new CumulativeScopeArtifactFilter(scopes), log);
}

private static ProjectBuildingRequest getBuildingRequestWithDefaults(final MavenSession session) {
ProjectBuildingRequest request;
if (session == null) {
request = new DefaultProjectBuildingRequest();
} else {
request = session.getProjectBuildingRequest();
}
return request;
this(session, Collections.singleton(project), graph, projectBuilder, new CumulativeScopeArtifactFilter(scopes), log);
}

/**
Expand Down Expand Up @@ -150,6 +140,8 @@ private Set<Artifact> getDependencies() {
getLog().debug(String.format("Building dependency graphs for %d projects", getProjects().size()));
getProjects().parallelStream().forEach(project -> {
try {
DefaultProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(getBuildingRequest());
buildingRequest.setProject(project);
dependencies.addAll(getGraph().buildDependencyGraph(buildingRequest, getFilter()).getChildren());
} catch (DependencyGraphBuilderException ex) {
getLog().warn(
Expand All @@ -174,6 +166,10 @@ protected Set<MavenProject> getProjects() {
return projects;
}

private void setSession(MavenSession session) {
this.session = session;
}

protected void setProjects(final Set<MavenProject> projects) {
this.projects = Optional.ofNullable(projects).orElse(new HashSet<>());
}
Expand Down Expand Up @@ -211,10 +207,7 @@ private void setLog(Log log) {
}

private ProjectBuildingRequest getBuildingRequest() {
return buildingRequest;
}

protected void setBuildingRequest(final ProjectBuildingRequest buildingRequest) {
this.buildingRequest = Optional.ofNullable(buildingRequest).orElse(new DefaultProjectBuildingRequest());
// There's an odd comment on the below used method, pretty sure it is not as stable as one likes it to be
return session.getProjectBuildingRequest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void test_null() throws IOException {
final String description = "A project with enforcement enabled but nothing in scope should find zero dependencies";
syncTarget("null");

Assertions.assertTrue(hasLogLine(LicenseMessage.INFO_DEPS_DISCOVERED + ": 1"), description);
Assertions.assertTrue(hasLogLine(LicenseMessage.INFO_DEPS_DISCOVERED + ": 0"), description);
}

@Test
Expand Down

0 comments on commit ac9a205

Please sign in to comment.