qoomon / maven-git-versioning-extension

This extension will set project version, based on current Git branch or tag.
GNU General Public License v3.0
303 stars 87 forks source link

On Jenkins maven-git-versioning-extension runs 2x for module but on local dev machine only once? #238

Closed jethomas-tsi closed 1 year ago

jethomas-tsi commented 1 year ago

Hello, I have a strange problem with different behavior on my local development machine and the Jenkins build.

Specifics on my "special-case" - I have a multi-module project with a sub-module using the flatten-maven-plugin to create a BOM POM.

Locally when I run clean install it works fine...I see that the maven-git-versioning-extension runs in the 'validate' phase and the 'flatten-maven-plugin' in the 'prepare-package' phase. Everything is as expected.

...
[INFO] --- maven-git-versioning-extension:9.3.1:git-versioning (git-versioning) @ pwc-cm-bom ---
[INFO] 
... 
[INFO] --- flatten-maven-plugin:1.2.0:flatten (default-flatten) @ pwc-cm-bom ---
[INFO] Flattening POM file ...
[INFO] com.tsystems.pwc:pwc-cm-bom
[INFO] 
[INFO] Using flatten mode: bom
[INFO] Writing flattened POM file to 'C:\JWT\PRJ\PWC\REPO\pwc-webapp-pwcng\pwc-cm\pwc-cm-bom\pom.flattened.xml'.
[INFO] Updating current BOM model to flattened POM file.
...

But on Jenkins I see a second run of the maven-git-versioning-extension in every module.

12:18:47  [INFO] --- maven-git-versioning-extension:9.3.1:git-versioning (git-versioning) @ pwc-cm-bom ---
12:18:47  [INFO] 
...
12:18:47  [INFO] --- flatten-maven-plugin:1.2.0:flatten (default-flatten) @ pwc-cm-bom ---
12:18:47  [INFO] Flattening POM file ...
12:18:47  [INFO] com.tsystems.pwc:pwc-cm-bom
12:18:47  [INFO] 
12:18:47  [INFO] Using flatten mode: bom
12:18:47  [INFO] Writing flattened POM file to '/home/jenkins/workspace/v_PWCDEV_pwc-webapp-pwcng_master/pwc-cm/pwc-cm-bom/pom.flattened.xml'.
12:18:47  [INFO] Updating current BOM model to flattened POM file.
...
12:18:47  [INFO] 
12:18:47  [INFO] --- maven-git-versioning-extension:9.3.1:git-versioning (git-versioning) @ pwc-cm-bom ---

And then the build breaks with this error:

[ERROR] Failed to execute goal me.qoomon:maven-git-versioning-extension:9.3.1:git-versioning (git-versioning) on project pwc-cm-bom: Execution git-versioning of goal me.qoomon:maven-git-versioning-extension:9.3.1:git-versioning failed: Cannot invoke "org.apache.maven.model.Build.getPlugins()" because the return value of "org.apache.maven.model.Model.getBuild()" is null -> [Help 1]

NOTE: If I run Maven 'clean', 'install' and 'deploy' in separate Jenkins declarative pipeline stages the build runs OK. But, if I try to run a single Jenkins stage with 'mvn clean install deploy' it breaks because the maven-git-versioning-extension is running a 2nd time in the install phase.

Any suggestions?

JWT007 commented 1 year ago

In debug mode I see:

14:50:10  [2023-02-11T13:50:10.930Z] [INFO] --- maven-git-versioning-extension:9.6.1:git-versioning (git-versioning) @ pwc-cm-bom ---
14:50:10  [2023-02-11T13:50:10.930Z] [DEBUG] Configuring mojo me.qoomon:maven-git-versioning-extension:9.6.1:git-versioning from plugin realm ClassRealm[plugin>me.qoomon:maven-git-versioning-extension:9.6.1, parent: jdk.internal.loader.ClassLoaders$AppClassLoader@5cb0d902]
14:50:10  [2023-02-11T13:50:10.930Z] [DEBUG] Configuring mojo 'me.qoomon:maven-git-versioning-extension:9.6.1:git-versioning' with basic configurator -->
14:50:10  [2023-02-11T13:50:10.930Z] [DEBUG]   (f) project = MavenProject: com.tsystems.pwc:pwc-cm-bom:23.x-SNAPSHOT @ /home/jenkins/workspace/v_PWCDEV_pwc-webapp-pwcng_master/pwc-cm/pwc-cm-bom/pom.flattened.xml
14:50:10  [2023-02-11T13:50:10.930Z] [DEBUG] -- end configuration --
14:50:10  [2023-02-11T13:50:10.930Z] [DEBUG] remove version build plugin

