Skip to content
This repository was archived by the owner on Sep 25, 2024. It is now read-only.

Commit 2ca003b

Browse files
Merge pull request #62 from wcm-io-devops/feature/support-return-values-in-execMaven
Add support for returnStdout and returnStatus in execMaven step
2 parents 215c3ae + a961901 commit 2ca003b

File tree

9 files changed

+207
-2
lines changed

9 files changed

+207
-2
lines changed

src/io/wcm/devops/jenkins/pipeline/shell/MavenCommandBuilderImpl.groovy

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class MavenCommandBuilderImpl implements Serializable, CommandBuilder, ConfigAwa
4040

4141
public String _globalSettingsId = null
4242
public String _settingsId = null
43+
public Boolean _returnStdout = false
44+
public Boolean _returnStatus = false
4345

4446
public CommandBuilder commandBuilder
4547

@@ -49,6 +51,8 @@ class MavenCommandBuilderImpl implements Serializable, CommandBuilder, ConfigAwa
4951

5052
public Logger log = new Logger(this)
5153

54+
55+
5256
/**
5357
* @param dsl The DSL object of the current pipeline script (available via this.steps in pipeline scripts)
5458
* @param executable The executable, default: 'maven'
@@ -245,6 +249,38 @@ class MavenCommandBuilderImpl implements Serializable, CommandBuilder, ConfigAwa
245249
}
246250

247251
/**
252+
* @return value of returnStdout
253+
*/
254+
@NonCPS
255+
Boolean getReturnStdout() {
256+
return _returnStdout
257+
}
258+
259+
/**
260+
* @return value of returnStdout
261+
*/
262+
@NonCPS
263+
Boolean getReturnStatus() {
264+
return _returnStatus
265+
}
266+
267+
/**
268+
* @param _returnStdout set returnStdout option
269+
*/
270+
@NonCPS
271+
void set_returnStdout(Boolean _returnStdout) {
272+
this._returnStdout = _returnStdout
273+
}
274+
275+
/**
276+
*
277+
* @param _returnStatus set returnStatus option
278+
*/
279+
@NonCPS
280+
void set_returnStatus(Boolean _returnStatus) {
281+
this._returnStatus = _returnStatus
282+
}
283+
/**
248284
* @param globalSettingsId The id of the global maven settings managed file
249285
*/
250286
@NonCPS
@@ -277,6 +313,8 @@ class MavenCommandBuilderImpl implements Serializable, CommandBuilder, ConfigAwa
277313
Object defines = mavenConfig[ConfigConstants.MAVEN_DEFINES] ?: [:]
278314
Boolean injectParameters = mavenConfig[ConfigConstants.MAVEN_INJECT_PARAMS] ?: false
279315
Object profiles = mavenConfig[ConfigConstants.MAVEN_PROFILES] ?: []
316+
_returnStatus = mavenConfig[ConfigConstants.MAVEN_RETURN_STATUS] ?: false
317+
_returnStdout = mavenConfig[ConfigConstants.MAVEN_RETURN_STDOUT] ?: false
280318

281319
if (mavenExecutable != null) {
282320
commandBuilder.setExecutable(mavenExecutable)

src/io/wcm/devops/jenkins/pipeline/utils/ConfigConstants.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class ConfigConstants {
6464
public static final String MAVEN_POM = "pom"
6565
public static final String MAVEN_PROFILES = "profiles"
6666
public static final String MAVEN_SETTINGS = "settings"
67+
public static final String MAVEN_RETURN_STDOUT = "returnStdout"
68+
public static final String MAVEN_RETURN_STATUS = "returnStatus"
6769

6870
public static final String MAVEN_PURGE_SNAPSHOTS = "purgeSnapShots"
6971
public static final String MAVEN_PURGE_SNAPSHOTS_REPO_PATH = "repoPath"

test/io/wcm/devops/jenkins/pipeline/shell/MavenCommandBuilderImplTest.groovy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import org.junit.Test
2424

2525
import static io.wcm.devops.jenkins.pipeline.utils.ConfigConstants.*
2626
import static org.junit.Assert.assertEquals
27+
import static org.junit.Assert.assertFalse
28+
import static org.junit.Assert.assertTrue
2729

2830
class MavenCommandBuilderImplTest extends DSLTestBase {
2931

@@ -124,12 +126,15 @@ class MavenCommandBuilderImplTest extends DSLTestBase {
124126
(MAVEN_POM) : "path with spaces/to/custom/pom.xml",
125127
(MAVEN_SETTINGS) : "settings-id",
126128
(MAVEN_PROFILES) : ["profile1", "profile2"],
129+
(MAVEN_RETURN_STATUS) : true
127130
]
128131
]
129132
underTest.applyConfig(config)
130133
assertEquals("path/to/custom/maven/bin/mvn -f path\\ with\\ spaces/to/custom/pom.xml clean install -B -U -DvalueDefine=value -DflagDefine -Pprofile1,profile2", underTest.build())
131134
assertEquals("settings-id", underTest.getSettingsId())
132135
assertEquals("global-settings-id", underTest.getGlobalSettingsId())
136+
assertFalse(underTest.getReturnStdout())
137+
assertTrue(underTest.getReturnStatus())
133138
assertEmptyAfterReset("path/to/custom/maven/bin/mvn")
134139
}
135140

@@ -145,12 +150,15 @@ class MavenCommandBuilderImplTest extends DSLTestBase {
145150
(MAVEN_POM) : "path with spaces/to/custom/pom.xml",
146151
(MAVEN_SETTINGS) : "settings-id",
147152
(MAVEN_PROFILES) : "profile3,profile4",
153+
(MAVEN_RETURN_STDOUT) : true
148154
]
149155
]
150156
underTest.applyConfig(config)
151157
assertEquals("path/to/custom/maven/bin/mvn -f path\\ with\\ spaces/to/custom/pom.xml clean install -B -U -DvalueDefine=value -DflagDefine -Pprofile3,profile4", underTest.build())
152158
assertEquals("settings-id", underTest.getSettingsId())
153159
assertEquals("global-settings-id", underTest.getGlobalSettingsId())
160+
assertTrue(underTest.getReturnStdout())
161+
assertFalse(underTest.getReturnStatus())
154162
assertEmptyAfterReset("path/to/custom/maven/bin/mvn")
155163
}
156164

