Skip to content

Commit

Permalink
Merge pull request #238 from ppalaga/i235
Browse files Browse the repository at this point in the history
Fix #235 NPE when there is no <url> under <license>
  • Loading branch information
ppalaga authored Feb 5, 2019
2 parents 61fec38 + 4fa41d5 commit 8d5bb28
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 54 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ install:
- cmd: SET PATH=C:\Users\appveyor\.m2\apache-maven-3.5.4-bin\4lcg54ki11c6mp435njk296gm5\apache-maven-3.5.4\bin;%JAVA_HOME%\bin;%PATH%

build_script:
- .\mvnw.cmd clean verify
- .\mvnw.cmd clean verify -e

cache:
- C:\Users\appveyor\.m2\repository
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@
<pomIncludes>
<pomInclude>MLICENSE-4/pom.xml</pomInclude>
</pomIncludes>
<cloneProjectsTo>${project.build.directory}/it2</cloneProjectsTo>
</configuration>
</execution>
</executions>
Expand Down
1 change: 1 addition & 0 deletions src/it/download-licenses-force/invoker.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
invoker.goals = clean license:download-licenses -Dlicense.forceDownload=true -Dlicense.errorRemedy=xmlOutput -Dlicense.sortByGroupIdAndArtifactId=true
invoker.buildResult=failure
14 changes: 14 additions & 0 deletions src/it/download-licenses-force/licenses.expected.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
</license>
</licenses>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
<licenses>
<license>
<name>JSR-000107 JCACHE 2.9 Public Review - Updated Specification
License</name>
<url>https://raw.github.com/jsr107/jsr107spec/master/LICENSE.txt</url>
<distribution>manual</distribution>
<file>jsr-000107 jcache 2.9 public review - updated specification license - license.txt</file>
</license>
</licenses>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-parent</artifactId>
Expand Down
11 changes: 11 additions & 0 deletions src/it/download-licenses-force/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
Expand Down
4 changes: 4 additions & 0 deletions src/it/download-licenses-force/postbuild.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ assert expectedLicensesXml.text.equals(licensesXml.text)
final Path expectedLicensesErrorsXml = basePath.resolve('licenses-errors.expected.xml')
final Path licensesErrorsXml = basePath.resolve('target/generated-resources/licenses-errors.xml')
assert expectedLicensesErrorsXml.text.equals(licensesErrorsXml.text)

