Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-7834] Fix NullPointerException in flatten-maven-plugin #1195

Merged
merged 3 commits into from
Jul 5, 2023

Conversation

gnodet
Copy link
Contributor

@gnodet gnodet commented Jul 4, 2023

JIRA issue: https://issues.apache.org/jira/browse/MNG-7834

The flatten-maven-plugin fails with a NPE:

[ERROR] Failed to execute goal org.codehaus.mojo:flatten-maven-plugin:1.5.0:flatten (default-cli) on project camel: failed to create a clean pom: Cannot invoke "java.util.List.stream()" 
because "dependencies" is null -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:flatten-maven-plugin:1.5.0:flatten (default-cli) on project camel: failed to create a clean 
pom
	at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2(MojoExecutor.java:341)
	at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute(MojoExecutor.java:324)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:174)
	at org.apache.maven.lifecycle.internal.MojoExecutor.access$000(MojoExecutor.java:77)
	at org.apache.maven.lifecycle.internal.MojoExecutor$1.run(MojoExecutor.java:162)
	at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute(DefaultMojosExecutionStrategy.java:39)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:159)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:114)
	at io.takari.maven.builder.smart.SmartBuilderImpl.buildProject(SmartBuilderImpl.java:204)
	at io.takari.maven.builder.smart.SmartBuilderImpl$ProjectBuildTask.run(SmartBuilderImpl.java:78)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:577)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
	at java.base/java.lang.Thread.run(Thread.java:1623)
Caused by: org.apache.maven.plugin.MojoExecutionException: failed to create a clean pom
	at org.codehaus.mojo.flatten.FlattenMojo.createFlattenedPom(FlattenMojo.java:556)
	at org.codehaus.mojo.flatten.FlattenMojo.execute(FlattenMojo.java:406)
	at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:143)
	at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2(MojoExecutor.java:336)
	... 15 common frames omitted
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "dependencies" is null
	at org.apache.maven.model.ModelBase.setDependencies(ModelBase.java:164)
	at org.codehaus.mojo.flatten.FlattenMojo.createCleanPom(FlattenMojo.java:686)
	at org.codehaus.mojo.flatten.FlattenMojo.createFlattenedPom(FlattenMojo.java:554)
	... 18 common frames omitted

@@ -201,18 +201,25 @@ public class ${class.name}
if (!Objects.equals(map, getDelegate().get${cap}())) {
update(getDelegate().with${cap}(map));
}
#else
#elseif ( $field.to != "String" && $field.type == "java.util.List" && $field.multiplicity == "*" )
if (${field.name} == null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the new code now looks like:

    public void setDependencies(List<Dependency> dependencies) {
        if (dependencies == null) {
            dependencies = Collections.emptyList();
        }
        if (!Objects.equals(dependencies, getDependencies())) {
            update(getDelegate().withDependencies(
                dependencies.stream().map(c -> c.getDelegate()).collect(Collectors.toList())));
            dependencies.forEach(e -> e.childrenTracking = this::replace);
        }
    }

#else
}
#elseif ( $field.to && $field.to != "String" )
if (!Objects.equals(${field.name}, ${pfx}${cap}())){
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new code looks like:

    public void setDistributionManagement(DistributionManagement distributionManagement) {
        if (!Objects.equals(distributionManagement, getDistributionManagement())){
            if (distributionManagement != null) {
                update(getDelegate().withDistributionManagement(distributionManagement.getDelegate()));
                distributionManagement.childrenTracking = this::replace;
            } else {
                update(getDelegate().withDistributionManagement(null));
            }
        }
    }

Copy link
Contributor

@elharo elharo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs tests

@gnodet gnodet force-pushed the MNG-7834-NPE-in-flatten-maven-plugin branch from 58f1361 to 43681d7 Compare July 4, 2023 12:38
@gnodet gnodet requested a review from elharo July 5, 2023 04:37
@gnodet gnodet merged commit bce40c7 into apache:master Jul 5, 2023
@gnodet gnodet added this to the 4.0.0-alpha-8 milestone Jul 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants