-
Notifications
You must be signed in to change notification settings - Fork 594
/
maven.go
349 lines (298 loc) · 11 KB
/
maven.go
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
package maven
import (
"bytes"
"fmt"
"io"
"net/http"
"path/filepath"
"strings"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
)
// ExecuteOptions are used by Execute() to construct the Maven command line.
type ExecuteOptions struct {
PomPath string `json:"pomPath,omitempty"`
ProjectSettingsFile string `json:"projectSettingsFile,omitempty"`
GlobalSettingsFile string `json:"globalSettingsFile,omitempty"`
M2Path string `json:"m2Path,omitempty"`
Goals []string `json:"goals,omitempty"`
Defines []string `json:"defines,omitempty"`
Flags []string `json:"flags,omitempty"`
LogSuccessfulMavenTransfers bool `json:"logSuccessfulMavenTransfers,omitempty"`
ReturnStdout bool `json:"returnStdout,omitempty"`
}
// EvaluateOptions are used by Evaluate() to construct the Maven command line.
// In contrast to ExecuteOptions, fewer settings are required for Evaluate and thus a separate type is needed.
type EvaluateOptions struct {
PomPath string `json:"pomPath,omitempty"`
ProjectSettingsFile string `json:"projectSettingsFile,omitempty"`
GlobalSettingsFile string `json:"globalSettingsFile,omitempty"`
M2Path string `json:"m2Path,omitempty"`
Defines []string `json:"defines,omitempty"`
}
type mavenExecRunner interface {
Stdout(out io.Writer)
Stderr(err io.Writer)
RunExecutable(e string, p ...string) error
}
type mavenUtils interface {
piperutils.FileUtils
DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error
}
type utilsBundle struct {
*piperhttp.Client
*piperutils.Files
}
func newUtils() *utilsBundle {
return &utilsBundle{
Client: &piperhttp.Client{},
Files: &piperutils.Files{},
}
}
const mavenExecutable = "mvn"
// Execute constructs a mvn command line from the given options, and uses the provided
// mavenExecRunner to execute it.
func Execute(options *ExecuteOptions, command mavenExecRunner) (string, error) {
stdOutBuf, stdOut := evaluateStdOut(options)
command.Stdout(stdOut)
command.Stderr(log.Writer())
parameters, err := getParametersFromOptions(options, newUtils())
if err != nil {
return "", fmt.Errorf("failed to construct parameters from options: %w", err)
}
err = command.RunExecutable(mavenExecutable, parameters...)
if err != nil {
commandLine := append([]string{mavenExecutable}, parameters...)
return "", fmt.Errorf("failed to run executable, command: '%s', error: %w", commandLine, err)
}
if stdOutBuf == nil {
return "", nil
}
return string(stdOutBuf.Bytes()), nil
}
// Evaluate constructs ExecuteOptions for using the maven-help-plugin's 'evaluate' goal to
// evaluate a given expression from a pom file. This allows to retrieve the value of - for
// example - 'project.version' from a pom file exactly as Maven itself evaluates it.
func Evaluate(options *EvaluateOptions, expression string, command mavenExecRunner) (string, error) {
defines := []string{"-Dexpression=" + expression, "-DforceStdout", "-q"}
defines = append(defines, options.Defines...)
executeOptions := ExecuteOptions{
PomPath: options.PomPath,
M2Path: options.M2Path,
ProjectSettingsFile: options.ProjectSettingsFile,
GlobalSettingsFile: options.GlobalSettingsFile,
Goals: []string{"org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate"},
Defines: defines,
ReturnStdout: true,
}
value, err := Execute(&executeOptions, command)
if err != nil {
return "", err
}
if strings.HasPrefix(value, "null object or invalid expression") {
return "", fmt.Errorf("expression '%s' in file '%s' could not be resolved", expression, options.PomPath)
}
return value, nil
}
// InstallFile installs a maven artifact and its pom into the local maven repository.
// If "file" is empty, only the pom is installed. "pomFile" must not be empty.
func InstallFile(file, pomFile, m2Path string, command mavenExecRunner) error {
if len(pomFile) == 0 {
return fmt.Errorf("pomFile can't be empty")
}
var defines []string
if len(file) > 0 {
defines = append(defines, "-Dfile="+file)
if strings.Contains(file, ".jar") {
defines = append(defines, "-Dpackaging=jar")
}
if strings.Contains(file, "-classes") {
defines = append(defines, "-Dclassifier=classes")
}
} else {
defines = append(defines, "-Dfile="+pomFile)
}
defines = append(defines, "-DpomFile="+pomFile)
mavenOptionsInstall := ExecuteOptions{
Goals: []string{"install:install-file"},
Defines: defines,
M2Path: m2Path,
}
_, err := Execute(&mavenOptionsInstall, command)
if err != nil {
return fmt.Errorf("failed to install maven artifacts: %w", err)
}
return nil
}
// InstallMavenArtifacts finds maven modules (identified by pom.xml files) and installs the artifacts into the local maven repository.
func InstallMavenArtifacts(command mavenExecRunner, options EvaluateOptions) error {
return doInstallMavenArtifacts(command, options, newUtils())
}
func doInstallMavenArtifacts(command mavenExecRunner, options EvaluateOptions, utils mavenUtils) error {
err := flattenPom(command, options)
if err != nil {
return err
}
pomFiles, err := utils.Glob(filepath.Join("**", "pom.xml"))
if err != nil {
return err
}
// Ensure m2 path is an absolute path, even if it is given relative
// This is important to avoid getting multiple m2 directories in a maven multimodule project
if options.M2Path != "" {
options.M2Path, err = filepath.Abs(options.M2Path)
if err != nil {
return err
}
}
for _, pomFile := range pomFiles {
log.Entry().Info("Installing maven artifacts from module: " + pomFile)
// Set this module's pom file as the pom file for evaluating the packaging,
// otherwise we would evaluate the root pom in all iterations.
evaluateProjectPackagingOptions := options
evaluateProjectPackagingOptions.PomPath = pomFile
packaging, err := Evaluate(&evaluateProjectPackagingOptions, "project.packaging", command)
if err != nil {
return err
}
currentModuleDir := filepath.Dir(pomFile)
// Use flat pom if available to avoid issues with unresolved variables.
pathToPomFile := pomFile
flattenedPomExists, _ := utils.FileExists(filepath.Join(currentModuleDir, ".flattened-pom.xml"))
if flattenedPomExists {
pathToPomFile = filepath.Join(currentModuleDir, ".flattened-pom.xml")
}
if packaging == "pom" {
err = InstallFile("", pathToPomFile, options.M2Path, command)
if err != nil {
return err
}
} else {
err = installJarWarArtifacts(pathToPomFile, currentModuleDir, command, utils, options)
if err != nil {
return err
}
}
}
return err
}
func installJarWarArtifacts(pomFile, dir string, command mavenExecRunner, utils mavenUtils, options EvaluateOptions) error {
options.PomPath = filepath.Join(dir, "pom.xml")
finalName, err := Evaluate(&options, "project.build.finalName", command)
if err != nil {
return err
}
if finalName == "" {
log.Entry().Warn("project.build.finalName is empty, skipping install of artifact. Installing only the pom file.")
err = InstallFile("", pomFile, options.M2Path, command)
if err != nil {
return err
}
return nil
}
jarExists, _ := utils.FileExists(jarFile(dir, finalName))
warExists, _ := utils.FileExists(warFile(dir, finalName))
classesJarExists, _ := utils.FileExists(classesJarFile(dir, finalName))
originalJarExists, _ := utils.FileExists(originalJarFile(dir, finalName))
log.Entry().Infof("JAR file with name %s does exist: %t", jarFile(dir, finalName), jarExists)
log.Entry().Infof("Classes-JAR file with name %s does exist: %t", classesJarFile(dir, finalName), classesJarExists)
log.Entry().Infof("Original-JAR file with name %s does exist: %t", originalJarFile(dir, finalName), originalJarExists)
log.Entry().Infof("WAR file with name %s does exist: %t", warFile(dir, finalName), warExists)
// Due to spring's jar repackaging we need to check for an "original" jar file because the repackaged one is no suitable source for dependent maven modules
if originalJarExists {
err = InstallFile(originalJarFile(dir, finalName), pomFile, options.M2Path, command)
if err != nil {
return err
}
} else if jarExists {
err = InstallFile(jarFile(dir, finalName), pomFile, options.M2Path, command)
if err != nil {
return err
}
}
if warExists {
err = InstallFile(warFile(dir, finalName), pomFile, options.M2Path, command)
if err != nil {
return err
}
}
if classesJarExists {
err = InstallFile(classesJarFile(dir, finalName), pomFile, options.M2Path, command)
if err != nil {
return err
}
}
return nil
}
func jarFile(dir, finalName string) string {
return filepath.Join(dir, "target", finalName+".jar")
}
func classesJarFile(dir, finalName string) string {
return filepath.Join(dir, "target", finalName+"-classes.jar")
}
func originalJarFile(dir, finalName string) string {
return filepath.Join(dir, "target", finalName+".jar.original")
}
func warFile(dir, finalName string) string {
return filepath.Join(dir, "target", finalName+".war")
}
func flattenPom(command mavenExecRunner, o EvaluateOptions) error {
mavenOptionsFlatten := ExecuteOptions{
Goals: []string{"flatten:flatten"},
Defines: []string{"-Dflatten.mode=resolveCiFriendliesOnly"},
PomPath: "pom.xml",
M2Path: o.M2Path,
}
_, err := Execute(&mavenOptionsFlatten, command)
return err
}
func evaluateStdOut(options *ExecuteOptions) (*bytes.Buffer, io.Writer) {
var stdOutBuf *bytes.Buffer
stdOut := log.Writer()
if options.ReturnStdout {
stdOutBuf = new(bytes.Buffer)
stdOut = io.MultiWriter(stdOut, stdOutBuf)
}
return stdOutBuf, stdOut
}
func getParametersFromOptions(options *ExecuteOptions, utils mavenUtils) ([]string, error) {
var parameters []string
parameters, err := DownloadAndGetMavenParameters(options.GlobalSettingsFile, options.ProjectSettingsFile, utils, utils)
if err != nil {
return nil, err
}
if options.M2Path != "" {
parameters = append(parameters, "-Dmaven.repo.local="+options.M2Path)
}
if options.PomPath != "" {
parameters = append(parameters, "--file", options.PomPath)
}
if options.Flags != nil {
parameters = append(parameters, options.Flags...)
}
if options.Defines != nil {
parameters = append(parameters, options.Defines...)
}
if !options.LogSuccessfulMavenTransfers {
parameters = append(parameters, "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn")
}
parameters = append(parameters, "--batch-mode")
parameters = append(parameters, options.Goals...)
return parameters, nil
}
func GetTestModulesExcludes() []string {
return getTestModulesExcludes(newUtils())
}
func getTestModulesExcludes(utils mavenUtils) []string {
var excludes []string
exists, _ := utils.FileExists("unit-tests/pom.xml")
if exists {
excludes = append(excludes, "-pl", "!unit-tests")
}
exists, _ = utils.FileExists("integration-tests/pom.xml")
if exists {
excludes = append(excludes, "-pl", "!integration-tests")
}
return excludes
}