diff --git a/src/main/java/org/codehaus/mojo/license/AbstractAddThirdPartyMojo.java b/src/main/java/org/codehaus/mojo/license/AbstractAddThirdPartyMojo.java index 2de85d404..676083dc3 100644 --- a/src/main/java/org/codehaus/mojo/license/AbstractAddThirdPartyMojo.java +++ b/src/main/java/org/codehaus/mojo/license/AbstractAddThirdPartyMojo.java @@ -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; /* @@ -561,34 +562,6 @@ public abstract class AbstractAddThirdPartyMojo * @since 1.4 */ private Map 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> 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 @@ -598,8 +571,9 @@ 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 loadDependencies(); + protected abstract SortedMap loadDependencies() throws DependenciesToolException; /** * Creates the unsafe mapping (says dependencies with no license given by their pom). @@ -607,12 +581,13 @@ public abstract class AbstractAddThirdPartyMojo * 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 @@ -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(); @@ -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 ); diff --git a/src/main/java/org/codehaus/mojo/license/AddThirdPartyMojo.java b/src/main/java/org/codehaus/mojo/license/AddThirdPartyMojo.java index 8f29b1cee..bd718fbcf 100644 --- a/src/main/java/org/codehaus/mojo/license/AddThirdPartyMojo.java +++ b/src/main/java/org/codehaus/mojo/license/AddThirdPartyMojo.java @@ -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; @@ -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> 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 @@ -189,9 +219,28 @@ protected void doAction() * {@inheritDoc} */ @Override - protected SortedMap loadDependencies() + protected SortedMap 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; } /** @@ -199,7 +248,7 @@ protected SortedMap loadDependencies() */ @Override protected SortedProperties createUnsafeMapping() - throws ProjectBuildingException, IOException, ThirdPartyToolException, MojoExecutionException + throws ProjectBuildingException, IOException, ThirdPartyToolException, MojoExecutionException, DependenciesToolException { SortedSet unsafeDependencies = getUnsafeDependencies(); @@ -207,7 +256,7 @@ protected SortedProperties createUnsafeMapping() SortedProperties unsafeMappings = getHelper().createUnsafeMapping( getLicenseMap(), getMissingFile(), missingFileUrl, useRepositoryMissingFiles, unsafeDependencies, - getProjectDependencies(), dependencyArtifacts.getAllDependencies() ); + getProjectDependencies(), resolveDependencyArtifacts().getAllDependencies() ); if ( isVerbose() ) { getLog().info( "found " + unsafeMappings.size() + " unsafe mappings" ); diff --git a/src/main/java/org/codehaus/mojo/license/AggregatorAddThirdPartyMojo.java b/src/main/java/org/codehaus/mojo/license/AggregatorAddThirdPartyMojo.java index 4432d338a..ce3f3c1bb 100644 --- a/src/main/java/org/codehaus/mojo/license/AggregatorAddThirdPartyMojo.java +++ b/src/main/java/org/codehaus/mojo/license/AggregatorAddThirdPartyMojo.java @@ -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; @@ -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(); } @@ -201,6 +197,11 @@ protected void doAction() String addThirdPartyRoleHint = groupId + ":" + artifactId + ":" + version + ":" + "add-third-party"; + Map> 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