Skip to content

Commit

Permalink
Merge pull request #257 from ppalaga/i252
Browse files Browse the repository at this point in the history
Fix #252 Add writeVersions parameter to AbstractDownloadLicensesMojo
  • Loading branch information
ppalaga authored Feb 13, 2019
2 parents 9d52d36 + a118736 commit 25d0772
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
<licenses>
<license>
<name>BSD License</name>
<url>http://www.antlr.org/license.html</url>
<distribution>repo</distribution>
<file>bsd-antlr.html</file>
<file>bsd-license-from-config.html</file>
</license>
</licenses>
</dependency>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
<licenses>
<license>
<name>CDDL/GPLv2+CE</name>
Expand All @@ -31,7 +29,6 @@
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
Expand All @@ -44,7 +41,6 @@
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<licenseSummary>
<dependencies>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<licenses>
<license>
<name>BSD License</name>
<url>http://www.antlr.org/license.html</url>
<distribution>repo</distribution>
<file>bsd-license-from-config.html</file>
</license>
</licenses>
</dependency>
</dependencies>
</licenseSummary>
9 changes: 3 additions & 6 deletions src/it/download-licenses-file-names/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<configuration>
<licensesOutputDirectory>${project.build.directory}/basic/licenses</licensesOutputDirectory>
<licensesOutputFile>${project.build.directory}/basic/licenses.xml</licensesOutputFile>
<licensesConfigFile>${basedir}/licenses-config.xml</licensesConfigFile>
<licensesConfigFile>${basedir}/licenses-config-basic.xml</licensesConfigFile>
<errorRemedy>xmlOutput</errorRemedy>
<sortByGroupIdAndArtifactId>true</sortByGroupIdAndArtifactId>
<licenseUrlReplacements>
Expand Down Expand Up @@ -140,14 +140,15 @@
<goal>download-licenses</goal>
</goals>
<configuration>
<licensesConfigFile>${basedir}/src/license/licenses.xml</licensesConfigFile>
<licensesConfigFile>${basedir}/licenses-config-no-versions.xml</licensesConfigFile>
<licensesOutputDirectory>${project.build.directory}/cleanLicDir/licenses</licensesOutputDirectory>
<licensesOutputFile>${project.build.directory}/cleanLicDir/licenses.xml</licensesOutputFile>
<licensesErrorsFile>${project.build.directory}/cleanLicDir/licenses-errors.xml</licensesErrorsFile>
<cleanLicensesOutputDirectory>true</cleanLicensesOutputDirectory>
<forceDownload>true</forceDownload>
<errorRemedy>xmlOutput</errorRemedy>
<sortByGroupIdAndArtifactId>true</sortByGroupIdAndArtifactId>
<writeVersions>false</writeVersions>
<licenseUrlReplacements>
<licenseUrlReplacement>
<regexp>^https?://github\.com/([^/]+)/([^/]+)/blob/(.*)$</regexp>
Expand All @@ -156,10 +157,6 @@
</licenseUrlReplacements>
<licenseUrlFileNames>
<spdx/>
<bsd-antlr.html>
sha1:074f8d6e91730b40875178666513014472888247
\Qhttp://www.antlr.org/license.html\E
</bsd-antlr.html>
<cddl-gplv2-ce.txt>
sha1:c27730aef98e1d50e937d808654daad00b258f90
\Qhttps://raw.githubusercontent.com/javaee/activation/master/LICENSE.txt\E
Expand Down
2 changes: 1 addition & 1 deletion src/it/download-licenses-file-names/postbuild.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ return {
assert Files.exists(asl2)
assert asl2.text.contains('Version 2.0, January 2004')

final Path bsdAntlr = outputBase.resolve('licenses/bsd-antlr.html')
final Path bsdAntlr = outputBase.resolve('licenses/bsd-license-from-config.html')
assert Files.exists(bsdAntlr)
assert bsdAntlr.text.contains('Copyright (c) 2012 Terence Parr and Sam Harwell')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,19 @@ public abstract class AbstractDownloadLicensesMojo
@Parameter
private Map<String, String> licenseUrlFileNames;

/**
* If {@code true}, {@link #licensesOutputFile} and {@link #licensesErrorsFile} will contain {@code <version>}
* elements for each {@code <dependency>}; otherwise the {@code <version>} {@link #licensesOutputFile} and
* {@link #licensesErrorsFile} elements will not be appended under {@code <dependency>} elements in
* <b>
* Might be useful if you want to keep the {@link #licensesOutputFile} under source control and you do not want to
* see the changing dependency versions there.
*
* @since 1.18
*/
@Parameter( property = "license.writeVersions", defaultValue = "true" )
private boolean writeVersions;

// ----------------------------------------------------------------------
// Plexus Components
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -581,11 +594,11 @@ public void execute()

List<ProjectLicenseInfo> depProjectLicensesWithErrors = filterErrors( depProjectLicenses );
LicenseSummaryWriter.writeLicenseSummary( depProjectLicenses, licensesOutputFile, charset,
licensesOutputFileEol );
licensesOutputFileEol, writeVersions );
if ( depProjectLicensesWithErrors != null && !depProjectLicensesWithErrors.isEmpty() )
{
LicenseSummaryWriter.writeLicenseSummary( depProjectLicensesWithErrors, licensesErrorsFile, charset,
licensesOutputFileEol );
licensesOutputFileEol, writeVersions );
}
}
catch ( Exception e )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
public class LicenseSummaryWriter
{
public static void writeLicenseSummary( List<ProjectLicenseInfo> dependencies, File outputFile, Charset charset,
Eol eol )
Eol eol, boolean writeVersions )
throws ParserConfigurationException, TransformerException, IOException
{
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
Expand All @@ -68,7 +68,7 @@ public static void writeLicenseSummary( List<ProjectLicenseInfo> dependencies, F

for ( ProjectLicenseInfo dep : dependencies )
{
dependenciesNode.appendChild( createDependencyNode( doc, dep ) );
dependenciesNode.appendChild( createDependencyNode( doc, dep, writeVersions ) );
}

// Prepare the output file File
Expand All @@ -87,7 +87,7 @@ public static void writeLicenseSummary( List<ProjectLicenseInfo> dependencies, F
}
}

public static Node createDependencyNode( Document doc, ProjectLicenseInfo dep )
public static Node createDependencyNode( Document doc, ProjectLicenseInfo dep, boolean writeVersions )
{
Node depNode = doc.createElement( "dependency" );

Expand All @@ -99,9 +99,12 @@ public static Node createDependencyNode( Document doc, ProjectLicenseInfo dep )
artifactIdNode.appendChild( doc.createTextNode( dep.getArtifactId() ) );
depNode.appendChild( artifactIdNode );

Node versionNode = doc.createElement( "version" );
versionNode.appendChild( doc.createTextNode( dep.getVersion() ) );
depNode.appendChild( versionNode );
if ( writeVersions )
{
Node versionNode = doc.createElement( "version" );
versionNode.appendChild( doc.createTextNode( dep.getVersion() ) );
depNode.appendChild( versionNode );
}

Node licensesNode = doc.createElement( "licenses" );
if ( dep.getLicenses() == null || dep.getLicenses().size() == 0 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,51 @@ public void testWriteReadLicenseSummary()
licSummary.add( dep1 );
licSummary.add( dep2 );

File licenseSummaryFile = File.createTempFile( "licSummary", "tmp" );
// File licenseSummaryFile = new File( "src/test/resources/license-summary-test-2.xml" );
LicenseSummaryWriter.writeLicenseSummary( licSummary, licenseSummaryFile, StandardCharsets.UTF_8, Eol.LF );
{
File licenseSummaryFile = File.createTempFile( "licSummary", "tmp" );
LicenseSummaryWriter.writeLicenseSummary( licSummary, licenseSummaryFile, StandardCharsets.UTF_8, Eol.LF,
true );

Assert.assertTrue( licenseSummaryFile.exists() );
FileInputStream fis = new FileInputStream( licenseSummaryFile );
List<ProjectLicenseInfo> list = LicenseSummaryReader.parseLicenseSummary( fis );
fis.close();
ProjectLicenseInfo dep = list.get( 0 );
Assert.assertEquals( "org.test", dep.getGroupId() );
Assert.assertEquals( "test1", dep.getArtifactId() );
Assert.assertEquals( "1.0", dep.getVersion() );
Assert.assertTrue( licenseSummaryFile.exists() );
FileInputStream fis = new FileInputStream( licenseSummaryFile );
List<ProjectLicenseInfo> list = LicenseSummaryReader.parseLicenseSummary( fis );
fis.close();
ProjectLicenseInfo dep = list.get( 0 );
Assert.assertEquals( "org.test", dep.getGroupId() );
Assert.assertEquals( "test1", dep.getArtifactId() );
Assert.assertEquals( "1.0", dep.getVersion() );

List<ProjectLicense> licenses = dep.getLicenses();
Assert.assertEquals( 1, licenses.size() );
ProjectLicense lic0 = dep.getLicenses().get(0);
Assert.assertEquals( "lgpl", lic0.getName() );
Assert.assertEquals( "http://www.gnu.org/licenses/lgpl-3.0.txt", lic0.getUrl() );
Assert.assertEquals( "lgpl-3.0.txt", lic0.getFile() );
Assert.assertEquals( "lgpl version 3.0", lic0.getComments() );
List<ProjectLicense> licenses = dep.getLicenses();
Assert.assertEquals( 1, licenses.size() );
ProjectLicense lic0 = dep.getLicenses().get(0);
Assert.assertEquals( "lgpl", lic0.getName() );
Assert.assertEquals( "http://www.gnu.org/licenses/lgpl-3.0.txt", lic0.getUrl() );
Assert.assertEquals( "lgpl-3.0.txt", lic0.getFile() );
Assert.assertEquals( "lgpl version 3.0", lic0.getComments() );
}

{
File licenseSummaryFile = File.createTempFile( "licSummaryNoVersion", "tmp" );
LicenseSummaryWriter.writeLicenseSummary( licSummary, licenseSummaryFile, StandardCharsets.UTF_8, Eol.LF,
false );

Assert.assertTrue( licenseSummaryFile.exists() );
FileInputStream fis = new FileInputStream( licenseSummaryFile );
List<ProjectLicenseInfo> list = LicenseSummaryReader.parseLicenseSummary( fis );
fis.close();
ProjectLicenseInfo dep = list.get( 0 );
Assert.assertEquals( "org.test", dep.getGroupId() );
Assert.assertEquals( "test1", dep.getArtifactId() );
Assert.assertNull( dep.getVersion() );

List<ProjectLicense> licenses = dep.getLicenses();
Assert.assertEquals( 1, licenses.size() );
ProjectLicense lic0 = dep.getLicenses().get(0);
Assert.assertEquals( "lgpl", lic0.getName() );
Assert.assertEquals( "http://www.gnu.org/licenses/lgpl-3.0.txt", lic0.getUrl() );
Assert.assertEquals( "lgpl-3.0.txt", lic0.getFile() );
Assert.assertEquals( "lgpl version 3.0", lic0.getComments() );
}

}
}

0 comments on commit 25d0772

Please sign in to comment.