final Path log = basePath.resolve('build.log')
assert Files.exists(log)
assert log.text.contains('There were 1 download errors - check ')
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ public abstract class AbstractDownloadLicensesMojo
* <ul>{@link ErrorRemedy#failFast}: a {@link MojoFailureException} is thrown on the first download related
* error</ul>
* <ul>{@link ErrorRemedy#xmlOutput}: error messages are added as {@code <downloaderMessages>} to
* {@link AbstractDownloadLicensesMojo#licensesErrorsFile}</ul>
* {@link AbstractDownloadLicensesMojo#licensesErrorsFile}; in case there are error messages, the build will
* fail after processing all dependencies</ul>
* </li>
* @since 1.18
*/
Expand Down Expand Up @@ -383,6 +384,8 @@ public abstract class AbstractDownloadLicensesMojo
*/
private String proxyLoginPasswordEncoded;

private int downloadErrorCount = 0;

protected abstract boolean isSkip();

protected MavenProject getProject()
Expand Down Expand Up @@ -443,9 +446,9 @@ public void execute()
Map<String, ProjectLicenseInfo> configuredDepLicensesMap = new HashMap<>();

// License info from previous build
if ( licensesOutputFile.exists() )
if ( !forceDownload && licensesOutputFile.exists() )
{
loadLicenseInfo( configuredDepLicensesMap, licensesOutputFile, !forceDownload );
loadLicenseInfo( configuredDepLicensesMap, licensesOutputFile, true );
}

// Manually configured license info, loaded second to override previously loaded info
Expand Down Expand Up @@ -531,6 +534,23 @@ public void execute()
//restore the system properties to what they where before the plugin execution
restoreProperties();
}

switch ( errorRemedy )
{
case ignore:
case failFast:
/* do nothing */
break;
case warn:
getLog().warn( "There were " + downloadErrorCount + " download errors - check the warnings above" );
break;
case xmlOutput:
throw new MojoFailureException( "There were " + downloadErrorCount + " download errors - check "
+ licensesErrorsFile.getAbsolutePath() );
default:
throw new IllegalStateException( "Unexpected value of " + ErrorRemedy.class.getName() + ": "
+ errorRemedy );
}
}

/**
Expand Down Expand Up @@ -863,6 +883,8 @@ private String getLicenseFileName( ProjectLicenseInfo depProject, final URL lice
// This means it isn't a valid file extension, so append the default
licenseFileName = licenseFileName + defaultExtension;
}
// Normalize whitespace
licenseFileName = licenseFileName.replaceAll( "\\s+", " " );
}

// lower case and (back)slash removal
Expand Down Expand Up @@ -890,72 +912,81 @@ private void downloadLicenses( LicenseDownloader licenseDownloader, ProjectLicen
return;
}

int licenseIndex = 0;
for ( ProjectLicense license : licenses )
{
final String licenseUrl = rewriteLicenseUrlIfNecessary( license.getUrl() );
try
if ( license.getUrl() == null )
{

File licenseOutputFile = downloadedLicenseURLs.get( licenseUrl );
if ( licenseOutputFile == null )
handleError( depProject, "No URL for license at index " + licenseIndex + " in dependency "
+ depProject.toString() );
}
else
{
final String licenseUrl = rewriteLicenseUrlIfNecessary( license.getUrl() );
try
{
final String licenseFileName;
if ( license.getFile() != null )
{
licenseFileName = new File( license.getFile() ).getName();
}
else
{
licenseFileName = getLicenseFileName( depProject,
new URL( license.getUrl() ),
license.getName() );
}
licenseOutputFile = new File( licensesOutputDirectory, licenseFileName );
}

if ( !licenseOutputFile.exists() || forceDownload )
{
if ( !downloadedLicenseURLs.containsKey( licenseUrl ) || organizeLicensesByDependencies )
File licenseOutputFile = downloadedLicenseURLs.get( licenseUrl );
if ( licenseOutputFile == null )
{
final LicenseDownloadResult result =
licenseDownloader.downloadLicense( licenseUrl, proxyLoginPasswordEncoded, licenseOutputFile,
getLog() );

if ( result.isSuccess() )
final String licenseFileName;
if ( license.getFile() != null )
{
licenseOutputFile = result.getFile();
downloadedLicenseURLs.put( licenseUrl, licenseOutputFile );
licenseFileName = new File( license.getFile() ).getName();
}
else
{
handleError( depProject, result.getErrorMessage() );
licenseFileName = getLicenseFileName( depProject,
new URL( license.getUrl() ),
license.getName() );
}
licenseOutputFile = new File( licensesOutputDirectory, licenseFileName );
}

if ( !licenseOutputFile.exists() || forceDownload )
{
if ( !downloadedLicenseURLs.containsKey( licenseUrl ) || organizeLicensesByDependencies )
{
final LicenseDownloadResult result =
licenseDownloader.downloadLicense( licenseUrl, proxyLoginPasswordEncoded,
licenseOutputFile, getLog() );

if ( result.isSuccess() )
{
licenseOutputFile = result.getFile();
downloadedLicenseURLs.put( licenseUrl, licenseOutputFile );
}
else
{
handleError( depProject, result.getErrorMessage() );
}

}
}

if ( licenseOutputFile != null )
{
license.setFile( licenseOutputFile.getName() );
}
}

if ( licenseOutputFile != null )
}
catch ( URISyntaxException e )
{
license.setFile( licenseOutputFile.getName() );
handleError( depProject, "POM for dependency " + depProject.toString()
+ " has an invalid license URL: " + licenseUrl );
}
catch ( FileNotFoundException e )
{
handleError( depProject, "POM for dependency " + depProject.toString()
+ " has a license URL that returns file not found: " + licenseUrl );
}
catch ( IOException e )
{
handleError( depProject, "Unable to retrieve license from URL '" + licenseUrl + "' for dependency '"
+ depProject.toString() + "': " + e.getMessage() );
}

}
catch ( URISyntaxException e )
{
handleError( depProject, "POM for dependency " + depProject.toString() + " has an invalid license URL: "
+ licenseUrl );
}
catch ( FileNotFoundException e )
{
handleError( depProject, "POM for dependency " + depProject.toString()
+ " has a license URL that returns file not found: " + licenseUrl );
}
catch ( IOException e )
{
handleError( depProject, "Unable to retrieve license from URL '" + licenseUrl + "' for dependency '"
+ depProject.toString() + "': " + e.getMessage() );
}

licenseIndex++;
}

}
Expand All @@ -979,6 +1010,7 @@ private void handleError( ProjectLicenseInfo depProject, String msg ) throws Moj
throw new IllegalStateException( "Unexpected value of " + ErrorRemedy.class.getName() + ": "
+ errorRemedy );
}
downloadErrorCount++;
}

private String rewriteLicenseUrlIfNecessary( final String originalLicenseUrl )
Expand Down Expand Up @@ -1019,7 +1051,8 @@ public static enum ErrorRemedy
* thrown */
failFast,
/** Error messages are added as {@code <downloaderMessages>} to
* {@link AbstractDownloadLicensesMojo#licensesErrorsFile} */
* {@link AbstractDownloadLicensesMojo#licensesErrorsFile}; in case there are error messages, the build will
* fail after processing all dependencies. */
xmlOutput
}
}

0 comments on commit 8d5bb28

Please sign in to comment.