Skip to content

Commit

Permalink
Fix #508 - Avoid NPE in AggregatorAddThirdPartyMojo (#512)
Browse files Browse the repository at this point in the history
Co-authored-by: Richard Zowalla <richard.zowalla@hs-heilbronn.de>
  • Loading branch information
slawekjaranowski and rzo1 authored Jul 23, 2023
1 parent d7922b4 commit 85ac3b0
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/it/ISSUE-508/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = license:aggregate-add-third-party
invoker.debug = true
50 changes: 50 additions & 0 deletions src/it/ISSUE-508/module-a/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
#%L
License Maven Plugin
%%
Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
%%
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Lesser Public License for more details.
You should have received a copy of the GNU General Lesser Public
License along with this program. If not, see
<http://www.gnu.org/licenses/lgpl-3.0.html>.
#L%
-->

<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.codehaus.mojo.license.test</groupId>
<artifactId>issue-508</artifactId>
<version>@project.version@</version>
</parent>

<name>License Test :: ISSUE-508 :: Module A</name>
<artifactId>issue-508-module-a</artifactId>

<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>

</project>
73 changes: 73 additions & 0 deletions src/it/ISSUE-508/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
#%L
License Maven Plugin
%%
Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
%%
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Lesser Public License for more details.
You should have received a copy of the GNU General Lesser Public
License along with this program. If not, see
<http://www.gnu.org/licenses/lgpl-3.0.html>.
#L%
-->

<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.codehaus.mojo.license.test</groupId>
<artifactId>issue-508</artifactId>
<version>@project.version@</version>

<name>License Test :: ISSUE-508</name>

<packaging>pom</packaging>

<modules>
<module>module-a</module>
</modules>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>@project.version@</version>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
23 changes: 23 additions & 0 deletions src/it/ISSUE-508/postbuild.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* #%L
* License Maven Plugin
* %%
* Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/
file = new File(basedir, 'target/generated-sources/license/THIRD-PARTY.txt')
assert file.exists()
38 changes: 38 additions & 0 deletions src/it/ISSUE-508/prebuild.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* #%L
* License Maven Plugin
* %%
* Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit, Tony chemit
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.attribute.FileTime
import java.time.LocalDateTime
import java.time.ZoneOffset

Path basePath = basedir.toPath()

// create a THIRD-PARTY.txt
Files.createFile(basePath.resolve('THIRD-PARTY.txt'))

// project file in child must is older then THIRD-PARTY.txt
def fileTime = FileTime.from(LocalDateTime.now().minusDays(1L).toInstant(ZoneOffset.UTC))
Files.setLastModifiedTime(basePath.resolve('module-a/pom.xml'), fileTime)

return true
25 changes: 8 additions & 17 deletions src/main/java/org/codehaus/mojo/license/AddThirdPartyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,6 @@ public class AddThirdPartyMojo extends AbstractAddThirdPartyMojo implements Mave
*/
private boolean doGenerateMissing;

/**
* Whether this is an aggregate build, or a single-project goal. This setting determines which dependency artifacts
* will be examined by the plugin. AddThirdParty needs to load dependencies only for the single project it is run
* for, while AggregateAddThirdParty needs to load dependencies for the parent project, as well as all child
* projects in the reactor.
*/
private boolean isAggregatorBuild = false;

/**
* The reactor projects. When resolving dependencies, the aggregator goal needs to do custom handling
* of sibling dependencies for projects in the reactor,
Expand Down Expand Up @@ -224,13 +216,8 @@ protected ResolvedProjectDependencies resolveDependencyArtifacts() throws Depend
if (dependencyArtifacts != null) {
return dependencyArtifacts;
}
if (isAggregatorBuild) {
dependencyArtifacts =
new ResolvedProjectDependencies(project.getArtifacts(), MojoHelper.getDependencyArtifacts(project));
} else {
dependencyArtifacts =
new ResolvedProjectDependencies(project.getArtifacts(), MojoHelper.getDependencyArtifacts(project));
}
dependencyArtifacts =
new ResolvedProjectDependencies(project.getArtifacts(), MojoHelper.getDependencyArtifacts(project));
return dependencyArtifacts;
}

Expand Down Expand Up @@ -390,6 +377,7 @@ private void writeMissingFile() throws IOException {
}
}

// magic method - to refactor
void initFromMojo(AggregatorAddThirdPartyMojo mojo, MavenProject mavenProject, List<MavenProject> reactorProjects)
throws Exception {
project = mavenProject;
Expand Down Expand Up @@ -420,7 +408,6 @@ void initFromMojo(AggregatorAddThirdPartyMojo mojo, MavenProject mavenProject, L
excludedLicenses = mojo.excludedLicenses;
bundleThirdPartyPath = mojo.bundleThirdPartyPath;
generateBundle = mojo.generateBundle;
force = mojo.force;
failIfWarning = mojo.failIfWarning;
failOnMissing = mojo.failOnMissing;
failOnBlacklist = mojo.failOnBlacklist;
Expand All @@ -432,11 +419,15 @@ void initFromMojo(AggregatorAddThirdPartyMojo mojo, MavenProject mavenProject, L

setLog(mojo.getLog());

isAggregatorBuild = true;
reactorProjectDependencies = reactorProjects;

// magic used by AggregatorAddThirdPartyMojo - only by build dependencies map for child projects
// so actions in init should be always executed
force = true;
init();

// consolidate should comply original flag
force = mojo.force;
consolidate();
}
}

0 comments on commit 85ac3b0

Please sign in to comment.