From 7a99a63921ff3d8c20b8467065233201f59b94bd Mon Sep 17 00:00:00 2001 From: Maxime Michel Date: Tue, 17 Sep 2024 12:14:35 +0200 Subject: [PATCH] Support withMaven step As to why $MVN_CMD is used, see https://github.com/jenkinsci/pipeline-maven-plugin/blob/master/FAQ.adoc#how-to-use-the-pipeline-maven-plugin-with-docker --- libraries/maven/README.md | 11 +++++++++++ libraries/maven/steps/maven_invoke.groovy | 14 ++++++++++++-- libraries/maven/test/MavenInvokeSpec.groovy | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/libraries/maven/README.md b/libraries/maven/README.md index 0fa36d3a..0ea772b8 100644 --- a/libraries/maven/README.md +++ b/libraries/maven/README.md @@ -43,6 +43,17 @@ libraries { phases = ['build'] artifacts = ['target/*.jar'] } + aThirdMavenStep { + stageName = 'Maven Build' + buildContainer = 'mvn' + phases = ['build'] + artifacts = ['target/*.jar'] + withMavenParams = [ + 'globalMavenSettingsConfig': '1c11594b-8b27-4f9e-a83c-3f2e72969514', + 'mavenOpts': '-Dsettings.security=/var/lib/jenkins/settings-security.xml', + 'traceability': false + ] + } } } ``` diff --git a/libraries/maven/steps/maven_invoke.groovy b/libraries/maven/steps/maven_invoke.groovy index 1889d6e4..b36d05d0 100644 --- a/libraries/maven/steps/maven_invoke.groovy +++ b/libraries/maven/steps/maven_invoke.groovy @@ -28,6 +28,10 @@ void call(app_env = [:]) { libStepConfig?.artifacts ?: [] as String[] + def withMavenParams = appStepConfig?.withMavenParams ?: + libStepConfig?.withMavenParams ?: + [] as String[] + // Gather and set non-secret environment variables this.setEnvVars(libStepConfig, appStepConfig) @@ -40,13 +44,19 @@ void call(app_env = [:]) { inside_sdp_image "${env.buildContainer}", { unstash 'workspace' - String command = 'mvn ' + String command = withMavenParams ? '$MVN_CMD ' : 'mvn ' options.each { value -> command += "${value} " } goals.each { value -> command += "${value} " } phases.each { value -> command += "${value} " } try { - sh command + if (withMavenParams) { + withMaven (withMavenParams) { + sh command + } + } else { + sh command + } } catch (any) { throw any diff --git a/libraries/maven/test/MavenInvokeSpec.groovy b/libraries/maven/test/MavenInvokeSpec.groovy index f0f66180..34bdceca 100644 --- a/libraries/maven/test/MavenInvokeSpec.groovy +++ b/libraries/maven/test/MavenInvokeSpec.groovy @@ -17,6 +17,8 @@ public class MavenInvokeSpec extends JTEPipelineSpecification { ] ] + LinkedHashMap withMavenParams = [ 'traceability': false ] + static class DummyException extends RuntimeException { DummyException(String _message) { super( _message ) } } @@ -88,4 +90,23 @@ public class MavenInvokeSpec extends JTEPipelineSpecification { then: 1 * getPipelineMock('archiveArtifacts.call')(_ as Map) } + + def 'Command is wrapped with withMaven if withMavenParams are defined' () { + setup: + getPipelineMock('sh')('mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false && cd my-app') + MavenInvoke.getBinding().setVariable('stepContext', [name: 'build']) + MavenInvoke.getBinding().setVariable('config', [ + build: [ + stageName: 'Maven Build', + buildContainer: 'mvn', + phases: ['clean', 'install'], + artifacts: ['target/*.jar'], + withMavenParams: withMavenParams + ] + ]) + when: + MavenInvoke() + then: + 1 * getPipelineMock('withMaven').call(withMavenParams) + } }