Skip to content

Commit 80df758

Browse files
committed
Improve settings XML handling by removing external dependencies
- Remove Apache Camel-K dependencies from settingsxml.go - Add local Maven structs (Settings, Mirror, Server) with proper XML tags - Fix URL trimming logic to handle multiple trailing slashes - Add comprehensive test coverage with 11 test cases - Clean up unused dependencies in go.mod This change eliminates external dependencies while maintaining full functionality for Maven settings.xml management and improves reliability.
1 parent dc1157d commit 80df758

File tree

4 files changed

+515
-119
lines changed

4 files changed

+515
-119
lines changed

artifactory/utils/maven/settingsxml.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,45 @@ package maven
33
import (
44
"encoding/xml"
55
"fmt"
6-
mavenv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
7-
"github.com/apache/camel-k/v2/pkg/util/maven"
86
"os"
97
"path/filepath"
108
"strings"
119
)
1210

11+
// Settings represents the Maven settings.xml structure
12+
type Settings struct {
13+
XMLName xml.Name
14+
XMLNs string `xml:"xmlns,attr"`
15+
XMLNsXsi string `xml:"xmlns:xsi,attr"`
16+
XsiSchemaLocation string `xml:"xsi:schemaLocation,attr"`
17+
LocalRepository string `xml:"localRepository"`
18+
Servers []Server `xml:"servers>server,omitempty"`
19+
Mirrors []Mirror `xml:"mirrors>mirror,omitempty"`
20+
}
21+
22+
// Mirror represents a Maven mirror configuration
23+
type Mirror struct {
24+
ID string `xml:"id"`
25+
Name string `xml:"name,omitempty"`
26+
URL string `xml:"url"`
27+
MirrorOf string `xml:"mirrorOf"`
28+
}
29+
30+
// Server represents a Maven server configuration with credentials
31+
type Server struct {
32+
XMLName xml.Name `xml:"server"`
33+
ID string `xml:"id,omitempty"`
34+
Username string `xml:"username,omitempty"`
35+
Password string `xml:"password,omitempty"`
36+
}
37+
1338
// ArtifactoryMirrorID is the ID used for the Artifactory mirror.
1439
const ArtifactoryMirrorID = "artifactory-mirror"
1540

1641
// SettingsXmlManager manages the maven settings file (`settings.xml`).
1742
type SettingsXmlManager struct {
1843
path string
19-
settings maven.Settings
44+
settings Settings
2045
}
2146

2247
// NewSettingsXmlManager creates a new SettingsXmlManager instance.
@@ -45,7 +70,7 @@ func (sxm *SettingsXmlManager) loadSettings() error {
4570
if err != nil {
4671
if os.IsNotExist(err) {
4772
// If file does not exist, initialize with empty settings
48-
sxm.settings = maven.Settings{
73+
sxm.settings = Settings{
4974
XMLName: xml.Name{Local: "settings"},
5075
}
5176
return nil
@@ -82,11 +107,11 @@ func (sxm *SettingsXmlManager) ConfigureArtifactoryMirror(artifactoryUrl, repoNa
82107
// updateMirror finds the existing mirror or creates a new one and updates it with the provided details.
83108
func (sxm *SettingsXmlManager) updateMirror(artifactoryUrl, repoName string) error {
84109
// Create the new mirror with the provided details
85-
updatedMirror := maven.Mirror{
110+
updatedMirror := Mirror{
86111
ID: ArtifactoryMirrorID,
87112
Name: repoName,
88113
MirrorOf: "*",
89-
URL: strings.TrimSuffix(artifactoryUrl, "/") + "/" + repoName,
114+
URL: strings.TrimRight(artifactoryUrl, "/") + "/" + repoName,
90115
}
91116

92117
// Find if the mirror already exists
@@ -111,7 +136,7 @@ func (sxm *SettingsXmlManager) updateMirror(artifactoryUrl, repoName string) err
111136
// updateServerCredentials updates or adds server credentials in the settings.
112137
func (sxm *SettingsXmlManager) updateServerCredentials(username, password string) error {
113138
// Create the new server with the provided credentials
114-
updatedServer := mavenv1.Server{
139+
updatedServer := Server{
115140
ID: ArtifactoryMirrorID,
116141
Username: username,
117142
Password: password,

0 commit comments

Comments
 (0)