Skip to content

Commit

Permalink
Move isAggregatorBuild and related fields in AddThirdPartyMojo
Browse files Browse the repository at this point in the history
  • Loading branch information
srdo committed Jan 3, 2019
1 parent ae3a443 commit 09d470b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.SortedMap;
import java.util.SortedSet;
import org.apache.maven.model.Dependency;
import org.codehaus.mojo.license.api.DependenciesToolException;
import org.codehaus.mojo.license.api.ResolvedProjectDependencies;

/*
Expand Down Expand Up @@ -561,34 +562,6 @@ public abstract class AbstractAddThirdPartyMojo
* @since 1.4
*/
private Map<String, String> globalKnownLicenses;

/**
* Whether this is an aggregate build, or a single-project goal. This setting determines which dependency artifacts
* will be examined by the plugin. AddThirdParty needs to load dependencies only for the single project it is run
* for, while AggregateAddThirdParty needs to load dependencies for the parent project, as well as all child
* projects in the reactor.
*/
boolean isAggregatorBuild = false;

/**
* Map from reactor project G/A to direct dependencies. When loading dependencies, the aggregator goal replaces all
* reactor projects with their direct dependencies, to avoid trying to load artifacts for projects that haven't been
* built/published yet. This field is used to keep track of which dependencies are also projects in the reactor.
*/
Map<String, List<Dependency>> reactorProjectDependencies;

/**
* Copies of the project's dependency sets. AddThirdParty needs to load dependencies only for the single project it
* is run for, while AggregateAddThirdParty needs to load dependencies for the parent project, as well as all child
* projects in the reactor.
*
* The aggregator goal replaces all reactor projects with their direct dependencies, to avoid trying to load
* artifacts for projects that haven't been built/published yet. This is necessary in cases where one child project
* A in a reactor depends on another project B in the same reactor. Since B is not necessarily built/published, the
* plugin needs to replace B with its dependencies when processing A. This field stores that modified view of the
* project's dependencies.
*/
ResolvedProjectDependencies dependencyArtifacts;

// ----------------------------------------------------------------------
// Abstract Methods
Expand All @@ -598,21 +571,23 @@ public abstract class AbstractAddThirdPartyMojo
* Loads the dependencies of the project (as {@link MavenProject}, indexed by their gav.
*
* @return the map of dependencies of the maven project indexed by their gav.
* @throws DependenciesToolException if the dependencies could not be loaded
*/
protected abstract SortedMap<String, MavenProject> loadDependencies();
protected abstract SortedMap<String, MavenProject> loadDependencies() throws DependenciesToolException;

/**
* Creates the unsafe mapping (says dependencies with no license given by their pom).
* <p>
* Can come from loaded missing file or from dependencies with no license at all.
*
* @return the map of usafe mapping indexed by their gav.
* @throws ProjectBuildingException if could not create maven porject for some dependencies
* @throws ProjectBuildingException if could not create maven project for some dependencies
* @throws IOException if could not load missing file
* @throws ThirdPartyToolException for third party tool error
* @throws DependenciesToolException if the dependencies could not be loaded
*/
protected abstract SortedProperties createUnsafeMapping()
throws ProjectBuildingException, IOException, ThirdPartyToolException, MojoExecutionException;
throws ProjectBuildingException, IOException, ThirdPartyToolException, MojoExecutionException, DependenciesToolException;

// ----------------------------------------------------------------------
// AbstractLicenseMojo Implementaton
Expand Down Expand Up @@ -664,12 +639,6 @@ protected void init()
// not generating bundled file
doGenerateBundle = false;
}

if ( isAggregatorBuild ) {
dependencyArtifacts = dependenciesTool.loadProjectArtifacts( localRepository, remoteRepositories, project, reactorProjectDependencies );
} else {
dependencyArtifacts = new ResolvedProjectDependencies( project.getArtifacts(), project.getDependencyArtifacts() );
}

projectDependencies = loadDependencies();

Expand Down Expand Up @@ -701,7 +670,7 @@ protected void init()
}

