-
Notifications
You must be signed in to change notification settings - Fork 56
/
mvn.go
270 lines (229 loc) · 7.52 KB
/
mvn.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
package mvn
import (
"errors"
"fmt"
gofrogcmd "github.com/jfrog/gofrog/io"
"github.com/jfrog/jfrog-cli-core/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/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"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
const mavenExtractorDependencyVersion = "2.17.0"
const classworldsConfFileName = "classworlds.conf"
const MavenHome = "M2_HOME"
type MvnCommand struct {
goals string
configPath string
insecureTls bool
configuration *utils.BuildConfiguration
rtDetails *config.ArtifactoryDetails
threads int
}
func NewMvnCommand() *MvnCommand {
return &MvnCommand{}
}
func (mc *MvnCommand) SetRtDetails(rtDetails *config.ArtifactoryDetails) *MvnCommand {
mc.rtDetails = rtDetails
return mc
}
func (mc *MvnCommand) SetConfiguration(configuration *utils.BuildConfiguration) *MvnCommand {
mc.configuration = configuration
return mc
}
func (mc *MvnCommand) SetConfigPath(configPath string) *MvnCommand {
mc.configPath = configPath
return mc
}
func (mc *MvnCommand) SetGoals(goals string) *MvnCommand {
mc.goals = goals
return mc
}
func (mc *MvnCommand) SetThreads(threads int) *MvnCommand {
mc.threads = threads
return mc
}
func (mc *MvnCommand) SetInsecureTls(insecureTls bool) *MvnCommand {
mc.insecureTls = insecureTls
return mc
}
func (mc *MvnCommand) Run() error {
log.Info("Running Mvn...")
err := validateMavenInstallation()
if err != nil {
return err
}
var dependenciesPath string
dependenciesPath, err = downloadDependencies()
if err != nil {
return err
}
mvnRunConfig, err := mc.createMvnRunConfig(dependenciesPath)
if err != nil {
return err
}
defer os.Remove(mvnRunConfig.buildInfoProperties)
return gofrogcmd.RunCmd(mvnRunConfig)
}
// Returns the ArtfiactoryDetails. The information returns from the config file provided.
func (mc *MvnCommand) RtDetails() (*config.ArtifactoryDetails, error) {
// Get the rtDetails from the config file.
var err error
if mc.rtDetails == nil {
vConfig, err := utils.ReadConfigFile(mc.configPath, utils.YAML)
if err != nil {
return nil, err
}
mc.rtDetails, err = utils.GetRtDetails(vConfig)
}
return mc.rtDetails, err
}
func (mc *MvnCommand) CommandName() string {
return "rt_maven"
}
func validateMavenInstallation() error {
log.Debug("Checking prerequisites.")
mavenHome := os.Getenv(MavenHome)
if mavenHome == "" {
return errorutils.CheckError(errors.New(MavenHome + " environment variable is not set"))
}
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 (mc *MvnCommand) createMvnRunConfig(dependenciesPath string) (*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)
}
}
mavenHome := os.Getenv("M2_HOME")
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.CheckError(errors.New("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
vConfig, err = utils.ReadConfigFile(mc.configPath, utils.YAML)
if err != nil {
return nil, err
}
if len(mc.configuration.BuildName) > 0 && len(mc.configuration.BuildNumber) > 0 {
vConfig.Set(utils.BUILD_NAME, mc.configuration.BuildName)
vConfig.Set(utils.BUILD_NUMBER, mc.configuration.BuildNumber)
err = utils.SaveBuildGeneralDetails(mc.configuration.BuildName, mc.configuration.BuildNumber)
if err != nil {
return nil, err
}
}
vConfig.Set(utils.INSECURE_TLS, mc.insecureTls)
if mc.threads > 0 {
vConfig.Set(utils.FORK_COUNT, mc.threads)
}
if !vConfig.IsSet("deployer") {
setEmptyDeployer(vConfig)
}
buildInfoProperties, err := utils.CreateBuildInfoPropertiesFile(mc.configuration.BuildName, mc.configuration.BuildNumber, 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: mc.goals,
buildInfoProperties: buildInfoProperties,
generatedBuildInfoPath: vConfig.GetString(utils.GENERATED_BUILD_INFO),
mavenOpts: mavenOpts,
}, nil
}
func setEmptyDeployer(vConfig *viper.Viper) {
vConfig.Set(utils.DEPLOYER_PREFIX+utils.DEPLOY_ARTIFACTS, "false")
vConfig.Set(utils.DEPLOYER_PREFIX+utils.URL, "http://empty_url")
vConfig.Set(utils.DEPLOYER_PREFIX+utils.RELEASE_REPO, "empty_repo")
vConfig.Set(utils.DEPLOYER_PREFIX+utils.SNAPSHOT_REPO, "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)
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, strings.Split(config.goals, " ")...)
return exec.Command(cmd[0], cmd[1:]...)
}
func (config *mvnRunConfig) GetEnv() map[string]string {
return map[string]string{}
}
func (config *mvnRunConfig) GetStdWriter() io.WriteCloser {
return nil
}
func (config *mvnRunConfig) GetErrWriter() io.WriteCloser {
return nil
}
type mvnRunConfig struct {
java string
plexusClassworlds string
cleassworldsConfig string
mavenHome string
pluginDependencies string
workspace string
pom string
goals string
buildInfoProperties string
generatedBuildInfoPath string
mavenOpts string
}