Skip to content

Commit

Permalink
[MPIR-407] Provide a way to map license names
Browse files Browse the repository at this point in the history
This closes #38
  • Loading branch information
gnodet authored and michael-o committed Jul 6, 2022
1 parent 9430070 commit eeb81c8
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 9 deletions.
18 changes: 18 additions & 0 deletions src/it/MPIR-407/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:dependencies
70 changes: 70 additions & 0 deletions src/it/MPIR-407/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugins.project-info-reports.its</groupId>
<artifactId>MPIR-407</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<url>http://maven.apache.org/plugins/it/${project.artifactId}</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>@mavenVersion@</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<licenseMappings>
<licenseMapping>
<froms>
<from>The Apache Software License, Version 2.0</from>
<from>Apache Public License 2.0</from>
</froms>
<to>Apache License, Version 2.0</to>
</licenseMapping>
</licenseMappings>
</configuration>
</plugin>
</plugins>
</build>

</project>
23 changes: 23 additions & 0 deletions src/it/MPIR-407/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

dependencies = new File( basedir, 'target/site/dependencies.html' ).text;

assert !( dependencies.contains( 'The Apache Software License, Version 2.0' ) );
assert !( dependencies.contains( 'Apache Public License 2.0' ) );
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ public abstract class AbstractProjectInfoReport
@Parameter( defaultValue = "true" )
protected boolean skipEmptyReport;

/**
* A mapping of license names to group licenses referred to with different names together
*
* @since 3.3.1
*/
@Parameter
private List<LicenseMapping> licenseMappings;

// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -259,6 +267,22 @@ public String getCategoryName()
// Protected methods
// ----------------------------------------------------------------------

protected Map<String, String> getLicenseMappings()
{
Map<String, String> map = new HashMap<>();
if ( licenseMappings != null )
{
for ( LicenseMapping mapping : licenseMappings )
{
for ( String from : mapping.getFroms() )
{
map.put( from, mapping.getTo() );
}
}
}
return map;
}

