Skip to content

Commit

Permalink
adding ability for the Maven object to create the settings file with …
Browse files Browse the repository at this point in the history
…local repo set

Signed-off-by: Shawn Hurley <shawn@hurley.page>
  • Loading branch information
shawn-hurley committed Nov 6, 2023
1 parent fe8643c commit ebbfccf
Showing 1 changed file with 65 additions and 43 deletions.
108 changes: 65 additions & 43 deletions repository/maven.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
package repository

import (
"os"
pathlib "path"

"github.com/clbanning/mxj"
liberr "github.com/jortel/go-utils/error"
"github.com/konveyor/tackle2-addon/command"
"github.com/konveyor/tackle2-hub/api"
"github.com/konveyor/tackle2-hub/nas"
"os"
pathlib "path"
)

//
const emptySettings = `
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0
http://maven.apache.org/xsd/settings-1.2.0.xsd">
</settings>
`

// Maven repository.
type Maven struct {
Remote
BinDir string
M2Dir string
}

//
func (r *Maven) CreateSettingsFile() (string, error) {
return r.writeSettings()
}

// Fetch fetches dependencies listed in the POM.
func (r *Maven) Fetch(sourceDir string) (err error) {
addon.Activity("[MVN] Fetch dependencies.")
Expand All @@ -32,7 +43,6 @@ func (r *Maven) Fetch(sourceDir string) (err error) {
return
}

//
// FetchArtifact fetches an application artifact.
func (r *Maven) FetchArtifact(artifact string) (err error) {
addon.Activity("[MVN] Fetch artifact %s.", artifact)
Expand All @@ -45,7 +55,6 @@ func (r *Maven) FetchArtifact(artifact string) (err error) {
return
}

//
// InstallArtifacts installs application artifacts.
func (r *Maven) InstallArtifacts(sourceDir string) (err error) {
addon.Activity("[MVN] Install application.")
Expand All @@ -60,7 +69,6 @@ func (r *Maven) InstallArtifacts(sourceDir string) (err error) {
return
}

//
// DeleteArtifacts deletes application artifacts.
func (r *Maven) DeleteArtifacts(sourceDir string) (err error) {
addon.Activity("[MVN] Delete application artifacts.")
Expand All @@ -74,7 +82,6 @@ func (r *Maven) DeleteArtifacts(sourceDir string) (err error) {
return
}

//
// HasModules determines if the POM specifies modules.
func (r *Maven) HasModules(sourceDir string) (found bool, err error) {
pom := pathlib.Join(sourceDir, "pom.xml")
Expand All @@ -97,7 +104,6 @@ func (r *Maven) HasModules(sourceDir string) (found bool, err error) {
return
}

//
// run executes maven.
func (r *Maven) run(options command.Options) (err error) {
settings, err := r.writeSettings()
Expand Down Expand Up @@ -126,24 +132,11 @@ func (r *Maven) run(options command.Options) (err error) {
return
}

//
// writeSettings writes settings file.
func (r *Maven) writeSettings() (path string, err error) {
id, found, err := r.findIdentity("maven")
if err != nil {
return
}
if found {
addon.Activity(
"[MVN] Using credentials (id=%d) %s.",
id.ID,
id.Name)
} else {
return
}
dir, _ := os.Getwd()
path = pathlib.Join(dir, "settings.xml")
found, err = nas.Exists(path)
found, err := nas.Exists(path)
if found || err != nil {
return
}
Expand All @@ -155,35 +148,66 @@ func (r *Maven) writeSettings() (path string, err error) {
path)
return
}
settings := id.Settings
settings, err = r.injectProxy(id)
defer f.Close()
id, found, err := r.findIdentity("maven")
if err != nil {
return
}
_, err = f.Write([]byte(settings))
settingsXML, err := mxj.NewMapXml([]byte(emptySettings))
if err != nil {
err = liberr.Wrap(err)
return "", err
}
if found {
addon.Activity(
"[MVN] Using credentials (id=%d) %s.",
id.ID,
id.Name)
// THis will use the settings in the idendity for settingsXML
settingsXML, err = r.injectProxy(id)
if err != nil {
return
}
}
settingsXML, err = r.injectCacheDir(settingsXML)
if err != nil {
return
}
settings, err := settingsXML.XmlIndent("", " ")
if err != nil {
err = liberr.Wrap(err)
return "", err
}
_, err = f.Write(settings)
if err != nil {
err = liberr.Wrap(
err,
"path",
path)
}
_ = f.Close()
addon.Activity("[FILE] Created %s.", path)
addon.Activity("[FILE] Created %s. -- file: %s", path, settings)
return
}

//
func (r *Maven) injectCacheDir(settings mxj.Map) (mxj.Map, error) {
err := settings.SetValueForPath(r.M2Dir, "settings.localRepository")
if err != nil {
err = liberr.Wrap(err)
return nil, err
}
return settings, nil
}

// injectProxy injects proxy settings.
func (r *Maven) injectProxy(id *api.Identity) (s string, err error) {
s = id.Settings
m, err := mxj.NewMapXml([]byte(id.Settings))
func (r *Maven) injectProxy(id *api.Identity) (mxj.Map, error) {
settings, err := mxj.NewMapXml([]byte(id.Settings))
if err != nil {
err = liberr.Wrap(err)
return
return settings, err
}
proxies, err := addon.Proxy.List()
if err != nil {
return
return settings, err
}
pList := []interface{}{}
for _, p := range proxies {
Expand All @@ -205,29 +229,27 @@ func (r *Maven) injectProxy(id *api.Identity) (s string, err error) {
pid, idErr := addon.Identity.Get(p.Identity.ID)
if idErr != nil {
err = idErr
return
return settings, err
}
mp["username"] = pid.User
mp["password"] = pid.Password
}
pList = append(pList, mp)
}
if len(pList) == 0 {
return
return settings, nil
}
v, err := m.ValuesForPath("settings.proxies.proxy")
v, err := settings.ValuesForPath("settings.proxies.proxy")
if err != nil {
err = liberr.Wrap(err)
return
return settings, err
}
err = m.SetValueForPath(
err = settings.SetValueForPath(
mxj.Map{"proxy": append(v, pList...)},
"settings.proxies")
if err != nil {
err = liberr.Wrap(err)
return
return settings, err
}
b, err := m.XmlIndent("", " ")
s = string(b)
return
return settings, nil
}

0 comments on commit ebbfccf

Please sign in to comment.