test/io/wcm/testing/jenkins/pipeline/plugins/WorkflowDurableTaskStepPluginMock.groovy

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class WorkflowDurableTaskStepPluginMock {
4242
def shellMapCallback = { Map incomingCommand ->
4343
context.getStepRecorder().record(SH, incomingCommand)
4444
Boolean returnStdout = incomingCommand.returnStdout ?: false
45+
Boolean returnStatus = incomingCommand.returnStatus ?: false
4546
String script = incomingCommand.script ?: ""
4647
// return default values for several commands
4748
if (returnStdout) {
@@ -52,9 +53,18 @@ class WorkflowDurableTaskStepPluginMock {
5253
break
5354
case "git branch": return "* (detached from 0HFGC0)"
5455
break
56+
case "mvn -f path/to/returnStdout.xml": return "stdout from maven"
57+
break
5558
default: return ""
5659
}
5760
}
61+
else if (returnStatus) {
62+
switch (script) {
63+
case "mvn -f path/to/returnStatus.xml": return 42
64+
break
65+
default: return 0
66+
}
67+
}
5868
}
5969

6070
}

test/vars/execMaven/ExecMavenIT.groovy

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,34 @@ class ExecMavenIT extends LibraryIntegrationTestBase {
7474
assertOneShellCommand(expectedCommand)
7575
}
7676

