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 8, 2023
1 parent fe8643c commit 368ab6b
Showing 1 changed file with 79 additions and 63 deletions.
142 changes: 79 additions & 63 deletions repository/maven.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,91 @@
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() (path string, err error) {
dir, _ := os.Getwd()
path = pathlib.Join(dir, "settings.xml")
found, err := nas.Exists(path)
if found || err != nil {
return
}
f, err := os.Create(path)
if err != nil {
err = liberr.Wrap(
err,
"path",
path)
return
}
defer f.Close()
id, found, err := r.findIdentity("maven")
if err != nil {
return
}
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)
settingsXML, err = mxj.NewMapXml([]byte(id.Settings))
if err != nil {
err = liberr.Wrap(err)
return
}
// THis will use the settings in the idendity for settingsXML
err = r.injectProxy(settingsXML)
if err != nil {
return
}
}
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)
}
addon.Activity("[FILE] Created %s. -- file: %s", path, settings)
return
}

// Fetch fetches dependencies listed in the POM.
func (r *Maven) Fetch(sourceDir string) (err error) {
addon.Activity("[MVN] Fetch dependencies.")
Expand All @@ -32,7 +99,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 +111,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 +125,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 +138,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,10 +160,9 @@ 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()
settings, err := r.CreateSettingsFile()
if err != nil {
return
}
Expand All @@ -126,61 +188,17 @@ 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)
if found || err != nil {
return
}
f, err := os.Create(path)
if err != nil {
err = liberr.Wrap(
err,
"path",
path)
return
}
settings := id.Settings
settings, err = r.injectProxy(id)
func (r *Maven) injectCacheDir(settings mxj.Map) (err error) {
err = settings.SetValueForPath(r.M2Dir, "settings.localRepository")
if err != nil {
err = liberr.Wrap(err)
return
}
_, err = f.Write([]byte(settings))
if err != nil {
err = liberr.Wrap(
err,
"path",
path)
}
_ = f.Close()
addon.Activity("[FILE] Created %s.", path)
return
}

//
// 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))
if err != nil {
err = liberr.Wrap(err)
return
}
func (r *Maven) injectProxy(settingsXML mxj.Map) (err error) {
proxies, err := addon.Proxy.List()
if err != nil {
return
Expand Down Expand Up @@ -215,19 +233,17 @@ func (r *Maven) injectProxy(id *api.Identity) (s string, err error) {
if len(pList) == 0 {
return
}
v, err := m.ValuesForPath("settings.proxies.proxy")
v, err := settingsXML.ValuesForPath("settings.proxies.proxy")
if err != nil {
err = liberr.Wrap(err)
return
}
err = m.SetValueForPath(
err = settingsXML.SetValueForPath(
mxj.Map{"proxy": append(v, pList...)},
"settings.proxies")
if err != nil {
err = liberr.Wrap(err)
return
}
b, err := m.XmlIndent("", " ")
s = string(b)
return
}

0 comments on commit 368ab6b

Please sign in to comment.