Skip to content

Commit

Permalink
Fix mojohaus#358 NPE on site generation caused by null returned by de…
Browse files Browse the repository at this point in the history
…precated MavenProject.getDependencyArtifacts()
  • Loading branch information
Wych-Dev committed Apr 12, 2020
1 parent c9f09f7 commit 7ec5990
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,6 @@
* #L%
*/

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.commons.collections.CollectionUtils;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
Expand All @@ -54,7 +41,6 @@
import org.codehaus.mojo.license.api.DependenciesTool;
import org.codehaus.mojo.license.api.DependenciesToolException;
import org.codehaus.mojo.license.api.MavenProjectDependenciesConfigurator;
import org.codehaus.mojo.license.api.ResolvedProjectDependencies;
import org.codehaus.mojo.license.api.ThirdPartyDetails;
import org.codehaus.mojo.license.api.ThirdPartyHelper;
import org.codehaus.mojo.license.api.ThirdPartyTool;
Expand All @@ -66,14 +52,28 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeSet;
import org.codehaus.mojo.license.api.ResolvedProjectDependencies;

/**
* Base class for third-party reports.
*
* @author tchemit [email protected]
* @since 1.1
*/
public abstract class AbstractThirdPartyReportMojo extends AbstractMavenReport
implements MavenProjectDependenciesConfigurator
implements MavenProjectDependenciesConfigurator
{
private static final Logger LOG = LoggerFactory.getLogger( AbstractThirdPartyReportMojo.class );

Expand Down Expand Up @@ -187,9 +187,9 @@ public abstract class AbstractThirdPartyReportMojo extends AbstractMavenReport
private File missingFile;

/**
* The Url that holds the missing license dependency entries. This is an extension to {@link #missingFile}. If set
* then the entries that will be found at this URL will be added additionally to the entries of the missing
* file.<br>
* The Url that holds the missing license dependency entries. This is an extension to {@link #missingFile}.
* If set then the entries that will be found at this URL will be added additionally to the entries of the
* missing file.<br>
* <br>
* <b>NOTE:</b><br>
* the response of the URL endpoint must return content that matches the THIRD-PARTY.properties file!
Expand All @@ -200,10 +200,10 @@ public abstract class AbstractThirdPartyReportMojo extends AbstractMavenReport
String missingFileUrl;

/**
* A file containing the override license information for dependencies. <b>Note:</b> Specify either
* {@link #overrideUrl} (preferred) or {@link #overrideFile}. If none of these is specified, then
* {@value LicenseMojoUtils#DEFAULT_OVERRIDE_THIRD_PARTY} resolved against <code>${basedir}</code> will be used if
* it exists.
* A file containing the override license information for dependencies.
* <b>Note:</b> Specify either {@link #overrideUrl} (preferred) or {@link #overrideFile}.
* If none of these is specified, then {@value LicenseMojoUtils#DEFAULT_OVERRIDE_THIRD_PARTY} resolved against
* <code>${basedir}</code> will be used if it exists.
*
* @since 1.11
* @deprecated Use {@link #overrideUrl} instead
Expand All @@ -213,13 +213,12 @@ public abstract class AbstractThirdPartyReportMojo extends AbstractMavenReport
private File overrideFile;

/**
* A URL pointing at a property file with the override license information for dependencies. <b>Note:</b> Specify
* either {@link #overrideUrl} (preferred) or {@link #overrideFile}. If none of these is specified, then
* {@value LicenseMojoUtils#DEFAULT_OVERRIDE_THIRD_PARTY} resolved against <code>${basedir}</code> will be used if
* it exists.
* A URL pointing at a property file with the override license information for dependencies.
* <b>Note:</b> Specify either {@link #overrideUrl} (preferred) or {@link #overrideFile}.
* If none of these is specified, then {@value LicenseMojoUtils#DEFAULT_OVERRIDE_THIRD_PARTY} resolved against
* <code>${basedir}</code> will be used if it exists.
* <p>
* An example of the file content:
*
* <pre>
* org.jboss.xnio--xnio-api--3.3.6.Final=The Apache Software License, Version 2.0
* </pre>
Expand Down Expand Up @@ -251,26 +250,26 @@ public abstract class AbstractThirdPartyReportMojo extends AbstractMavenReport
* <p>
* Example :
* <p>
*
* <pre>
* &lt;licenseMerges&gt; &lt;licenseMerge&gt;The Apache Software License|Version 2.0,Apache License, Version
* 2.0&lt;/licenseMerge&gt; &lt;/licenseMerges&gt; &lt;/pre&gt;
* &lt;licenseMerges&gt;
* &lt;licenseMerge&gt;The Apache Software License|Version 2.0,Apache License, Version 2.0&lt;/licenseMerge&gt;
* &lt;/licenseMerges&gt;
* &lt;/pre&gt;
*
* <b>Note:</b> This option will be overridden by {@link #licenseMergesUrl} if it is used by command line.
*
* @since 1.0
*/
@Parameter
private List<String> licenseMerges;

/**
* Location of file with the merge licenses in order to be used by command line. <b>Note:</b> This option overrides
* {@link #licenseMerges}.
*
* @since 1.18
*/
@Parameter( property = "license.licenseMergesUrl" )
protected String licenseMergesUrl;
* Location of file with the merge licenses in order to be used by command line.
* <b>Note:</b> This option overrides {@link #licenseMerges}.
*
* @since 1.18
*/
@Parameter( property = "license.licenseMergesUrl" )
protected String licenseMergesUrl;

/**
* The output directory for the report. Note that this parameter is only evaluated if the goal is run directly from
Expand All @@ -285,7 +284,8 @@ public abstract class AbstractThirdPartyReportMojo extends AbstractMavenReport
/**
* Flag to activate verbose mode.
* <p>
* <b>Note:</b> Verbose mode is always on if you starts a debug maven instance (says via {@code -X}).
* <b>Note:</b> Verbose mode is always on if you starts a debug maven instance
* (says via {@code -X}).
*
* @since 1.0
*/
Expand All @@ -295,7 +295,8 @@ public abstract class AbstractThirdPartyReportMojo extends AbstractMavenReport
/**
* Encoding used to read and writes files.
* <p>
* <b>Note:</b> If nothing is filled here, we will use the system property {@code file.encoding}.
* <b>Note:</b> If nothing is filled here, we will use the system
* property {@code file.encoding}.
*
* @since 1.0
*/
Expand Down Expand Up @@ -348,7 +349,6 @@ public abstract class AbstractThirdPartyReportMojo extends AbstractMavenReport

/**
* A URL returning a plain text file that contains include/exclude artifact filters in the following format:
*
* <pre>
* {@code
* # this is a comment
Expand All @@ -360,8 +360,7 @@ public abstract class AbstractThirdPartyReportMojo extends AbstractMavenReport
* exclude scope system
* include type jar
* exclude type war
* }
* </pre>
* }</pre>
*
* @since 1.18
*/
Expand All @@ -374,9 +373,10 @@ public abstract class AbstractThirdPartyReportMojo extends AbstractMavenReport
// Protected Abstract Methods
// ----------------------------------------------------------------------

protected abstract Collection<ThirdPartyDetails> createThirdPartyDetails() throws IOException,
ThirdPartyToolException, ProjectBuildingException, MojoFailureException, InvalidDependencyVersionException,
ArtifactNotFoundException, ArtifactResolutionException, DependenciesToolException, MojoExecutionException;
protected abstract Collection<ThirdPartyDetails> createThirdPartyDetails()
throws IOException, ThirdPartyToolException, ProjectBuildingException, MojoFailureException,
InvalidDependencyVersionException, ArtifactNotFoundException, ArtifactResolutionException,
DependenciesToolException, MojoExecutionException;

// ----------------------------------------------------------------------
// AbstractMavenReport Implementation
Expand All @@ -386,10 +386,10 @@ protected abstract Collection<ThirdPartyDetails> createThirdPartyDetails() throw
* Method to initialize the mojo before doing any concrete actions.
*
* <b>Note:</b> The method is invoked before the {@link #executeReport()} method.
*
* @throws IOException
*/
protected void init() throws IOException
protected void init()
throws IOException
{
if ( licenseMergesUrl != null )
{
Expand All @@ -406,7 +406,8 @@ protected void init() throws IOException
/**
* {@inheritDoc}
*/
protected void executeReport( Locale locale ) throws MavenReportException
protected void executeReport( Locale locale )
throws MavenReportException
{
resolvedOverrideUrl = LicenseMojoUtils.prepareThirdPartyOverrideUrl( resolvedOverrideUrl, overrideFile,
overrideUrl, project.getBasedir() );
Expand Down Expand Up @@ -451,8 +452,8 @@ protected void executeReport( Locale locale ) throws MavenReportException
throw new MavenReportException( e.getMessage(), e );
}

ThirdPartyReportRenderer renderer = new ThirdPartyReportRenderer( getSink(), i18n, getOutputName(), locale,
details );
ThirdPartyReportRenderer renderer =
new ThirdPartyReportRenderer( getSink(), i18n, getOutputName(), locale, details );
renderer.render();

}
Expand Down Expand Up @@ -528,8 +529,8 @@ public ArtifactFilters getArtifactFilters()
if ( artifactFilters == null )
{
artifactFilters = ArtifactFilters.of( includedGroups, excludedGroups, includedArtifacts, excludedArtifacts,
includedScopes, excludedScopes, includedTypes, excludedTypes, includeOptional, artifactFiltersUrl,
getEncoding() );
includedScopes, excludedScopes, includedTypes, excludedTypes,
includeOptional, artifactFiltersUrl , getEncoding() );
}
return artifactFilters;
}
Expand All @@ -547,8 +548,8 @@ public boolean isVerbose()
// ----------------------------------------------------------------------

Collection<ThirdPartyDetails> createThirdPartyDetails( MavenProject project, boolean loadArtifacts )
throws IOException, ThirdPartyToolException, ProjectBuildingException, MojoFailureException,
DependenciesToolException, MojoExecutionException
throws IOException, ThirdPartyToolException, ProjectBuildingException, MojoFailureException,
DependenciesToolException, MojoExecutionException
{

ResolvedProjectDependencies loadedDependencies;
Expand All @@ -563,8 +564,10 @@ Collection<ThirdPartyDetails> createThirdPartyDetails( MavenProject project, boo
MojoHelper.getDependencyArtifacts( project ) );
}

ThirdPartyHelper thirdPartyHelper = new DefaultThirdPartyHelper( project, encoding, verbose, dependenciesTool,
thirdPartyTool, project.getRemoteArtifactRepositories(), project.getRemoteProjectRepositories() );
ThirdPartyHelper thirdPartyHelper =
new DefaultThirdPartyHelper( project, encoding, verbose,
dependenciesTool, thirdPartyTool,
project.getRemoteArtifactRepositories(), project.getRemoteProjectRepositories() );
// load dependencies of the project
SortedMap<String, MavenProject> projectDependencies = thirdPartyHelper.loadDependencies( this,
loadedDependencies );
Expand All @@ -576,7 +579,8 @@ Collection<ThirdPartyDetails> createThirdPartyDetails( MavenProject project, boo
SortedSet<MavenProject> dependenciesWithNoLicense = thirdPartyHelper.getProjectsWithNoLicense( licenseMap );

// compute safe dependencies (with pom licenses)
Set<MavenProject> dependenciesWithPomLicense = new TreeSet<>( MojoHelper.newMavenProjectComparator() );
Set<MavenProject> dependenciesWithPomLicense =
new TreeSet<>( MojoHelper.newMavenProjectComparator() );
dependenciesWithPomLicense.addAll( projectDependencies.values() );

if ( CollectionUtils.isNotEmpty( dependenciesWithNoLicense ) )
Expand All @@ -586,11 +590,10 @@ Collection<ThirdPartyDetails> createThirdPartyDetails( MavenProject project, boo

if ( useMissingFile )
{
// Resolve unsafe dependencies using missing files, this will update licenseMap
// and unsafeDependencies
// Resolve unsafe dependencies using missing files, this will update licenseMap and unsafeDependencies
thirdPartyHelper.createUnsafeMapping( licenseMap, missingFile, missingFileUrl,
useRepositoryMissingFiles, dependenciesWithNoLicense, projectDependencies,
loadedDependencies.getAllDependencies() );
useRepositoryMissingFiles, dependenciesWithNoLicense,
projectDependencies, loadedDependencies.getAllDependencies() );
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/main/java/org/codehaus/mojo/license/utils/MojoHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,9 @@ public int compare( MavenProject o1, MavenProject o2 )

}

protected static final double[] TIME_FACTORS =
{ 1000000, 1000, 60, 60, 24 };
protected static final double[] TIME_FACTORS = { 1000000, 1000, 60, 60, 24 };

protected static final String[] TIME_UNITES =
{ "ns", "ms", "s", "m", "h", "d" };
protected static final String[] TIME_UNITES = { "ns", "ms", "s", "m", "h", "d" };

public static String convertTime( long value )
{
Expand All @@ -161,7 +159,7 @@ public static String convert( long value, double[] factors, String[] unites )
long sign = value == 0 ? 1 : value / Math.abs( value );
int i = 0;
double tmp = Math.abs( value );
while (i < factors.length && i < unites.length && tmp > factors[i])
while ( i < factors.length && i < unites.length && tmp > factors[i] )
{
tmp = tmp / factors[i++];
}
Expand Down Expand Up @@ -275,5 +273,4 @@ public static Set<Artifact> getDependencyArtifacts( MavenProject project )
}
return project.getDependencyArtifacts();
}

}

0 comments on commit 7ec5990

Please sign in to comment.