And the following stacktrace:

14:50:10  [2023-02-11T13:50:10.931Z] [INFO] [jenkins-event-spy] Generated /home/jenkins/workspace/v_PWCDEV_pwc-webapp-pwcng_master@tmp/withMaven01356d2e/maven-spy-20230211-134644-16312063357735305290142.log
14:50:10  [2023-02-11T13:50:10.931Z] [ERROR] Failed to execute goal me.qoomon:maven-git-versioning-extension:9.6.1:git-versioning (git-versioning) on project pwc-cm-bom: Execution git-versioning of goal me.qoomon:maven-git-versioning-extension:9.6.1:git-versioning failed: Cannot invoke "org.apache.maven.model.Build.getPlugins()" because the return value of "org.apache.maven.model.Model.getBuild()" is null -> [Help 1]
14:50:10  [2023-02-11T13:50:10.931Z] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal me.qoomon:maven-git-versioning-extension:9.6.1:git-versioning (git-versioning) on project pwc-cm-bom: Execution git-versioning of goal me.qoomon:maven-git-versioning-extension:9.6.1:git-versioning failed: Cannot invoke "org.apache.maven.model.Build.getPlugins()" because the return value of "org.apache.maven.model.Model.getBuild()" is null
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:375)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:298)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
14:50:10  [2023-02-11T13:50:10.931Z]     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
14:50:10  [2023-02-11T13:50:10.931Z]     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77)
14:50:10  [2023-02-11T13:50:10.931Z]     at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
14:50:10  [2023-02-11T13:50:10.931Z]     at java.lang.reflect.Method.invoke (Method.java:568)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
14:50:10  [2023-02-11T13:50:10.931Z]     at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
14:50:10  [2023-02-11T13:50:10.932Z] Caused by: org.apache.maven.plugin.PluginExecutionException: Execution git-versioning of goal me.qoomon:maven-git-versioning-extension:9.6.1:git-versioning failed: Cannot invoke "org.apache.maven.model.Build.getPlugins()" because the return value of "org.apache.maven.model.Model.getBuild()" is null
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:148)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:370)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:298)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
14:50:10  [2023-02-11T13:50:10.932Z]     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
14:50:10  [2023-02-11T13:50:10.932Z]     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77)
14:50:10  [2023-02-11T13:50:10.932Z]     at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
14:50:10  [2023-02-11T13:50:10.932Z]     at java.lang.reflect.Method.invoke (Method.java:568)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
14:50:10  [2023-02-11T13:50:10.932Z] Caused by: java.lang.NullPointerException: Cannot invoke "org.apache.maven.model.Build.getPlugins()" because the return value of "org.apache.maven.model.Model.getBuild()" is null
14:50:10  [2023-02-11T13:50:10.932Z]     at me.qoomon.maven.gitversioning.GitVersioningMojo.execute (GitVersioningMojo.java:35)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:370)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:298)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
14:50:10  [2023-02-11T13:50:10.932Z]     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
14:50:10  [2023-02-11T13:50:10.932Z]     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77)
14:50:10  [2023-02-11T13:50:10.932Z]     at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
14:50:10  [2023-02-11T13:50:10.932Z]     at java.lang.reflect.Method.invoke (Method.java:568)
14:50:10  [2023-02-11T13:50:10.932Z]     at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
14:50:10  [2023-02-11T13:50:10.933Z]     at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
14:50:10  [2023-02-11T13:50:10.933Z]     at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
14:50:10  [2023-02-11T13:50:10.933Z]     at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
14:50:10  [2023-02-11T13:50:10.933Z] [ERROR] 
14:50:10  [2023-02-11T13:50:10.933Z] [ERROR] 
14:50:10  [2023-02-11T13:50:10.933Z] [ERROR] For more information about the errors and possible solutions, please read the following articles:
14:50:10  [2023-02-11T13:50:10.933Z] [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
14:50:10  [2023-02-11T13:50:10.933Z] [ERROR] 
14:50:10  [2023-02-11T13:50:10.933Z] [ERROR] After correcting the problems, you can resume the build with the command
14:50:10  [2023-02-11T13:50:10.933Z] [ERROR]   mvn <args> -rf :pwc-cm-bom
qoomon commented 1 year ago

How do you execute the maven command? by jenkins plugin or bi cli command?

JWT007 commented 1 year ago

Via Jenkinsfile:

steps {
                withMaven(
                        maven: 'maven',
                        mavenSettingsConfig: "${MAVEN_SETTINGS}",
                        options: [artifactsPublisher(disabled: true),
                                            findbugsPublisher(disabled: true),
                                            openTasksPublisher(disabled: true),
                                            junitPublisher(disabled: true),
                                            pipelineGraphPublisher(disabled: false, skipDownstreamTriggers: false),
                                            invokerPublisher(disabled: true)]
                ) {
                    sh "mvn clean install deploy -U -Pjenkins-build-pwcng"
                }
            }

What is strange is it works if I use 3 stages - one each for clean, install, and deploy. But if I put them all in one phase it doesn't.

Two reasons I am trying to get it down to one phase:

JWT007 commented 1 year ago

I am assuming there is some bad "mojo" between the flatten-maven-plugin and the git-versioning-extension when the maven-git-versioning-extension runs this 2nd time - but don't understand why it runs twice only on Jenkins.

Side Note: my development machine is Windows and the Jenkins servier Linux :/ but both using same Maven version.

JWT007 commented 1 year ago

Is there a null-check possibly missing here in GitVersioningMojo?

@Override
    public synchronized void execute() {
        File gitVersionedPomFile = new File(project.getBasedir(), GitVersioningModelProcessor.GIT_VERSIONING_POM_NAME);
        project.setPomFile(gitVersionedPomFile);

        getLog().debug("remove version build plugin");
        project.getModel().getBuild().getPlugins().remove(asPlugin());
        project.getOriginalModel().getBuild().getPlugins().remove(asPlugin());
    }

Because this generated "flattened" BOM (Bill-of-Materials) POM has no 'build' section (and logically no plugins/plugin-management).

From Stacktrace:

14:50:10  [2023-02-11T13:50:10.932Z] Caused by: java.lang.NullPointerException: Cannot invoke "org.apache.maven.model.Build.getPlugins()" because the return value of "org.apache.maven.model.Model.getBuild()" is null
14:50:10  [2023-02-11T13:50:10.932Z]     at me.qoomon.maven.gitversioning.GitVersioningMojo.execute (GitVersioningMojo.java:35)
qoomon commented 1 year ago

I'll add a null check, give me a second.

JWT007 commented 1 year ago

I made a test... this seems to work:

        Optional.of(project.getModel())
                .map(Model::getBuild)
                .map(Build::getPlugins)
                .ifPresent(pl -> pl.remove(asPlugin()));

        Optional.of(project.getOriginalModel())
                .map(Model::getBuild)
                .map(Build::getPlugins)
                .ifPresent(pl -> pl.remove(asPlugin()));

Thanks for your support on a Saturday!

JWT007 commented 1 year ago

So... I made a local fork of this, changed the version to 9.6.1.1, changed only the two lines above and pushed it to our Nexus Repo.

The maven part of the build now works. 👍

The full build is currently still broken because the SonarQube part of the pipeline only looks at maven central and can't resolve this custom version of the extension/plugin - so I am looking forward to hopefuly a positive review and a release 😄

NOTE: I did have a problem with one unit-test: `GitUtilTest#tagsPointAt_multipleTags' - the expected order didn't match the reverse-sort order from the tag-comparator.

I had to change to:

        // then
        assertThat(tags).containsExactly(givenTagName3, givenTagName2, givenTagName1);

Which I think makes sense because they were created in order 1,2,3 so reverse-sort is 3,2,1 - but please check 😉

qoomon commented 1 year ago

I've just released the fixed version 9.6.2

JWT007 commented 1 year ago

You are amazing! :) Thank you so much...this actually helps me a lot to get some stuff done this weekend!

JWT007 commented 1 year ago

One more quick question Bengt, how long do releases usally take before they show up on maven-central? :)

EDIT: The answer is the next day :)

qoomon commented 1 year ago

they usually show up in about a couple of hours, however releases are available to pull within 20 minutes I would say. They are just not visible at the maven.org UI.