From 1942da675ebbe29eddf4850a12f37b989e1daa5b Mon Sep 17 00:00:00 2001 From: Peter Palaga Date: Tue, 5 Feb 2019 19:31:14 +0100 Subject: [PATCH] Fix #235 NPE when there is no under --- .../license/AbstractDownloadLicensesMojo.java | 107 ++++++++++-------- 1 file changed, 58 insertions(+), 49 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/license/AbstractDownloadLicensesMojo.java b/src/main/java/org/codehaus/mojo/license/AbstractDownloadLicensesMojo.java index 635b12f0e..7a348caef 100644 --- a/src/main/java/org/codehaus/mojo/license/AbstractDownloadLicensesMojo.java +++ b/src/main/java/org/codehaus/mojo/license/AbstractDownloadLicensesMojo.java @@ -890,72 +890,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++; } }