-
Notifications
You must be signed in to change notification settings - Fork 55
/
utils.go
312 lines (271 loc) · 10.4 KB
/
utils.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
package mvnutils
import (
"fmt"
"github.com/jfrog/jfrog-client-go/utils/version"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/spf13/viper"
)
const (
mavenExtractorDependencyVersion = "2.30.2"
classworldsConfFileName = "classworlds.conf"
mavenHome = "M2_HOME"
minSupportedMvnVersion = "3.1.0"
minSupportedMvnVersionError = "JFrog CLI mvn commands requires Maven version \"+minSupportedMvnVersion+\" or higher."
)
func RunMvn(configPath, deployableArtifactsFile string, buildConf *utils.BuildConfiguration, goals []string, threads int, insecureTls, disableDeploy bool) error {
log.Info("Running Mvn...")
mvnHome, err := getMavenHomeAndValidateVersion()
if err != nil {
return err
}
var dependenciesPath string
dependenciesPath, err = downloadDependencies()
if err != nil {
return err
}
mvnRunConfig, err := createMvnRunConfig(dependenciesPath, configPath, deployableArtifactsFile, mvnHome, buildConf, goals, threads, insecureTls, disableDeploy)
if err != nil {
return err
}
defer os.Remove(mvnRunConfig.buildInfoProperties)
return mvnRunConfig.runCmd()
}
func getMavenHomeAndValidateVersion() (string, error) {
log.Debug("Checking prerequisites.")
mvnHome := os.Getenv(mavenHome)
mvnVersion := ""
output, err := runMvnVersionCommand(mvnHome)
if err != nil {
return "", err
}
// Finding the relevant "Maven home" line in command response.
for _, line := range output {
if mvnHome == "" && strings.HasPrefix(line, "Maven home:") {
// The M2_HOME environment variable is not defined.
// Since Maven installation can be located in different locations,
// Depending on the installation type and the OS (for example: For Mac with brew install: /usr/local/Cellar/maven/{version}/libexec or Ubuntu with debian: /usr/share/maven),
mvnHome, err = parseMvnHome(line)
if err != nil {
return "", err
}
}
if strings.HasPrefix(line, "Apache Maven") {
mvnVersion = strings.Split(line, " ")[2]
}
}
if mvnHome == "" {
return "", errorutils.CheckErrorf("Could not find the location of the maven home directory, by running 'mvn --version' command. The command output is:\n" + strings.Join(output, " ") + "\nYou also have the option of setting the M2_HOME environment variable value to the maven installation directory, which is the directory which includes the bin and lib directories.")
}
if mvnVersion == "" {
log.Info("Could not get maven version, by running 'mvn --version' command. " + minSupportedMvnVersionError)
}
err = validateMinimumVersion(mvnVersion)
if err != nil {
return "", err
}
log.Debug("Maven home location: ", mvnHome)
log.Debug("Maven version: ", mvnVersion)
return mvnHome, nil
}
func runMvnVersionCommand(mavenHome string) ([]string, error) {
mvnPath := ""
var err error
if mavenHome != "" {
mvnPath = filepath.Join(mavenHome, "bin", "mvn")
} else {
mvnPath, err = exec.LookPath("mvn")
if err != nil || mvnPath == "" {
return nil, errorutils.CheckErrorf(err.Error() + "Hint: The mvn executable may not be included in the PATH. Either add it to the path, or set the M2_HOME environment variable value to the maven installation directory, which is the directory which includes the bin and lib directories.")
}
}
cmd := exec.Command(mvnPath, "--version")
output, err := cmd.Output()
if errorutils.CheckError(err) != nil {
return nil, err
}
return strings.Split(strings.TrimSpace(string(output)), "\n"), nil
}
func parseMvnHome(line string) (string, error) {
mavenHome := strings.Split(line, " ")[2]
if coreutils.IsWindows() {
mavenHome = strings.TrimSuffix(mavenHome, "\r")
}
mavenHome, err := filepath.Abs(mavenHome)
if err != nil {
return "", errorutils.CheckError(err)
}
return mavenHome, nil
}
func validateMinimumVersion(mvnVersion string) error {
ver := version.NewVersion(mvnVersion)
if ver.Compare(minSupportedMvnVersion) > 0 {
return errorutils.CheckErrorf("%s The Current version is: %s", minSupportedMvnVersionError, mvnVersion)
}
return nil
}
func downloadDependencies() (string, error) {
dependenciesPath, err := config.GetJfrogDependenciesPath()
if err != nil {
return "", err
}
dependenciesPath = filepath.Join(dependenciesPath, "maven", mavenExtractorDependencyVersion)
filename := fmt.Sprintf("build-info-extractor-maven3-%s-uber.jar", mavenExtractorDependencyVersion)
filePath := fmt.Sprintf("org/jfrog/buildinfo/build-info-extractor-maven3/%s", mavenExtractorDependencyVersion)
downloadPath := path.Join(filePath, filename)
err = utils.DownloadExtractorIfNeeded(downloadPath, filepath.Join(dependenciesPath, filename))
if err != nil {
return "", err
}
err = createClassworldsConfig(dependenciesPath)
return dependenciesPath, err
}
func createClassworldsConfig(dependenciesPath string) error {
classworldsPath := filepath.Join(dependenciesPath, classworldsConfFileName)
if fileutils.IsPathExists(classworldsPath, false) {
return nil
}
return errorutils.CheckError(ioutil.WriteFile(classworldsPath, []byte(utils.ClassworldsConf), 0644))
}
func createMvnRunConfig(dependenciesPath, configPath, deployableArtifactsFile, mavenHome string, buildConf *utils.BuildConfiguration, goals []string, threads int, insecureTls, disableDeploy bool) (*mvnRunConfig, error) {
var err error
var javaExecPath string
javaHome := os.Getenv("JAVA_HOME")
if javaHome != "" {
javaExecPath = filepath.Join(javaHome, "bin", "java")
} else {
javaExecPath, err = exec.LookPath("java")
if err != nil {
return nil, errorutils.CheckError(err)
}
}
plexusClassworlds, err := filepath.Glob(filepath.Join(mavenHome, "boot", "plexus-classworlds*.jar"))
if err != nil {
return nil, errorutils.CheckError(err)
}
mavenOpts := os.Getenv("MAVEN_OPTS")
if len(plexusClassworlds) < 1 {
return nil, errorutils.CheckErrorf("couldn't find plexus-classworlds-x.x.x.jar in Maven installation path, please check M2_HOME environment variable")
}
var currentWorkdir string
currentWorkdir, err = os.Getwd()
if err != nil {
return nil, errorutils.CheckError(err)
}
var vConfig *viper.Viper
if configPath == "" {
vConfig = viper.New()
vConfig.SetConfigType(string(utils.YAML))
vConfig.Set("type", utils.Maven.String())
} else {
vConfig, err = utils.ReadConfigFile(configPath, utils.YAML)
if err != nil {
return nil, err
}
}
if len(buildConf.BuildName) > 0 && len(buildConf.BuildNumber) > 0 {
vConfig.Set(utils.BuildName, buildConf.BuildName)
vConfig.Set(utils.BuildNumber, buildConf.BuildNumber)
vConfig.Set(utils.BuildProject, buildConf.Project)
err = utils.SaveBuildGeneralDetails(buildConf.BuildName, buildConf.BuildNumber, buildConf.Project)
if err != nil {
return nil, err
}
}
vConfig.Set(utils.InsecureTls, insecureTls)
if threads > 0 {
vConfig.Set(utils.ForkCount, threads)
}
if !vConfig.IsSet("deployer") {
setEmptyDeployer(vConfig)
}
if disableDeploy {
setDeployFalse(vConfig)
}
buildInfoProperties, err := utils.CreateBuildInfoPropertiesFile(buildConf.BuildName, buildConf.BuildNumber, buildConf.Project, deployableArtifactsFile, vConfig, utils.Maven)
if err != nil {
return nil, err
}
return &mvnRunConfig{
java: javaExecPath,
pluginDependencies: dependenciesPath,
plexusClassworlds: plexusClassworlds[0],
cleassworldsConfig: filepath.Join(dependenciesPath, classworldsConfFileName),
mavenHome: mavenHome,
workspace: currentWorkdir,
goals: goals,
buildInfoProperties: buildInfoProperties,
artifactoryResolutionEnabled: vConfig.IsSet("resolver"),
generatedBuildInfoPath: vConfig.GetString(utils.GeneratedBuildInfo),
mavenOpts: mavenOpts,
deployableArtifactsFilePath: vConfig.GetString(utils.DeployableArtifacts),
}, nil
}
func setEmptyDeployer(vConfig *viper.Viper) {
vConfig.Set(utils.DeployerPrefix+utils.DeployArtifacts, "false")
vConfig.Set(utils.DeployerPrefix+utils.Url, "http://empty_url")
vConfig.Set(utils.DeployerPrefix+utils.ReleaseRepo, "empty_repo")
vConfig.Set(utils.DeployerPrefix+utils.SnapshotRepo, "empty_repo")
}
func setDeployFalse(vConfig *viper.Viper) {
vConfig.Set(utils.DeployerPrefix+utils.DeployArtifacts, "false")
if vConfig.GetString(utils.DeployerPrefix+utils.Url) == "" {
vConfig.Set(utils.DeployerPrefix+utils.Url, "http://empty_url")
}
if vConfig.GetString(utils.DeployerPrefix+utils.ReleaseRepo) == "" {
vConfig.Set(utils.DeployerPrefix+utils.ReleaseRepo, "empty_repo")
}
if vConfig.GetString(utils.DeployerPrefix+utils.SnapshotRepo) == "" {
vConfig.Set(utils.DeployerPrefix+utils.SnapshotRepo, "empty_repo")
}
}
func (config *mvnRunConfig) GetCmd() *exec.Cmd {
var cmd []string
cmd = append(cmd, config.java)
cmd = append(cmd, "-classpath", config.plexusClassworlds)
cmd = append(cmd, "-Dmaven.home="+config.mavenHome)
cmd = append(cmd, "-DbuildInfoConfig.propertiesFile="+config.buildInfoProperties)
if config.artifactoryResolutionEnabled {
cmd = append(cmd, "-DbuildInfoConfig.artifactoryResolutionEnabled=true")
}
cmd = append(cmd, "-Dm3plugin.lib="+config.pluginDependencies)
cmd = append(cmd, "-Dclassworlds.conf="+config.cleassworldsConfig)
cmd = append(cmd, "-Dmaven.multiModuleProjectDirectory="+config.workspace)
if config.mavenOpts != "" {
cmd = append(cmd, strings.Split(config.mavenOpts, " ")...)
}
cmd = append(cmd, "org.codehaus.plexus.classworlds.launcher.Launcher")
cmd = append(cmd, config.goals...)
return exec.Command(cmd[0], cmd[1:]...)
}
type mvnRunConfig struct {
java string
plexusClassworlds string
cleassworldsConfig string
mavenHome string
pluginDependencies string
workspace string
pom string
goals []string
buildInfoProperties string
artifactoryResolutionEnabled bool
generatedBuildInfoPath string
mavenOpts string
deployableArtifactsFilePath string
}
func (config *mvnRunConfig) runCmd() error {
command := config.GetCmd()
command.Stderr = os.Stderr
command.Stdout = os.Stderr
return coreutils.ConvertExitCodeError(errorutils.CheckError(command.Run()))
}