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

fix: Configure the project for the ProjectBuildingRequest being used. #473

Merged
merged 1 commit into from
Feb 13, 2023
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
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