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
/
Copy pathexecMaven.groovy
184 lines (165 loc) · 8.07 KB
/
execMaven.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
/*-
* #%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%
*/
import hudson.AbortException
import io.wcm.devops.jenkins.pipeline.environment.EnvironmentConstants
import io.wcm.devops.jenkins.pipeline.managedfiles.ManagedFile
import io.wcm.devops.jenkins.pipeline.managedfiles.ManagedFileConstants
import io.wcm.devops.jenkins.pipeline.managedfiles.ManagedFileParser
import io.wcm.devops.jenkins.pipeline.model.PatternMatchable
import io.wcm.devops.jenkins.pipeline.shell.MavenCommandBuilderImpl
import io.wcm.devops.jenkins.pipeline.utils.ConfigConstants
import io.wcm.devops.jenkins.pipeline.utils.PatternMatcher
import io.wcm.devops.jenkins.pipeline.utils.logging.Logger
import io.wcm.devops.jenkins.pipeline.utils.resources.JsonLibraryResource
import net.sf.json.JSON
import org.jenkinsci.plugins.workflow.cps.DSL
/**
* Executes maven with the given configuration options inside the "maven" element.
* This step implements
* - auto lookup for global maven settings
* - auto lookup for local maven settings
* - auto lookup for NPMRC and NPM_CONFIG_USERCONFIG
* - auto lookup for BUNDLE_CONFIG
*
* @param config Configuration options for the step
*
* @return void, stdOut or stdErr
*/
Object call(Map config = null) {
config = config ?: [:]
Logger log = new Logger(this)
// retrieve the configuration and set defaults
// retrieve scm url via utility step
String scmUrl = getScmUrl(config)
// initialize the command builder
MavenCommandBuilderImpl commandBuilder = new MavenCommandBuilderImpl((DSL) steps, params)
commandBuilder.applyConfig(config)
// initialize the configuration files
List configFiles = []
// retrieve global settingsId
if (commandBuilder.getGlobalSettingsId() == null) {
// use autolookup for maven global settingsId
ManagedFile mavenGlobalSettingsManagedFile = autoLookupMavenSettings(ManagedFileConstants.GLOBAL_MAVEN_SETTINGS_PATH, scmUrl, log)
if (mavenGlobalSettingsManagedFile) {
commandBuilder.setGlobalSettingsId(mavenGlobalSettingsManagedFile.getId())
}
log.info("mavenGlobalSettings was null, result from autolookup: ", commandBuilder.getGlobalSettingsId())
}
// retrieve settingsId
if (commandBuilder.getSettingsId() == null) {
// use autolookup for maven global settingsId
ManagedFile mavenSettingsManagedFile = autoLookupMavenSettings(ManagedFileConstants.MAVEN_SETTINGS_PATH, scmUrl, log)
if (mavenSettingsManagedFile) {
commandBuilder.setSettingsId(mavenSettingsManagedFile.getId())
}
log.info("mavenSettings was null, result from autolookup: ", commandBuilder.getSettingsId())
}
// check if global maven settingsId were found during auto lookup and add them
if (commandBuilder.getGlobalSettingsId() != null) {
configFiles.push(configFile(fileId: commandBuilder.getGlobalSettingsId(), targetLocation: '', variable: ManagedFileConstants.GLOBAL_MAVEN__SETTINGS_ENV))
}
// check if local maven settingsId were found during auto lookup and add them
if (commandBuilder.getSettingsId() != null) {
configFiles.push(configFile(fileId: commandBuilder.getSettingsId(), targetLocation: "", variable: ManagedFileConstants.MAVEN_SETTING_ENV))
}
// add config file for NPM_CONFIG_USERCONFIG if defined
addManagedFile(log, scmUrl, ManagedFileConstants.NPM_CONFIG_USERCONFIG_PATH, ManagedFileConstants.NPM_CONFIG_USERCONFIG_ENV, configFiles)
// add config file for ruby
addManagedFile(log, scmUrl, ManagedFileConstants.BUNDLE_CONFIG_PATH, ManagedFileConstants.BUNDLE_CONFIG_ENV, configFiles)
Object result = void
configFileProvider(configFiles) {
// add global settingsId
if (commandBuilder.getGlobalSettingsId() != null) {
String path = env.getProperty(ManagedFileConstants.GLOBAL_MAVEN__SETTINGS_ENV)
commandBuilder.setGlobalSettings(path)
}
// add local settingsId
if (commandBuilder.getSettingsId() != null) {
String path = env.getProperty(ManagedFileConstants.MAVEN_SETTING_ENV)
commandBuilder.setSettings(path)
}
// build the command line
command = commandBuilder.build()
log.info("executing maven with: $command")
// execute the maven command
if (commandBuilder.returnStdout || commandBuilder.returnStatus) {
result = sh(script: command, returnStatus: commandBuilder.returnStatus, returnStdout: commandBuilder.returnStdout)
} else {
result = sh(command)
}
}
return result
}
/**
* Searches for a matching maven setting for the scmUrl in the provided json
*
* @param jsonPath Path to the json conaining configurations for managed files
* @param scmUrl The url of the used scm
* @return A found Managed file, or null
*/
ManagedFile autoLookupMavenSettings(String jsonPath, String scmUrl, Logger log) {
// load and parse the json
JsonLibraryResource jsonLibraryResource = new JsonLibraryResource(steps, jsonPath)
JSON managedFilesJson
try {
managedFilesJson = jsonLibraryResource.load()
} catch (AbortException ex) {
log.warn("Exception during loading '$jsonPath', it seems that you do not have a pipeline configuration present, skip parsing of managedfiles. " +
"Refer to https://github.com/wcm-io-devops/jenkins-pipeline-library/blob/master/docs/tutorial-setup.md for proper setup.")
return null
}
ManagedFileParser parser = new ManagedFileParser()
List<PatternMatchable> managedFiles = parser.parse(managedFilesJson)
// match the scmUrl against the parsed mangedFiles and get the best match
PatternMatcher matcher = new PatternMatcher()
return (ManagedFile) matcher.getBestMatch(scmUrl, managedFiles)
}
/**
* Searches for a managed file in the json from jsonPath by using the scmUrl for matching and adds the file
* to the provided configFiles object when a result was found.
*
* @param log Instance of the execMaven logger
* @param scmUrl The scm url of the current job
* @param jsonPath Path to the json containing configurations for managed files
* @param envVar The environment variable where the configFileProvider should store the path in
* @param configFiles List of config files where the found file has to be added
*/
void addManagedFile(Logger log, String scmUrl, String jsonPath, String envVar, List configFiles) {
// load and parse the json
JsonLibraryResource jsonLibraryResource = new JsonLibraryResource(steps, jsonPath)
JSON managedFilesJson
try {
managedFilesJson = jsonLibraryResource.load()
} catch (AbortException ex) {
log.warn("Exception during loading '$jsonPath', it seems that you do not have a pipeline configuration present, skip parsing of managedfiles. " +
"Refer to https://github.com/wcm-io-devops/jenkins-pipeline-library/blob/master/docs/tutorial-setup.md for proper setup.")
return
}
ManagedFileParser parser = new ManagedFileParser()
List<PatternMatchable> managedFiles = parser.parse(managedFilesJson)
PatternMatcher matcher = new PatternMatcher()
// match the scmUrl against the parsed mangedFiles and get the best match
PatternMatchable managedFile = matcher.getBestMatch(scmUrl, managedFiles)
// when a file was found add it to the configFiles
if (managedFile) {
log.info("Found managed file for env var '$envVar' with id: '${managedFile.id}', adding to provided config files")
configFiles.push(configFile(fileId: managedFile.getId(), targetLocation: "", variable: envVar))
}
}