/**
* @param coll The collection to be checked.
* @return true if coll is empty false otherwise.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void executeReport( Locale locale )
DependenciesRenderer r =
new DependenciesRenderer( getSink(), locale, getI18N( locale ), getLog(), dependencies,
dependencyNode, config, repoUtils, repositorySystem, projectBuilder,
buildingRequest );
buildingRequest, getLicenseMappings() );
r.render();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void executeReport( Locale locale )
DependencyManagementRenderer r =
new DependencyManagementRenderer( getSink(), locale, getI18N( locale ), getLog(),
getManagementDependencies(), artifactMetadataSource, repositorySystem,
projectBuilder, buildingRequest, repoUtils );
projectBuilder, buildingRequest, repoUtils, getLicenseMappings() );
r.render();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.apache.maven.report.projectinfo;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.util.List;

/**
* License mapping
*/
public class LicenseMapping
{

private List<String> froms;
private String to;

public List<String> getFroms()
{
return froms;
}

public void setFroms( List<String> froms )
{
this.froms = froms;
}

public String getTo()
{
return to;
}

public void setTo( String to )
{
this.to = to;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.report.projectinfo.AbstractProjectInfoRenderer;
import org.apache.maven.report.projectinfo.LicenseMapping;
import org.apache.maven.report.projectinfo.ProjectInfoReportUtils;
import org.apache.maven.report.projectinfo.dependencies.Dependencies;
import org.apache.maven.report.projectinfo.dependencies.DependenciesReportConfiguration;
Expand Down Expand Up @@ -132,6 +133,8 @@ public Object put( String key, Object value )

private final ProjectBuildingRequest buildingRequest;

private final Map<String, String> licenseMappings;

static
{
Set<String> jarSubtype = new HashSet<>();
Expand Down Expand Up @@ -161,12 +164,13 @@ public Object put( String key, Object value )
* @param repositorySystem {@link RepositorySystem}
* @param projectBuilder {@link ProjectBuilder}
* @param buildingRequest {@link ProjectBuildingRequest}
* @param licenseMappings {@link LicenseMapping}
*/
public DependenciesRenderer( Sink sink, Locale locale, I18N i18n, Log log,
Dependencies dependencies, DependencyNode dependencyTreeNode,
DependenciesReportConfiguration config, RepositoryUtils repoUtils,
RepositorySystem repositorySystem, ProjectBuilder projectBuilder,
ProjectBuildingRequest buildingRequest )
ProjectBuildingRequest buildingRequest, Map<String, String> licenseMappings )
{
super( sink, i18n, locale );

Expand All @@ -178,6 +182,7 @@ public DependenciesRenderer( Sink sink, Locale locale, I18N i18n, Log log,
this.repositorySystem = repositorySystem;
this.projectBuilder = projectBuilder;
this.buildingRequest = buildingRequest;
this.licenseMappings = licenseMappings;

// Using the right set of symbols depending of the locale
DEFAULT_DECIMAL_FORMAT.setDecimalFormatSymbols( new DecimalFormatSymbols( locale ) );
Expand Down Expand Up @@ -800,7 +805,12 @@ private void renderArtifactRow( Artifact artifact, boolean withClassifier, boole
List<License> licenses = artifactProject.getLicenses();
for ( License license : licenses )
{
sb.append( ProjectInfoReportUtils.getArtifactIdCell( license.getName(), license.getUrl() ) );
String name = license.getName();
if ( licenseMappings != null && licenseMappings.containsKey( name ) )
{
name = licenseMappings.get( name );
}
sb.append( ProjectInfoReportUtils.getArtifactIdCell( name, license.getUrl() ) );
}
}
catch ( ProjectBuildingException e )
Expand Down Expand Up @@ -956,6 +966,10 @@ private void printDescriptionsAndURLs( DependencyNode node, String uid )
License license = it.next();

String licenseName = license.getName();
if ( licenseMappings != null && licenseMappings.containsKey( licenseName ) )
{
licenseName = licenseMappings.get( licenseName );
}
if ( StringUtils.isEmpty( licenseName ) )
{
licenseName = getI18nString( "unnamed" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.report.projectinfo.AbstractProjectInfoRenderer;
import org.apache.maven.report.projectinfo.LicenseMapping;
import org.apache.maven.report.projectinfo.ProjectInfoReportUtils;
import org.apache.maven.report.projectinfo.dependencies.ManagementDependencies;
import org.apache.maven.report.projectinfo.dependencies.RepositoryUtils;
Expand Down Expand Up @@ -69,6 +70,8 @@ public class DependencyManagementRenderer

private final RepositoryUtils repoUtils;

private final Map<String, String> licenseMappings;

/**
* Default constructor
*
Expand All @@ -82,12 +85,14 @@ public class DependencyManagementRenderer
* @param projectBuilder {@link ProjectBuilder}
* @param buildingRequest {@link ProjectBuildingRequest}
* @param repoUtils {@link RepositoryUtils}
* @param licenseMappings {@link LicenseMapping}
*/
public DependencyManagementRenderer( Sink sink, Locale locale, I18N i18n, Log log,
ManagementDependencies dependencies,
ArtifactMetadataSource artifactMetadataSource,
RepositorySystem repositorySystem, ProjectBuilder projectBuilder,
ProjectBuildingRequest buildingRequest, RepositoryUtils repoUtils )
ProjectBuildingRequest buildingRequest, RepositoryUtils repoUtils,
Map<String, String> licenseMappings )
{
super( sink, i18n, locale );

Expand All @@ -98,6 +103,7 @@ public DependencyManagementRenderer( Sink sink, Locale locale, I18N i18n, Log lo
this.projectBuilder = projectBuilder;
this.buildingRequest = buildingRequest;
this.repoUtils = repoUtils;
this.licenseMappings = licenseMappings;
}

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -255,7 +261,12 @@ private String[] getDependencyRow( Dependency dependency, boolean hasClassifier
List<License> licenses = artifactProject.getLicenses();
for ( License license : licenses )
{
String licenseCell = ProjectInfoReportUtils.getArtifactIdCell( license.getName(), license.getUrl() );
String name = license.getName();
if ( licenseMappings != null && licenseMappings.containsKey( name ) )
{
name = licenseMappings.get( name );
}
String licenseCell = ProjectInfoReportUtils.getArtifactIdCell( name, license.getUrl() );
if ( licensesBuffer.length() > 0 )
{
licensesBuffer.append( ", " );
Expand All @@ -273,11 +284,11 @@ private String[] getDependencyRow( Dependency dependency, boolean hasClassifier
}
catch ( ProjectBuildingException e )
{
if ( log.isDebugEnabled() )
if ( log.isDebugEnabled() )
{
log.warn( "Unable to create Maven project for " + artifact.getId() + " from repository.", e );
}
else
}
else
{
log.warn( "Unable to create Maven project for " + artifact.getId() + " from repository." );
}
Expand Down

0 comments on commit eeb81c8

Please sign in to comment.