This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
MavenCommandBuilderImpl.groovy
416 lines (373 loc) · 10 KB
/
MavenCommandBuilderImpl.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*-
* #%L
* wcm.io
* %%
* Copyright (C) 2017 wcm.io DevOps
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package io.wcm.devops.jenkins.pipeline.shell
import com.cloudbees.groovy.cps.NonCPS
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import io.wcm.devops.jenkins.pipeline.utils.ConfigConstants
import io.wcm.devops.jenkins.pipeline.utils.logging.Logger
import org.jenkinsci.plugins.workflow.cps.DSL
/**
* Utility for building maven commands executed via the sh pipeline step
*/
@SuppressFBWarnings("RANGE_ARRAY_INDEX") // constructor marked with this issue, but there is no array, maybe a bug in findbugs
class MavenCommandBuilderImpl implements Serializable, CommandBuilder, ConfigAwareCommandBuilder {
private static final long serialVersionUID = 1L
static final String EXECUTABLE = "mvn"
static final String ARG_GLOBAL_SETTINGS = "--global-settings"
static final String ARG_SETTINGS = "--settings"
static final String ARG_FILE = "-f"
public String _globalSettingsId = null
public String _settingsId = null
public Boolean _returnStdout = false
public Boolean _returnStatus = false
public CommandBuilder commandBuilder
public DSL dsl
public Map _params
public Logger log = new Logger(this)
/**
* @param dsl The DSL object of the current pipeline script (available via this.steps in pipeline scripts)
* @param executable The executable, default: 'maven'
*/
MavenCommandBuilderImpl(DSL dsl, Map params = [:], String executable = null) {
commandBuilder = new CommandBuilderImpl(dsl, executable ?: EXECUTABLE)
this.dsl = dsl
this._params = params
this.reset()
}
/**
* Adds the global settings argument with the given path to the command line
* @param path Path to the global maven settings
*
* @return The current command builder instance
*/
@NonCPS
MavenCommandBuilderImpl setGlobalSettings(String path) {
this.addPathArgument(ARG_GLOBAL_SETTINGS, path)
return this
}
/**
* Sets maven profiles based upon the provided string
*
* @param profiles The maven profiles as string, comma separated
*
* @return MavenCommandBuilderImpl
*/
@NonCPS
MavenCommandBuilderImpl addProfiles(String profiles) {
if (profiles && profiles != "") {
this.addArgument("-P${profiles}")
}
return this
}
/**
* Sets the maven profiles based upon the list
* @param profiles The maven profiles as list
*
* @return The current command builder instance
*/
@NonCPS
MavenCommandBuilderImpl addProfiles(List<String> profiles) {
if (profiles.size() > 0) {
this.addProfiles(profiles.join(","))
}
return this
}
/**
* Sets the goals to be executed
*
* @param goals The Maven goals as string
* @return The current command builder instance
*/
@NonCPS
MavenCommandBuilderImpl setGoals(String goals) {
this.addArgument(goals)
return this
}
/**
* Sets the goals to be executed.
* Adapter function for goals that are provided as list.
*
* @param goals The maven goals as list
* @return The current command builder instance
*/
@NonCPS
MavenCommandBuilderImpl setGoals(List goals) {
if (goals.size() > 0) {
this.setGoals(goals.join(" "))
}
return this
}
/**
* Adds the settings argument with the given path to the command line
*
* @param path Path to the maven settings
* @return The current command builder instance
*/
@NonCPS
MavenCommandBuilderImpl setSettings(String path) {
this.addPathArgument(ARG_SETTINGS, path)
return this
}
/**
* Sets the path to the pom
*
* @param path The path to the pom file for maven
* @return The current command builder instance
*/
@NonCPS
MavenCommandBuilderImpl setPom(String path) {
this.addPathArgument(ARG_FILE, path)
return this
}
/**
* Adds a flag define to the maven command line
*
* @param defineName The define to be added (-D is automatically added)
* @return The current command builder instance
*/
@NonCPS
MavenCommandBuilderImpl addDefine(String defineName) {
if (defineName == null) return this
this.addArgument("-D$defineName".toString())
return this
}
/**
* Adds defined based on map input
* @param defines the defines as map
* @return The current command builder instance
*/
@NonCPS
@SuppressFBWarnings('SE_NO_SERIALVERSIONID')
MavenCommandBuilderImpl addDefines(Map defines) {
defines.each {
String k, v ->
if (v != null) {
this.addArgument("-D$k=$v".toString())
} else {
this.addArgument("-D$k".toString())
}
}
return this
}
/**
* Adds defines as string
*
* @param defines The defines to be added (each define must be prefixed with -D)
* @return The current command builder instance
*/
@NonCPS
MavenCommandBuilderImpl addDefines(String defines) {
this.addArguments(defines)
return this
}
/**
* Adds a value define to the maven command line
*
* @param defineName The name of the define (will be automatically prefixed with -D)
* @param defineValue The value of the define
* @return The current command builder instance
*/
@NonCPS
MavenCommandBuilderImpl addDefine(String defineName, Object defineValue) {
if (defineName == null) return this
if (defineValue == null) {
this.addDefine(defineName)
} else {
this.addArgument("-D${defineName}=${defineValue}".toString())
}
return this
}
/**
* {@inheritDoc}
*/
@Override
@NonCPS
CommandBuilder reset() {
this._globalSettingsId = null
this._settingsId = null
commandBuilder.reset()
return this
}
/**
*
* @return id of maven global settings managed file
*/
@NonCPS
String getGlobalSettingsId() {
return _globalSettingsId
}
/**
* @return id of the maven settings managed file
*/
@NonCPS
String getSettingsId() {
return _settingsId
}
/**
* @return value of returnStdout
*/
@NonCPS
Boolean getReturnStdout() {
return _returnStdout
}
/**
* @return value of returnStdout
*/
@NonCPS
Boolean getReturnStatus() {
return _returnStatus
}
/**
* @param _returnStdout set returnStdout option
*/
@NonCPS
void set_returnStdout(Boolean _returnStdout) {
this._returnStdout = _returnStdout
}
/**
*
* @param _returnStatus set returnStatus option
*/
@NonCPS
void set_returnStatus(Boolean _returnStatus) {
this._returnStatus = _returnStatus
}
/**
* @param globalSettingsId The id of the global maven settings managed file
*/
@NonCPS
void setGlobalSettingsId(String globalSettingsId) {
this._globalSettingsId = globalSettingsId
}
/**
* @param settingsId The id of the maven settings managed file
*/
@NonCPS
void setSettingsId(String settingsId) {
this._settingsId = settingsId
}
/**
* {@inheritDoc}
*/
@Override
@NonCPS
ConfigAwareCommandBuilder applyConfig(Map config) {
// parse config
Map mavenConfig = (Map) config[ConfigConstants.MAVEN] ?: [:]
String mavenExecutable = mavenConfig[ConfigConstants.MAVEN_EXECUTABLE] ?: null
String pom = mavenConfig[ConfigConstants.MAVEN_POM] ?: null
Object goals = mavenConfig[ConfigConstants.MAVEN_GOALS] ?: []
Object arguments = mavenConfig[ConfigConstants.MAVEN_ARGUMENTS] ?: []
globalSettingsId = mavenConfig[ConfigConstants.MAVEN_GLOBAL_SETTINGS] ?: null
settingsId = mavenConfig[ConfigConstants.MAVEN_SETTINGS] ?: null
Object defines = mavenConfig[ConfigConstants.MAVEN_DEFINES] ?: [:]
Boolean injectParameters = mavenConfig[ConfigConstants.MAVEN_INJECT_PARAMS] ?: false
Object profiles = mavenConfig[ConfigConstants.MAVEN_PROFILES] ?: []
_returnStatus = mavenConfig[ConfigConstants.MAVEN_RETURN_STATUS] ?: false
_returnStdout = mavenConfig[ConfigConstants.MAVEN_RETURN_STDOUT] ?: false
if (mavenExecutable != null) {
commandBuilder.setExecutable(mavenExecutable)
}
// set pom
this.setPom(pom)
// set goals
this.setGoals(goals)
// set arguments
this.addArguments(arguments)
// add defines to command builder
this.addDefines(defines)
if (injectParameters) {
this.addDefines(this._params)
}
// add profiles
this.addProfiles(profiles)
}
/**
* {@inheritDoc}
*/
@Override
@NonCPS
CommandBuilder setExecutable(String executable) {
commandBuilder.setExecutable(executable)
return this
}
/**
* {@inheritDoc}
*/
@Override
@NonCPS
CommandBuilder addArgument(String argument) {
commandBuilder.addArgument(argument)
return this
}
/**
* {@inheritDoc}
*/
@Override
@NonCPS
CommandBuilder addPathArgument(String argument) {
commandBuilder.addPathArgument(argument)
return this
}
/**
* {@inheritDoc}
*/
@Override
@NonCPS
CommandBuilder addPathArgument(String argumentName, String value) {
commandBuilder.addPathArgument(argumentName, value)
return this
}
/**
* {@inheritDoc}
*/
@Override
@NonCPS
CommandBuilder addArgument(String argumentName, String argumentValue) {
commandBuilder.addArgument(argumentName, argumentValue)
return this
}
/**
* {@inheritDoc}
*/
@Override
@NonCPS
String build() {
return commandBuilder.build()
}
/**
* {@inheritDoc}
*/
@Override
@NonCPS
CommandBuilder addArguments(String arguments) {
commandBuilder.addArguments(arguments)
return this
}
/**
* {@inheritDoc}
*/
@Override
@NonCPS
CommandBuilder addArguments(List<String> arguments) {
commandBuilder.addArguments(arguments)
return this
}
}