diff --git a/src/main/java/org/codehaus/mojo/license/AbstractLicenseReportRenderer.java b/src/main/java/org/codehaus/mojo/license/AbstractLicenseReportRenderer.java index d8c7211f5..0b9544d5c 100644 --- a/src/main/java/org/codehaus/mojo/license/AbstractLicenseReportRenderer.java +++ b/src/main/java/org/codehaus/mojo/license/AbstractLicenseReportRenderer.java @@ -32,6 +32,7 @@ import java.util.Collection; import java.util.Locale; +import java.util.Map; /** * Base class for report renderers. @@ -311,13 +312,16 @@ protected void safeItalic_() } } - protected void renderThirdPartyDetailTable( ThirdPartyDetails details ) + protected void renderThirdPartyDetailTable( ThirdPartyDetails details, + Map licenseLookup ) { - renderThirdPartyDetailTable( details, true, true, true ); + renderThirdPartyDetailTable( details, licenseLookup, true, true, true ); } - protected void renderThirdPartyDetailTable( ThirdPartyDetails details, boolean includeScope, - boolean includeClassifier, boolean includeType ) + protected void renderThirdPartyDetailTable( ThirdPartyDetails details, + Map licenseLookup, + boolean includeScope, boolean includeClassifier, + boolean includeType ) { final String cellWidth = "80%"; final String headerWidth = "20%"; @@ -377,7 +381,7 @@ else if ( details.hasThirdPartyLicenses() ) } String[] licenses = details.getLicenses(); - if ( details.hasPomLicenses() ) + if ( details.hasPomLicenses() || details.hasThirdPartyLicenses() ) { sink.tableRow(); sinkHeaderCellText( headerWidth, getText( "report.licenses" ) ); @@ -389,25 +393,17 @@ else if ( details.hasThirdPartyLicenses() ) { sink.lineBreak(); } - sink.text( licenses[i] ); - } - sink.tableCell_(); - sink.tableRow_(); - } - else if ( details.hasThirdPartyLicenses() ) - { - sink.tableRow(); - sinkHeaderCellText( headerWidth, getText( "report.licenses" ) ); - sink.tableCell( attrs ); - for ( int i = 0; i < licenses.length; i++ ) - { - if ( i > 0 ) + if ( licenseLookup.containsKey( licenses[i] ) ) { - sink.lineBreak(); + sink.link( licenseLookup.get( licenses[i] ) ); + sink.text( licenses[i] ); + sink.link_(); + } + else + { + sink.text( licenses[i] ); } - sink.text( licenses[i] ); - } sink.tableCell_(); sink.tableRow_(); diff --git a/src/main/java/org/codehaus/mojo/license/AbstractThirdPartyReportMojo.java b/src/main/java/org/codehaus/mojo/license/AbstractThirdPartyReportMojo.java index 61c529c90..89c3c95f6 100644 --- a/src/main/java/org/codehaus/mojo/license/AbstractThirdPartyReportMojo.java +++ b/src/main/java/org/codehaus/mojo/license/AbstractThirdPartyReportMojo.java @@ -26,6 +26,7 @@ import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.doxia.siterenderer.Renderer; +import org.apache.maven.model.License; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Component; @@ -57,13 +58,16 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Properties; import java.util.Set; import java.util.SortedMap; import java.util.SortedSet; import java.util.TreeSet; + import org.codehaus.mojo.license.api.ResolvedProjectDependencies; /** @@ -369,6 +373,16 @@ public abstract class AbstractThirdPartyReportMojo extends AbstractMavenReport private ArtifactFilters artifactFilters; + /** + * Maps license names to URLs. + * + * This is used by the
third-party-report
-goal in order to set custom license URLs + * + * @since 2.0.1 + */ + @Parameter( property = "license.urlmappings" ) + private Properties urlMappings; + // ---------------------------------------------------------------------- // Protected Abstract Methods // ---------------------------------------------------------------------- @@ -378,6 +392,8 @@ protected abstract Collection createThirdPartyDetails() InvalidDependencyVersionException, ArtifactNotFoundException, ArtifactResolutionException, DependenciesToolException, MojoExecutionException; + protected abstract Map createLicenseLookup(); + // ---------------------------------------------------------------------- // AbstractMavenReport Implementation // ---------------------------------------------------------------------- @@ -413,11 +429,13 @@ protected void executeReport( Locale locale ) overrideUrl, project.getBasedir() ); Collection details; + Map licenseLookup; try { init(); details = createThirdPartyDetails(); + licenseLookup = createLicenseLookup(); } catch ( IOException e ) { @@ -453,7 +471,7 @@ protected void executeReport( Locale locale ) } ThirdPartyReportRenderer renderer = - new ThirdPartyReportRenderer( getSink(), i18n, getOutputName(), locale, details ); + new ThirdPartyReportRenderer( getSink(), i18n, getOutputName(), locale, details, licenseLookup ); renderer.render(); } @@ -552,17 +570,7 @@ Collection createThirdPartyDetails( MavenProject project, boo DependenciesToolException, MojoExecutionException { - ResolvedProjectDependencies loadedDependencies; - if ( loadArtifacts ) - { - loadedDependencies = - new ResolvedProjectDependencies( project.getArtifacts(), project.getDependencyArtifacts() ); - } - else - { - loadedDependencies = new ResolvedProjectDependencies( getProject().getArtifacts(), - getProject().getDependencyArtifacts() ); - } + ResolvedProjectDependencies loadedDependencies = resolveProjectDependencies( project, loadArtifacts ); ThirdPartyHelper thirdPartyHelper = new DefaultThirdPartyHelper( project, encoding, verbose, @@ -628,6 +636,62 @@ else if ( !dependenciesWithNoLicense.contains( dependency ) ) return details; } + protected Map createLicenseLookup( MavenProject project, boolean loadArtifacts ) + { + Map licenseLookup = new HashMap<>(); + + //load explicit license url mappings + for ( String licenseName : urlMappings.stringPropertyNames() ) + { + licenseLookup.put( licenseName, urlMappings.getProperty( licenseName ) ); + } + + ResolvedProjectDependencies loadedDependencies = resolveProjectDependencies( project, loadArtifacts ); + + ThirdPartyHelper thirdPartyHelper = + new DefaultThirdPartyHelper( project, encoding, verbose, + dependenciesTool, thirdPartyTool, + project.getRemoteArtifactRepositories(), project.getRemoteProjectRepositories() ); + // load dependencies of the project + SortedMap projectDependencies = thirdPartyHelper.loadDependencies( this, + loadedDependencies ); + + //add the licenses to the lookup table + for ( MavenProject dependency : projectDependencies.values() ) + { + + for ( License license : dependency.getLicenses() ) + { + String licenseName = license.getName(); + + if ( !licenseLookup.containsKey( license.getName() ) ) + { + licenseLookup.put( licenseName, license.getUrl() ); + } + + } + + } + + return licenseLookup; + } + + private ResolvedProjectDependencies resolveProjectDependencies( MavenProject project, boolean loadArtifacts ) + { + ResolvedProjectDependencies loadedDependencies; + if ( loadArtifacts ) + { + loadedDependencies = + new ResolvedProjectDependencies( project.getArtifacts(), project.getDependencyArtifacts() ); + } + else + { + loadedDependencies = new ResolvedProjectDependencies( getProject().getArtifacts(), + getProject().getDependencyArtifacts() ); + } + return loadedDependencies; + } + /** {@inheritDoc} */ public String getArtifactFiltersUrl() { diff --git a/src/main/java/org/codehaus/mojo/license/AggregatorThirdPartyReportMojo.java b/src/main/java/org/codehaus/mojo/license/AggregatorThirdPartyReportMojo.java index dbbfa1223..2e12565c2 100644 --- a/src/main/java/org/codehaus/mojo/license/AggregatorThirdPartyReportMojo.java +++ b/src/main/java/org/codehaus/mojo/license/AggregatorThirdPartyReportMojo.java @@ -38,8 +38,10 @@ import java.io.IOException; import java.util.Collection; +import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; /** * Generates a report of all third-parties detected in the module. @@ -132,4 +134,21 @@ protected Collection createThirdPartyDetails() return details; } + /** + * {@inheritDoc} + */ + protected Map createLicenseLookup() + { + Map licenses = new HashMap<>(); + + for ( MavenProject reactorProject : reactorProjects ) + { + + Map thirdPartyLicenses = createLicenseLookup( reactorProject, true ); + licenses.putAll( thirdPartyLicenses ); + + } + + return licenses; + } } \ No newline at end of file diff --git a/src/main/java/org/codehaus/mojo/license/ThirdPartyReportMojo.java b/src/main/java/org/codehaus/mojo/license/ThirdPartyReportMojo.java index 81f2cd4da..b7f9cc168 100644 --- a/src/main/java/org/codehaus/mojo/license/ThirdPartyReportMojo.java +++ b/src/main/java/org/codehaus/mojo/license/ThirdPartyReportMojo.java @@ -34,6 +34,7 @@ import java.io.IOException; import java.util.Collection; +import java.util.Map; /** * Generates a report of all third-parties detected in the module. @@ -89,4 +90,13 @@ protected Collection createThirdPartyDetails() { return createThirdPartyDetails( getProject(), false ); } + + /** + * {@inheritDoc} + */ + @Override + protected Map createLicenseLookup() + { + return createLicenseLookup( getProject(), false ); + } } diff --git a/src/main/java/org/codehaus/mojo/license/ThirdPartyReportRenderer.java b/src/main/java/org/codehaus/mojo/license/ThirdPartyReportRenderer.java index a7927b1f0..d5794eb16 100644 --- a/src/main/java/org/codehaus/mojo/license/ThirdPartyReportRenderer.java +++ b/src/main/java/org/codehaus/mojo/license/ThirdPartyReportRenderer.java @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Locale; +import java.util.Map; /** * Generates a report of third parties of the project. @@ -41,12 +42,14 @@ public class ThirdPartyReportRenderer extends AbstractLicenseReportRenderer { private final Collection details; + private final Map licenseLookup; public ThirdPartyReportRenderer( Sink sink, I18N i18n, String outputName, Locale locale, - Collection details ) + Collection details, Map licenseLookup ) { super( sink, outputName, i18n, locale ); this.details = details; + this.licenseLookup = licenseLookup; } protected Collection getThirdPartiesPomLicense() @@ -259,7 +262,7 @@ private void renderThirdPartyDetail( ThirdPartyDetails detail ) sink.sectionTitle2(); sink.text( getGAV( detail ) ); sink.sectionTitle2_(); - renderThirdPartyDetailTable( detail ); + renderThirdPartyDetailTable( detail, licenseLookup ); sink.link( "#" + getText( "report.overview.title" ) ); sink.text( getText( "report.back.to.top.page" ) );