void consolidate() throws IOException, ArtifactNotFoundException, ArtifactResolutionException, MojoFailureException,
ProjectBuildingException, ThirdPartyToolException, MojoExecutionException
ProjectBuildingException, ThirdPartyToolException, MojoExecutionException, DependenciesToolException
{

unsafeDependencies = getHelper().getProjectsWithNoLicense( licenseMap );
Expand Down
57 changes: 53 additions & 4 deletions src/main/java/org/codehaus/mojo/license/AddThirdPartyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingException;
import org.codehaus.mojo.license.api.DependenciesToolException;
import org.codehaus.mojo.license.api.MavenProjectDependenciesConfigurator;
import org.codehaus.mojo.license.api.ResolvedProjectDependencies;
import org.codehaus.mojo.license.api.ThirdPartyToolException;
import org.codehaus.mojo.license.model.LicenseMap;
import org.codehaus.mojo.license.utils.FileUtil;
Expand Down Expand Up @@ -86,6 +88,34 @@ public class AddThirdPartyMojo extends AbstractAddThirdPartyMojo implements Mave
* Internal flag to know if missing file must be generated.
*/
private boolean doGenerateMissing;

/**
* Whether this is an aggregate build, or a single-project goal. This setting determines which dependency artifacts
* will be examined by the plugin. AddThirdParty needs to load dependencies only for the single project it is run
* for, while AggregateAddThirdParty needs to load dependencies for the parent project, as well as all child
* projects in the reactor.
*/
private boolean isAggregatorBuild = false;

/**
* Map from reactor project G/A to direct dependencies. When loading dependencies, the aggregator goal replaces all
* reactor projects with their direct dependencies, to avoid trying to load artifacts for projects that haven't been
* built/published yet. This field is used to keep track of which dependencies are also projects in the reactor.
*/
private Map<String, List<Dependency>> reactorProjectDependencies;

/**
* Copies of the project's dependency sets. AddThirdParty needs to load dependencies only for the single project it
* is run for, while AggregateAddThirdParty needs to load dependencies for the parent project, as well as all child
* projects in the reactor.
*
* The aggregator goal replaces all reactor projects with their direct dependencies, to avoid trying to load
* artifacts for projects that haven't been built/published yet. This is necessary in cases where one child project
* A in a reactor depends on another project B in the same reactor. Since B is not necessarily built/published, the
* plugin needs to replace B with its dependencies when processing A. This field stores that modified view of the
* project's dependencies.
*/
private ResolvedProjectDependencies dependencyArtifacts;

// ----------------------------------------------------------------------
// AbstractLicenseMojo Implementaton
Expand Down Expand Up @@ -189,25 +219,44 @@ protected void doAction()
* {@inheritDoc}
*/
@Override
protected SortedMap<String, MavenProject> loadDependencies()
protected SortedMap<String, MavenProject> loadDependencies() throws DependenciesToolException
{
return getHelper().loadDependencies( this, dependencyArtifacts );
return getHelper().loadDependencies( this, resolveDependencyArtifacts() );
}

/**
* Resolves the transitive and direct dependency sets for this project.
*
* @return The set of all dependencies, and the set of only direct dependency artifacts.
* @throws org.codehaus.mojo.license.api.DependenciesToolException if the dependencies could not be resolved
*/
protected ResolvedProjectDependencies resolveDependencyArtifacts() throws DependenciesToolException
{
if ( dependencyArtifacts != null ) {
return dependencyArtifacts;
}
if ( isAggregatorBuild ) {
dependencyArtifacts = dependenciesTool.loadProjectArtifacts( localRepository, remoteRepositories, project, reactorProjectDependencies );
} else {
dependencyArtifacts = new ResolvedProjectDependencies( project.getArtifacts(), project.getDependencyArtifacts() );
}
return dependencyArtifacts;
}

/**
* {@inheritDoc}
*/
@Override
protected SortedProperties createUnsafeMapping()
throws ProjectBuildingException, IOException, ThirdPartyToolException, MojoExecutionException
throws ProjectBuildingException, IOException, ThirdPartyToolException, MojoExecutionException, DependenciesToolException
{

SortedSet<MavenProject> unsafeDependencies = getUnsafeDependencies();

SortedProperties unsafeMappings =
getHelper().createUnsafeMapping( getLicenseMap(), getMissingFile(), missingFileUrl,
useRepositoryMissingFiles, unsafeDependencies,
getProjectDependencies(), dependencyArtifacts.getAllDependencies() );
getProjectDependencies(), resolveDependencyArtifacts().getAllDependencies() );
if ( isVerbose() )
{
getLog().info( "found " + unsafeMappings.size() + " unsafe mappings" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.TreeMap;
import org.apache.commons.collections.CollectionUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
Expand Down Expand Up @@ -154,11 +155,6 @@ protected void init() throws Exception {
missingLicensesFileArtifact = aggregateMissingLicensesFileArtifact;
}

isAggregatorBuild = true;
reactorProjectDependencies = new TreeMap<>();
for (MavenProject reactorProject : this.reactorProjects) {
reactorProjectDependencies.put( String.format( "%s:%s", reactorProject.getGroupId(), reactorProject.getArtifactId() ), reactorProject.getDependencies() );
}
super.init();
}

Expand Down Expand Up @@ -201,6 +197,11 @@ protected void doAction()

String addThirdPartyRoleHint = groupId + ":" + artifactId + ":" + version + ":" + "add-third-party";

Map<String, List<Dependency>> reactorProjectDependencies = new TreeMap<>();
for (MavenProject reactorProject : this.reactorProjects) {
reactorProjectDependencies.put( String.format( "%s:%s", reactorProject.getGroupId(), reactorProject.getArtifactId() ), reactorProject.getDependencies() );
}

for (MavenProject reactorProject : reactorProjects) {
if (getProject().equals(reactorProject)) {
// do not process pom
Expand Down

0 comments on commit 09d470b

Please sign in to comment.