77+
@Test
78+
void shouldExecuteMavenWithReturnStatus() {
79+
80+
expectedCommand = [
81+
script: 'mvn -f path/to/returnStatus.xml',
82+
returnStatus: true,
83+
returnStdout: false
84+
]
85+
Object result = loadAndExecuteScript("vars/execMaven/jobs/execMavenWithReturnStatus.groovy")
86+
87+
assertOneShellCommand(expectedCommand)
88+
assertEquals(42, result)
89+
}
90+
91+
@Test
92+
void shouldExecuteMavenWithReturnStdout() {
93+
94+
expectedCommand = [
95+
script: 'mvn -f path/to/returnStdout.xml',
96+
returnStatus: false,
97+
returnStdout: true
98+
]
99+
Object result = loadAndExecuteScript("vars/execMaven/jobs/execMavenWithReturnStdout.groovy")
100+
101+
assertOneShellCommand(expectedCommand)
102+
assertEquals("stdout from maven", result)
103+
}
104+
77105
@Test
78106
void shouldExecuteWithCustomConfigVariant2() {
79107

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*-
2+
* #%L
3+
* wcm.io
4+
* %%
5+
* Copyright (C) 2017 wcm.io DevOps
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package vars.execMaven.jobs
21+
22+
import static io.wcm.devops.jenkins.pipeline.utils.ConfigConstants.*
23+
24+
/**
25+
* Runs execMaven step with return status
26+
*
27+
* @return The script
28+
* @see vars.execMaven.ExecMavenIT
29+
*/
30+
def execute() {
31+
return execMaven(
32+
(SCM): [(SCM_URL): "https://subdomain.domain-new.tld/group/project1.git"],
33+
(MAVEN): [
34+
(MAVEN_POM) : "path/to/returnStatus.xml",
35+
(MAVEN_RETURN_STATUS): true,
36+
]
37+
)
38+
}
39+
40+
return this
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*-
2+
* #%L
3+
* wcm.io
4+
* %%
5+
* Copyright (C) 2017 wcm.io DevOps
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package vars.execMaven.jobs
21+
22+
import static io.wcm.devops.jenkins.pipeline.utils.ConfigConstants.*
23+
24+
/**
25+
* Runs execMaven step with return stdout
26+
*
27+
* @return The script
28+
* @see vars.execMaven.ExecMavenIT
29+
*/
30+
def execute() {
31+
return execMaven(
32+
(SCM): [(SCM_URL): "https://subdomain.domain-new.tld/group/project1.git"],
33+
(MAVEN): [
34+
(MAVEN_POM) : "path/to/returnStdout.xml",
35+
(MAVEN_RETURN_STDOUT): true,
36+
]
37+
)
38+
}
39+
40+
return this

vars/execMaven.groovy

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ import org.jenkinsci.plugins.workflow.cps.DSL
4040
* - auto lookup for BUNDLE_CONFIG
4141
*
4242
* @param config Configuration options for the step
43+
*
44+
* @return void, stdOut or stdErr
4345
*/
44-
void call(Map config = null) {
46+
Object call(Map config = null) {
4547
config = config ?: [:]
4648
Logger log = new Logger(this)
4749

@@ -92,6 +94,8 @@ void call(Map config = null) {
9294
// add config file for ruby
9395
addManagedFile(log, scmUrl, ManagedFileConstants.BUNDLE_CONFIG_PATH, ManagedFileConstants.BUNDLE_CONFIG_ENV, configFiles)
9496

97+
Object result = void
98+
9599
configFileProvider(configFiles) {
96100
// add global settingsId
97101
if (commandBuilder.getGlobalSettingsId() != null) {
@@ -110,8 +114,13 @@ void call(Map config = null) {
110114
log.info("executing maven with: $command")
111115

112116
// execute the maven command
113-
sh(command)
117+
if (commandBuilder.returnStdout || commandBuilder.returnStatus) {
118+
result = sh(script: command, returnStatus: commandBuilder.returnStatus, returnStdout: commandBuilder.returnStdout)
119+
} else {
120+
result = sh(command)
121+
}
114122
}
123+
return result
115124
}
116125

117126
/**

vars/execMaven.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ given configuration into a `sh` step call.
4646
* [`goals` (optional)](#goals-optional)
4747
* [`pom` (optional)](#pom-optional)
4848
* [`profiles` (optional)](#profiles-optional)
49+
* [`returnStatus` (optional)](#returnstatus-optional)
50+
* [`returnStdout` (optional)](#returnstdout-optional)
4951
* [`settings` (optional)](#settings-optional)
5052
* [Related classes](#related-classes)
5153

@@ -375,6 +377,8 @@ execMaven(
375377
(MAVEN_POM): "/path/to/pom.xml",
376378
(MAVEN_PROFILES): ["profile1", "profile2"],
377379
(MAVEN_SETTINGS): "managed-file-id",
380+
(MAVEN_RETURN_STATUS): false,
381+
(MAVEN_RETURN_STDOUT): false,
378382
]
379383
)
380384
```
@@ -480,6 +484,32 @@ current working directory
480484

481485
Maven profiles to use.
482486

487+
### `returnStatus` (optional)
488+
| ||
489+
|:---------|:-------------------------------------------------------------------------------------------------------|
490+
| Constant | [`ConfigConstants.MAVEN_SETTINGS`](../src/io/wcm/devops/jenkins/pipeline/utils/ConfigConstants.groovy) |
491+
| Type | `Boolean` |
492+
| Default | `false` |
493+
494+
When provided the `sh` step will be called with `returnStatus: true` and
495+
the `execMaven` step will return the status code of the shell command.
496+
497+
Please note that either `returnStdout` or `returnStatus` can be set to
498+
`true` due to the used `sh` step.
499+
500+
### `returnStdout` (optional)
501+
| ||
502+
|:---------|:-------------------------------------------------------------------------------------------------------|
503+
| Constant | [`ConfigConstants.MAVEN_SETTINGS`](../src/io/wcm/devops/jenkins/pipeline/utils/ConfigConstants.groovy) |
504+
| Type | `Boolean` |
505+
| Default | `false` |
506+
507+
When provided the `sh` step will be called with `returnStdout: true` and
508+
the `execMaven` step will return the stdout of the shell command.
509+
510+
Please note that either `returnStdout` or `returnStatus` can be set to
511+
`true` due to the used `sh` step.
512+
483513
### `settings` (optional)
484514
|||
485515
|---|---|

0 commit comments

Comments
 (0)