-
Notifications
You must be signed in to change notification settings - Fork 346
/
maven_project.go
231 lines (196 loc) · 6.07 KB
/
maven_project.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
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package maven
import (
"bytes"
"encoding/xml"
"strings"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
)
func NewProject() Project {
return Project{
XMLName: xml.Name{Local: "project"},
XMLNs: "http://maven.apache.org/POM/4.0.0",
XMLNsXsi: "http://www.w3.org/2001/XMLSchema-instance",
XsiSchemaLocation: "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd",
ModelVersion: "4.0.0",
}
}
func NewProjectWithGAV(group string, artifact string, version string) Project {
p := NewProject()
p.GroupID = group
p.ArtifactID = artifact
p.Version = version
p.Properties = v1.Properties{
"project.build.sourceEncoding": "UTF-8",
}
return p
}
func (p Project) Command(context Context) *Command {
return &Command{
context: context,
project: p,
}
}
func (p Project) MarshalBytes() ([]byte, error) {
w := &bytes.Buffer{}
w.WriteString(xml.Header)
e := xml.NewEncoder(w)
e.Indent("", " ")
if err := e.Encode(p); err != nil {
return []byte{}, err
}
return w.Bytes(), nil
}
func (p *Project) LookupDependency(dep Dependency) *Dependency {
for i := range p.Dependencies {
// Check if the given dependency is already included in the dependency list
if p.Dependencies[i].GroupID == dep.GroupID && p.Dependencies[i].ArtifactID == dep.ArtifactID {
return &p.Dependencies[i]
}
}
return nil
}
func (p *Project) ReplaceDependency(dep Dependency) {
for i, d := range p.Dependencies {
// Check if the given dependency is already included in the dependency list
if d.GroupID == dep.GroupID && d.ArtifactID == dep.ArtifactID {
p.Dependencies[i] = dep
return
}
}
}
// AddDependency adds a dependency to maven's dependencies.
func (p *Project) AddDependency(dep Dependency) {
for _, d := range p.Dependencies {
// Check if the given dependency is already included in the dependency list
if d.GroupID == dep.GroupID && d.ArtifactID == dep.ArtifactID {
return
}
}
p.Dependencies = append(p.Dependencies, dep)
}
// AddDependencies adds dependencies to maven's dependencies.
func (p *Project) AddDependencies(deps ...Dependency) {
for _, d := range deps {
p.AddDependency(d)
}
}
// AddDependencyGAV adds a dependency to maven's dependencies.
func (p *Project) AddDependencyGAV(groupID string, artifactID string, version string) {
p.AddDependency(NewDependency(groupID, artifactID, version))
}
// AddEncodedDependencyGAV adds a dependency in GAV format to maven's dependencies.
func (p *Project) AddEncodedDependencyGAV(gav string) {
if d, err := ParseGAV(gav); err == nil {
// TODO: error handling
p.AddDependency(d)
}
}
func (p *Project) AddDependencyExclusion(dep Dependency, exclusion Exclusion) {
if t := p.LookupDependency(dep); t != nil {
if t.Exclusions == nil {
exclusions := make([]Exclusion, 0)
t.Exclusions = &exclusions
}
for _, e := range *t.Exclusions {
if e.ArtifactID == exclusion.ArtifactID && e.GroupID == exclusion.GroupID {
return
}
}
*t.Exclusions = append(*t.Exclusions, exclusion)
}
}
func (p *Project) AddDependencyExclusions(dep Dependency, exclusions ...Exclusion) {
for _, e := range exclusions {
p.AddDependencyExclusion(dep, e)
}
}
func (p *Project) AddEncodedDependencyExclusion(gav string, exclusion Exclusion) {
if d, err := ParseGAV(gav); err == nil {
// TODO: error handling
p.AddDependencyExclusion(d, exclusion)
}
}
func (p *Project) AddProfiles(profiles string) {
p.Profiles = ProfilesContent{InnerXML: profiles}
}
// NewDependency creates an new dependency from the given GAV.
func NewDependency(groupID string, artifactID string, version string) Dependency {
return Dependency{
GroupID: groupID,
ArtifactID: artifactID,
Version: version,
Type: "",
Classifier: "",
}
}
// NewRepository parses the given repository URL and generates the corresponding v1.Repository.
//
// The repository can be customized by appending @param to the repository
// URL, e.g.:
//
// http://my-nexus:8081/repository/publicc@id=my-repo@snapshots
//
// That enables snapshots and sets the repository id to `my-repo`.
func NewRepository(repo string) v1.Repository {
r := v1.Repository{
URL: repo,
Releases: v1.RepositoryPolicy{
Enabled: true,
ChecksumPolicy: "fail",
},
Snapshots: v1.RepositoryPolicy{
Enabled: false,
ChecksumPolicy: "fail",
},
}
if idx := strings.Index(repo, "@"); idx != -1 {
r.URL = repo[:idx]
for _, attribute := range strings.Split(repo[idx+1:], "@") {
switch {
case attribute == "snapshots":
r.Snapshots.Enabled = true
case attribute == "noreleases":
r.Releases.Enabled = false
case strings.HasPrefix(attribute, "id="):
r.ID = attribute[3:]
case strings.HasPrefix(attribute, "name="):
r.Name = attribute[5:]
case strings.HasPrefix(attribute, "checksumpolicy="):
r.Snapshots.ChecksumPolicy = attribute[15:]
r.Releases.ChecksumPolicy = attribute[15:]
}
}
}
return r
}
func NewMirror(repo string) Mirror {
m := Mirror{}
if idx := strings.Index(repo, "@"); idx != -1 {
m.URL = repo[:idx]
for _, attribute := range strings.Split(repo[idx+1:], "@") {
switch {
case strings.HasPrefix(attribute, "mirrorOf="):
m.MirrorOf = attribute[9:]
case strings.HasPrefix(attribute, "id="):
m.ID = attribute[3:]
case strings.HasPrefix(attribute, "name="):
m.Name = attribute[5:]
}
}
}
return m
}