Skip to content

Commit

Permalink
Add links to license in HTML report
Browse files Browse the repository at this point in the history
fixes mojohaus#212
This commit adds links to license URLs in the license detail table if such a URL is present.
  • Loading branch information
danthe1st committed Feb 24, 2021
1 parent 01fd9a1 commit a5c756b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 35 deletions.
Expand Up @@ -26,12 +26,14 @@
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
import org.apache.maven.doxia.sink.SinkEventAttributes;
import org.apache.maven.model.License;
import org.apache.maven.reporting.AbstractMavenReportRenderer;
import org.codehaus.mojo.license.api.ThirdPartyDetails;
import org.codehaus.plexus.i18n.I18N;

import java.util.Collection;
import java.util.Locale;
import java.util.Map;

/**
* Base class for report renderers.
Expand Down Expand Up @@ -311,13 +313,16 @@ protected void safeItalic_()
}
}

protected void renderThirdPartyDetailTable( ThirdPartyDetails details )
protected void renderThirdPartyDetailTable( ThirdPartyDetails details,
Map<String, License> 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<String, License> licenseLookup,
boolean includeScope, boolean includeClassifier,
boolean includeType )
{
final String cellWidth = "80%";
final String headerWidth = "20%";
Expand Down Expand Up @@ -377,7 +382,7 @@ else if ( details.hasThirdPartyLicenses() )
}
String[] licenses = details.getLicenses();

if ( details.hasPomLicenses() )
if ( details.hasPomLicenses() || details.hasThirdPartyLicenses() )
{
sink.tableRow();
sinkHeaderCellText( headerWidth, getText( "report.licenses" ) );
Expand All @@ -389,25 +394,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] ).getUrl() );
sink.text( licenses[i] );
sink.link_();
}
else
{
sink.text( licenses[i] );
}
sink.text( licenses[i] );

}
sink.tableCell_();
sink.tableRow_();
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -57,13 +58,15 @@
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.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeSet;

import org.codehaus.mojo.license.api.ResolvedProjectDependencies;

/**
Expand Down Expand Up @@ -378,6 +381,8 @@ protected abstract Collection<ThirdPartyDetails> createThirdPartyDetails()
InvalidDependencyVersionException, ArtifactNotFoundException, ArtifactResolutionException,
DependenciesToolException, MojoExecutionException;

protected abstract Map<String, License> createLicenseLookup();

// ----------------------------------------------------------------------
// AbstractMavenReport Implementation
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -413,11 +418,13 @@ protected void executeReport( Locale locale )
overrideUrl, project.getBasedir() );

Collection<ThirdPartyDetails> details;
Map<String, License> licenseLookup;

try
{
init();
details = createThirdPartyDetails();
licenseLookup = createLicenseLookup();
}
catch ( IOException e )
{
Expand Down Expand Up @@ -453,7 +460,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();

}
Expand Down Expand Up @@ -552,17 +559,7 @@ Collection<ThirdPartyDetails> 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,
Expand Down Expand Up @@ -628,6 +625,57 @@ else if ( !dependenciesWithNoLicense.contains( dependency ) )
return details;
}

protected Map<String, License> createLicenseLookup( MavenProject project, boolean loadArtifacts )
{
Map<String, License> licenseLookup = new HashMap<>();

ResolvedProjectDependencies loadedDependencies = resolveProjectDependencies( project, loadArtifacts );

ThirdPartyHelper thirdPartyHelper =
new DefaultThirdPartyHelper( project, encoding, verbose,
dependenciesTool, thirdPartyTool,
project.getRemoteArtifactRepositories(), project.getRemoteProjectRepositories() );
// load dependencies of the project
SortedMap<String, MavenProject> 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 );
}

}

}

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()
{
Expand Down
Expand Up @@ -24,6 +24,7 @@

import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.model.License;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
Expand All @@ -38,8 +39,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.
Expand Down Expand Up @@ -132,4 +135,21 @@ protected Collection<ThirdPartyDetails> createThirdPartyDetails()
return details;
}

/**
* {@inheritDoc}
*/
protected Map<String, License> createLicenseLookup()
{
Map<String, License> licenses = new HashMap<>();

for ( MavenProject reactorProject : reactorProjects )
{

Map<String, License> thirdPartyLicenses = createLicenseLookup( reactorProject, true );
licenses.putAll( thirdPartyLicenses );

}

return licenses;
}
}
11 changes: 11 additions & 0 deletions src/main/java/org/codehaus/mojo/license/ThirdPartyReportMojo.java
Expand Up @@ -22,6 +22,7 @@
* #L%
*/

import org.apache.maven.model.License;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
Expand All @@ -34,6 +35,7 @@

import java.io.IOException;
import java.util.Collection;
import java.util.Map;

/**
* Generates a report of all third-parties detected in the module.
Expand Down Expand Up @@ -89,4 +91,13 @@ protected Collection<ThirdPartyDetails> createThirdPartyDetails()
{
return createThirdPartyDetails( getProject(), false );
}

/**
* {@inheritDoc}
*/
@Override
protected Map<String, License> createLicenseLookup()
{
return createLicenseLookup( getProject(), false );
}
}
Expand Up @@ -24,12 +24,14 @@

import org.apache.commons.collections.CollectionUtils;
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.model.License;
import org.codehaus.mojo.license.api.ThirdPartyDetails;
import org.codehaus.plexus.i18n.I18N;

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.
Expand All @@ -41,12 +43,14 @@ public class ThirdPartyReportRenderer
extends AbstractLicenseReportRenderer
{
private final Collection<ThirdPartyDetails> details;
private final Map<String, License> licenseLookup;

public ThirdPartyReportRenderer( Sink sink, I18N i18n, String outputName, Locale locale,
Collection<ThirdPartyDetails> details )
Collection<ThirdPartyDetails> details, Map<String, License> licenseLookup )
{
super( sink, outputName, i18n, locale );
this.details = details;
this.licenseLookup = licenseLookup;
}

protected Collection<ThirdPartyDetails> getThirdPartiesPomLicense()
Expand Down Expand Up @@ -259,7 +263,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" ) );
Expand Down

0 comments on commit a5c756b

Please sign in to comment.