Skip to content

Commit 8f0437e

Browse files
author
Maxime Michel
committed
Support withMaven step
1 parent a2186fa commit 8f0437e

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

libraries/maven/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ libraries {
4343
phases = ['build']
4444
artifacts = ['target/*.jar']
4545
}
46+
aThirdMavenStep {
47+
stageName = 'Maven Build'
48+
buildContainer = 'mvn'
49+
phases = ['build']
50+
artifacts = ['target/*.jar']
51+
withMavenParams = [
52+
'maven': 'Maven 3.9.6',
53+
'jdk': 'JDK8',
54+
'globalMavenSettingsConfig': '1c11594b-8b27-4f9e-a83c-3f2e72969514',
55+
'mavenOpts': '-Dsettings.security=/var/lib/jenkins/settings-security.xml',
56+
'traceability': false
57+
]
58+
}
4659
}
4760
}
4861
```

libraries/maven/steps/maven_invoke.groovy

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ void call(app_env = [:]) {
2828
libStepConfig?.artifacts ?:
2929
[] as String[]
3030

31+
def withMavenParams = appStepConfig?.withMavenParams ?:
32+
libStepConfig?.withMavenParams ?:
33+
[] as String[]
34+
3135
// Gather and set non-secret environment variables
3236
this.setEnvVars(libStepConfig, appStepConfig)
3337

@@ -46,7 +50,13 @@ void call(app_env = [:]) {
4650
phases.each { value -> command += "${value} " }
4751

4852
try {
49-
sh command
53+
if (withMavenParams) {
54+
withMaven (withMavenParams) {
55+
sh command
56+
}
57+
} else {
58+
sh command
59+
}
5060
}
5161
catch (any) {
5262
throw any

libraries/maven/test/MavenInvokeSpec.groovy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class MavenInvokeSpec extends JTEPipelineSpecification {
1717
]
1818
]
1919

20+
LinkedHashMap withMavenParams = [ 'maven': 'Maven 3.9.6', 'traceability': false ]
21+
2022
static class DummyException extends RuntimeException {
2123
DummyException(String _message) { super( _message ) }
2224
}
@@ -88,4 +90,23 @@ public class MavenInvokeSpec extends JTEPipelineSpecification {
8890
then:
8991
1 * getPipelineMock('archiveArtifacts.call')(_ as Map)
9092
}
93+
94+
def 'Command is wrapped with withMaven if withMavenParams are defined' () {
95+
setup:
96+
getPipelineMock('sh')('mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false && cd my-app')
97+
MavenInvoke.getBinding().setVariable('stepContext', [name: 'build'])
98+
MavenInvoke.getBinding().setVariable('config', [
99+
build: [
100+
stageName: 'Maven Build',
101+
buildContainer: 'mvn',
102+
phases: ['clean', 'install'],
103+
artifacts: ['target/*.jar'],
104+
withMavenParams: withMavenParams
105+
]
106+
])
107+
when:
108+
MavenInvoke()
109+
then:
110+
1 * getPipelineMock('withMaven').call(withMavenParams)
111+
}
91112
}

0 commit comments

Comments
 (0)