Skip to content

Commit

Permalink
Fix #242 Proxy settings ignored after #233
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Feb 8, 2019
1 parent a83cfc2 commit 57e0963
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.codehaus.mojo.license.utils.LicenseSummaryReader;
import org.codehaus.mojo.license.utils.LicenseSummaryWriter;
import org.codehaus.mojo.license.utils.MojoHelper;
import org.codehaus.plexus.util.Base64;

import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -425,13 +424,6 @@ public abstract class AbstractDownloadLicensesMojo
*/
private Cache cache;

/**
* Proxy Login/Password encoded(only if usgin a proxy with authentication).
*
* @since 1.4
*/
private String proxyLoginPasswordEncoded;

private int downloadErrorCount = 0;

protected abstract boolean isSkip();
Expand All @@ -454,21 +446,6 @@ protected SortedMap<String, MavenProject> getDependencies( MavenProject project
this, localRepository, remoteRepositories, null );
}

protected java.util.Properties systemProperties;

protected void storeProperties()
{
systemProperties = (java.util.Properties) System.getProperties().clone();
}
protected void restoreProperties()
{
if ( systemProperties != null )
{
System.setProperties( systemProperties );
systemProperties = null;
}
}

/**
* {@inheritDoc}
* @throws MojoFailureException
Expand All @@ -489,113 +466,101 @@ public void execute()

initDirectories();

try
{
initProxy();

Map<String, ProjectLicenseInfo> configuredDepLicensesMap = new HashMap<>();
Map<String, ProjectLicenseInfo> configuredDepLicensesMap = new HashMap<>();

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

// Manually configured license info, loaded second to override previously loaded info
if ( licensesConfigFile.exists() )
{
loadLicenseInfo( configuredDepLicensesMap, licensesConfigFile, false );
}
// Manually configured license info, loaded second to override previously loaded info
if ( licensesConfigFile.exists() )
{
loadLicenseInfo( configuredDepLicensesMap, licensesConfigFile, false );
}

Set<MavenProject> dependencies = getDependencies();
Set<MavenProject> dependencies = getDependencies();

// The resulting list of licenses after dependency resolution
List<ProjectLicenseInfo> depProjectLicenses = new ArrayList<>();
// The resulting list of licenses after dependency resolution
List<ProjectLicenseInfo> depProjectLicenses = new ArrayList<>();

try ( LicenseDownloader licenseDownloader = new LicenseDownloader() )
try ( LicenseDownloader licenseDownloader = new LicenseDownloader( findActiveProxy() ) )
{
for ( MavenProject project : dependencies )
{
for ( MavenProject project : dependencies )
Artifact artifact = project.getArtifact();
getLog().debug( "Checking licenses for project " + artifact );
String artifactProjectId = getArtifactProjectId( artifact );
ProjectLicenseInfo depProject;
if ( configuredDepLicensesMap.containsKey( artifactProjectId ) )
{
Artifact artifact = project.getArtifact();
getLog().debug( "Checking licenses for project " + artifact );
String artifactProjectId = getArtifactProjectId( artifact );
ProjectLicenseInfo depProject;
if ( configuredDepLicensesMap.containsKey( artifactProjectId ) )
{
depProject = configuredDepLicensesMap.get( artifactProjectId );
depProject.setVersion( artifact.getVersion() );
}
else
{
depProject = createDependencyProject( project );
}
depProjectLicenses.add( depProject );
depProject = configuredDepLicensesMap.get( artifactProjectId );
depProject.setVersion( artifact.getVersion() );
}
if ( !offline )
else
{
/* First save the matching URLs into the cache */
for ( ProjectLicenseInfo depProject : depProjectLicenses )
{
downloadLicenses( licenseDownloader, depProject, true );
}
getLog().debug( "Finished populating cache" );
/*
* Then attempt to download the rest of the URLs using the available cache entries to select local
* file names based on file content sha1
*/
for ( ProjectLicenseInfo depProject : depProjectLicenses )
{
downloadLicenses( licenseDownloader, depProject, false );
}
depProject = createDependencyProject( project );
}
depProjectLicenses.add( depProject );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}



try
if ( !offline )
{
if ( sortByGroupIdAndArtifactId )
/* First save the matching URLs into the cache */
for ( ProjectLicenseInfo depProject : depProjectLicenses )
{
depProjectLicenses = sortByGroupIdAndArtifactId( depProjectLicenses );
downloadLicenses( licenseDownloader, depProject, true );
}

if ( licensesOutputFileEncoding == null )
getLog().debug( "Finished populating cache" );
/*
* Then attempt to download the rest of the URLs using the available cache entries to select local
* file names based on file content sha1
*/
for ( ProjectLicenseInfo depProject : depProjectLicenses )
{
licensesOutputFileEncoding = System.getProperty( "file.encoding" );
getLog().warn( "Using the default system encoding for reading or writing licenses.xml file."
+ " This makes your build platform dependent. You should set either"
+ " project.build.sourceEncoding or licensesOutputFileEncoding" );
}
final Charset charset = Charset.forName( licensesOutputFileEncoding );
if ( licensesOutputFileEol == Eol.AUTODETECT )
{
final Path autodetectFromFile = licensesConfigFile.exists() ? licensesConfigFile.toPath()
: project.getBasedir().toPath().resolve( "pom.xml" );
licensesOutputFileEol = Eol.autodetect( autodetectFromFile, charset );
downloadLicenses( licenseDownloader, depProject, false );
}
}
}
catch ( IOException e )
{
throw new RuntimeException( e );
}

