Skip to content

Commit

Permalink
Merge pull request #243 from ppalaga/i242
Browse files Browse the repository at this point in the history
Fix #242 Proxy settings ignored after #233
  • Loading branch information
ppalaga committed Feb 8, 2019
2 parents a83cfc2 + 9d60d50 commit a8f79ef
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 147 deletions.
Expand Up @@ -37,16 +37,15 @@
import org.codehaus.mojo.license.api.ResolvedProjectDependencies;
import org.codehaus.mojo.license.download.Cache;
import org.codehaus.mojo.license.download.FileNameEntry;
import org.codehaus.mojo.license.download.LicenseDownloader;
import org.codehaus.mojo.license.download.PreferredFileNames;
import org.codehaus.mojo.license.download.LicenseDownloader.LicenseDownloadResult;
import org.codehaus.mojo.license.model.ProjectLicense;
import org.codehaus.mojo.license.model.ProjectLicenseInfo;
import org.codehaus.mojo.license.utils.FileUtil;
import org.codehaus.mojo.license.utils.LicenseDownloader;
import org.codehaus.mojo.license.utils.LicenseDownloader.LicenseDownloadResult;
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
Expand Up @@ -27,7 +27,7 @@
import java.util.Map;
import java.util.Map.Entry;

import org.codehaus.mojo.license.utils.LicenseDownloader.LicenseDownloadResult;
import org.codehaus.mojo.license.download.LicenseDownloader.LicenseDownloadResult;

/**
* A simple {@link HashMap} based in-memory cache for storing {@link LicenseDownloadResult}s.
Expand Down

0 comments on commit a8f79ef

Please sign in to comment.