Skip to content

Commit

Permalink
Support withMaven step
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Michel committed Sep 23, 2024
1 parent a2186fa commit 7a99a63
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
11 changes: 11 additions & 0 deletions libraries/maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
}
}
}
```
Expand Down
14 changes: 12 additions & 2 deletions libraries/maven/steps/maven_invoke.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions libraries/maven/test/MavenInvokeSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class MavenInvokeSpec extends JTEPipelineSpecification {
]
]

LinkedHashMap withMavenParams = [ 'traceability': false ]

static class DummyException extends RuntimeException {
DummyException(String _message) { super( _message ) }
}
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 7a99a63

Please sign in to comment.