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 01fd9a1 commit a89989b
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 13 deletions.
Empty file modified pom.xml
100644 → 100755
Empty file.
11 changes: 11 additions & 0 deletions src/it/ISSUE-358/child-1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.tomred</groupId>
<artifactId>ISSUE-358</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>child-1</artifactId>
</project>
2 changes: 2 additions & 0 deletions src/it/ISSUE-358/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals=clean site
invoker.failureBehavior=fail-fast
51 changes: 51 additions & 0 deletions src/it/ISSUE-358/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.tomred</groupId>
<artifactId>ISSUE-358</artifactId>
<version>0.0.1-SNAPSHOT</version>

<packaging>pom</packaging>

<modules>
<module>child-1</module>
</modules>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>@pom.version@</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.0</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-site-renderer</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>

<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>@pom.version@</version>
</plugin>
</plugins>
</reporting>
</project>
6 changes: 3 additions & 3 deletions src/main/java/org/codehaus/mojo/license/AbstractThirdPartyReportMojo.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,13 @@ Collection<ThirdPartyDetails> createThirdPartyDetails( MavenProject project, boo
ResolvedProjectDependencies loadedDependencies;
if ( loadArtifacts )
{
loadedDependencies =
new ResolvedProjectDependencies( project.getArtifacts(), project.getDependencyArtifacts() );
loadedDependencies = new ResolvedProjectDependencies( project.getArtifacts(),
MojoHelper.getDependencyArtifacts( project ) );
}
else
{
loadedDependencies = new ResolvedProjectDependencies( getProject().getArtifacts(),
getProject().getDependencyArtifacts() );
MojoHelper.getDependencyArtifacts( project ) );
}

ThirdPartyHelper thirdPartyHelper =
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/codehaus/mojo/license/AddThirdPartyMojo.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.codehaus.mojo.license.api.ThirdPartyToolException;
import org.codehaus.mojo.license.model.LicenseMap;
import org.codehaus.mojo.license.utils.FileUtil;
import org.codehaus.mojo.license.utils.MojoHelper;
import org.codehaus.mojo.license.utils.SortedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -240,13 +241,13 @@ protected ResolvedProjectDependencies resolveDependencyArtifacts() throws Depend
}
if ( isAggregatorBuild )
{
dependencyArtifacts =
new ResolvedProjectDependencies( project.getArtifacts(), project.getDependencyArtifacts() );
dependencyArtifacts = new ResolvedProjectDependencies( project.getArtifacts(),
MojoHelper.getDependencyArtifacts( project ) );
}
else
{
dependencyArtifacts = new ResolvedProjectDependencies( project.getArtifacts(),
project.getDependencyArtifacts() );
MojoHelper.getDependencyArtifacts( project ) );
}
return dependencyArtifacts;
}
Expand Down Expand Up @@ -442,7 +443,7 @@ void initFromMojo( AggregatorAddThirdPartyMojo mojo, MavenProject mavenProject,
resolvedOverrideUrl = mojo.resolvedOverrideUrl;
missingLicensesFileArtifact = mojo.missingLicensesFileArtifact;
localRepository = mojo.localRepository;
dependencies = new HashSet<>( mavenProject.getDependencyArtifacts() );
dependencies = new HashSet<>( MojoHelper.getDependencyArtifacts( mavenProject ) );
licenseMerges = mojo.licenseMerges;
licenseMergesFile = mojo.licenseMergesFile;
includedLicenses = mojo.includedLicenses;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/codehaus/mojo/license/AggregateDownloadLicensesMojo.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.license.api.ResolvedProjectDependencies;
import org.codehaus.mojo.license.download.LicensedArtifact;
import org.codehaus.mojo.license.utils.MojoHelper;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -115,8 +116,7 @@ protected Map<String, LicensedArtifact> getDependencies()
for ( MavenProject p : reactorProjects )
{
licensedArtifactResolver.loadProjectDependencies( new ResolvedProjectDependencies( p.getArtifacts(),
p.getDependencyArtifacts() ),
this, remoteRepositories, result );
MojoHelper.getDependencyArtifacts( p ) ), this, remoteRepositories, result );
}
return result;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/codehaus/mojo/license/DownloadLicensesMojo.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.codehaus.mojo.license.api.ResolvedProjectDependencies;
import org.codehaus.mojo.license.download.LicensedArtifact;
import org.codehaus.mojo.license.utils.MojoHelper;

/**
* Download the license files of all the current project's dependencies, and generate a summary file containing a list
Expand Down Expand Up @@ -86,7 +87,7 @@ protected Map<String, LicensedArtifact> getDependencies()
{
final Map<String, LicensedArtifact> result = new TreeMap<>();
licensedArtifactResolver.loadProjectDependencies(
new ResolvedProjectDependencies( project.getArtifacts(), project.getDependencyArtifacts() ),
new ResolvedProjectDependencies( project.getArtifacts(), MojoHelper.getDependencyArtifacts( project ) ),
this, remoteRepositories, result );
return result;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/codehaus/mojo/license/LicensesXmlInsertVersionsMojo.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.codehaus.mojo.license.download.LicensedArtifact;
import org.codehaus.mojo.license.download.ProjectLicenseInfo;
import org.codehaus.mojo.license.utils.FileUtil;
import org.codehaus.mojo.license.utils.MojoHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -139,9 +140,8 @@ public ArtifactFilters getArtifactFilters()
}
};
final Map<String, LicensedArtifact> resolvedDeps = new TreeMap<String, LicensedArtifact>();
licensedArtifactResolver.loadProjectDependencies(
new ResolvedProjectDependencies( project.getArtifacts(), project.getDependencyArtifacts() ),
config, remoteRepositories, resolvedDeps );
licensedArtifactResolver.loadProjectDependencies( new ResolvedProjectDependencies( project.getArtifacts(),
MojoHelper.getDependencyArtifacts( project ) ), config, remoteRepositories, resolvedDeps );
final Map<String, LicensedArtifact> resolvedDepsMap = new HashMap<>( resolvedDeps.size() );
for ( LicensedArtifact dep : resolvedDeps.values() )
{
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/codehaus/mojo/license/utils/MojoHelper.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Resource;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Mojo helper methods.
Expand All @@ -42,6 +46,7 @@
*/
public class MojoHelper
{
private static final Logger LOG = LoggerFactory.getLogger( MojoHelper.class );

/**
* Add the directory as a resource of the given project.
Expand Down Expand Up @@ -245,4 +250,27 @@ public static List<String> getParams( String params )
String[] split = params == null ? new String[0] : params.split( "," );
return Arrays.asList( split );
}

/**
* {@link MavenProject#getDependencyArtifacts()} is deprecated.
*
* <p>
* This method checks if the dependency artifacts is {@code null} and returns an empty {@code HashSet} to avoid the
* {@code NullPointerException}s caused by the {@link MavenProject#getDependencyArtifacts()} returning {@code null}.
* </p>
*
* @param project the MavenProject to retrieve artifacts from
* @return a HashSet of dependencies or an empty set
*/
public static Set<Artifact> getDependencyArtifacts( MavenProject project )
{
if ( project == null || project.getDependencyArtifacts() == null )
{
LOG.warn( "" );
LOG.warn( "Non-transitive dependencies cannot be found. " );
LOG.warn( "" );
return new HashSet<Artifact>();
}
return project.getDependencyArtifacts();
}
}

0 comments on commit a89989b

Please sign in to comment.