List<ProjectLicenseInfo> depProjectLicensesWithErrors = filterErrors( depProjectLicenses );
LicenseSummaryWriter.writeLicenseSummary( depProjectLicenses, licensesOutputFile, charset,
licensesOutputFileEol );
if ( depProjectLicensesWithErrors != null && !depProjectLicensesWithErrors.isEmpty() )
{
LicenseSummaryWriter.writeLicenseSummary( depProjectLicensesWithErrors, licensesErrorsFile, charset,
licensesOutputFileEol );
}
try
{
if ( sortByGroupIdAndArtifactId )
{
depProjectLicenses = sortByGroupIdAndArtifactId( depProjectLicenses );
}
catch ( Exception e )

if ( licensesOutputFileEncoding == null )
{
licensesOutputFileEncoding = System.getProperty( "file.encoding" );
getLog().warn( "Using the default system encoding for reading or writing licenses.xml file."
+ " This makes your build platform dependent. You should set either"
+ " project.build.sourceEncoding or licensesOutputFileEncoding" );
}
final Charset charset = Charset.forName( licensesOutputFileEncoding );
if ( licensesOutputFileEol == Eol.AUTODETECT )
{
throw new MojoExecutionException( "Unable to write license summary file: " + licensesOutputFile, e );
final Path autodetectFromFile = licensesConfigFile.exists() ? licensesConfigFile.toPath()
: project.getBasedir().toPath().resolve( "pom.xml" );
licensesOutputFileEol = Eol.autodetect( autodetectFromFile, charset );
}

List<ProjectLicenseInfo> depProjectLicensesWithErrors = filterErrors( depProjectLicenses );
LicenseSummaryWriter.writeLicenseSummary( depProjectLicenses, licensesOutputFile, charset,
licensesOutputFileEol );
if ( depProjectLicensesWithErrors != null && !depProjectLicensesWithErrors.isEmpty() )
{
LicenseSummaryWriter.writeLicenseSummary( depProjectLicensesWithErrors, licensesErrorsFile, charset,
licensesOutputFileEol );
}
}
finally
catch ( Exception e )
{
//restore the system properties to what they where before the plugin execution
restoreProperties();
throw new MojoExecutionException( "Unable to write license summary file: " + licensesOutputFile, e );
}

switch ( errorRemedy )
Expand Down Expand Up @@ -798,37 +763,18 @@ private void initDirectories()
}
}

private void initProxy()
private Proxy findActiveProxy()
throws MojoExecutionException
{
Proxy proxyToUse = null;
for ( Proxy proxy : proxies )
{
if ( proxy.isActive() && "http".equals( proxy.getProtocol() ) )
{

// found our proxy
proxyToUse = proxy;
break;
}
}
if ( proxyToUse != null )
{
//Save our system settings for restore after plugin run
storeProperties();
System.getProperties().put( "proxySet", "true" );
System.setProperty( "proxyHost", proxyToUse.getHost() );
System.setProperty( "proxyPort", String.valueOf( proxyToUse.getPort() ) );
if ( proxyToUse.getNonProxyHosts() != null )
{
System.setProperty( "nonProxyHosts", proxyToUse.getNonProxyHosts() );
}
if ( proxyToUse.getUsername() != null )
{
String loginPassword = proxyToUse.getUsername() + ":" + proxyToUse.getPassword();
proxyLoginPasswordEncoded = new String( Base64.encodeBase64( loginPassword.getBytes() ) );
return proxy;
}
}
return null;
}

/**
Expand Down Expand Up @@ -1067,8 +1013,7 @@ else if ( license.getUrl() != null )
if ( !licenseOutputFile.exists() || forceDownload )
{
LicenseDownloadResult result =
licenseDownloader.downloadLicense( licenseUrl, proxyLoginPasswordEncoded,
fileNameEntry, getLog() );
licenseDownloader.downloadLicense( licenseUrl, fileNameEntry, getLog() );
if ( !organizeLicensesByDependencies && result.isSuccess() )
{
/* check if we can re-use an existing file that has the same content */
Expand Down
Loading

0 comments on commit 57e0963

Please sign in to comment.