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

Add links to license in HTML report #401

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -32,6 +32,7 @@

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

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

protected void renderThirdPartyDetailTable( ThirdPartyDetails details )
protected void renderThirdPartyDetailTable( ThirdPartyDetails details,
Map<String, String> 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, String> licenseLookup,
boolean includeScope, boolean includeClassifier,
boolean includeType )
{
final String cellWidth = "80%";
final String headerWidth = "20%";
Expand Down Expand Up @@ -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" ) );
Expand All @@ -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_();
Expand Down
Original file line number Diff line number Diff line change
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,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;

/**
Expand Down Expand Up @@ -369,6 +373,16 @@ public abstract class AbstractThirdPartyReportMojo extends AbstractMavenReport

private ArtifactFilters artifactFilters;

/**
* Maps license names to URLs.
*
* This is used by the <pre>third-party-report</pre>-goal in order to set custom license URLs
*
* @since 2.0.1
*/
@Parameter( property = "license.urlmappings" )
private Properties urlMappings;

// ----------------------------------------------------------------------
// Protected Abstract Methods
// ----------------------------------------------------------------------
Expand All @@ -378,6 +392,8 @@ protected abstract Collection<ThirdPartyDetails> createThirdPartyDetails()
InvalidDependencyVersionException, ArtifactNotFoundException, ArtifactResolutionException,
DependenciesToolException, MojoExecutionException;

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

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

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

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

}
Expand Down Expand Up @@ -552,17 +570,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 +636,62 @@ else if ( !dependenciesWithNoLicense.contains( dependency ) )
return details;
}

protected Map<String, String> createLicenseLookup( MavenProject project, boolean loadArtifacts )
{
Map<String, String> 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<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.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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -132,4 +134,21 @@ protected Collection<ThirdPartyDetails> createThirdPartyDetails()
return details;
}

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

for ( MavenProject reactorProject : reactorProjects )
{

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

}

return licenses;
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/codehaus/mojo/license/ThirdPartyReportMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -89,4 +90,13 @@ protected Collection<ThirdPartyDetails> createThirdPartyDetails()
{
return createThirdPartyDetails( getProject(), false );
}

/**
* {@inheritDoc}
*/
@Override
protected Map<String, String> createLicenseLookup()
{
return createLicenseLookup( getProject(), false );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -41,12 +42,14 @@ public class ThirdPartyReportRenderer
extends AbstractLicenseReportRenderer
{
private final Collection<ThirdPartyDetails> details;
private final Map<String, String> licenseLookup;

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

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