Skip to content

Commit

Permalink
chore(mavne): simplify maven model
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Jan 30, 2019
1 parent 92328ab commit 42dbe77
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 239 deletions.
17 changes: 7 additions & 10 deletions pkg/builder/builder_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,22 @@ func GenerateProject(ctx *Context) error {
// Repositories
//

ctx.Project.Repositories = maven.Repositories{
Repositories: make([]maven.Repository, 0, len(ctx.Request.Repositories)),
}
ctx.Project.Repositories = make([]maven.Repository, 0, len(ctx.Request.Repositories))

for i, r := range ctx.Request.Repositories {
repo := maven.NewRepository(r)
if repo.ID == "" {
repo.ID = fmt.Sprintf("repo-%03d", i)
}

ctx.Project.Repositories.Repositories = append(ctx.Project.Repositories.Repositories, repo)
ctx.Project.Repositories = append(ctx.Project.Repositories, repo)
}

//
// set-up dependencies
//

deps := &ctx.Project.Dependencies
deps.AddGAV("org.apache.camel.k", "camel-k-runtime-jvm", version.Version)
ctx.Project.AddDependencyGAV("org.apache.camel.k", "camel-k-runtime-jvm", version.Version)

for _, d := range ctx.Request.Dependencies {
switch {
Expand All @@ -76,24 +73,24 @@ func GenerateProject(ctx *Context) error {
artifactID = "camel-" + artifactID
}

deps.AddGAV("org.apache.camel", artifactID, "")
ctx.Project.AddDependencyGAV("org.apache.camel", artifactID, "")
case strings.HasPrefix(d, "camel-k:"):
artifactID := strings.TrimPrefix(d, "camel-k:")

if !strings.HasPrefix(artifactID, "camel-") {
artifactID = "camel-" + artifactID
}

deps.AddGAV("org.apache.camel.k", artifactID, version.Version)
ctx.Project.AddDependencyGAV("org.apache.camel.k", artifactID, version.Version)
case strings.HasPrefix(d, "mvn:"):
mid := strings.TrimPrefix(d, "mvn:")
gav := strings.Replace(mid, "/", ":", -1)

deps.AddEncodedGAV(gav)
ctx.Project.AddEncodedDependencyGAV(gav)
case strings.HasPrefix(d, "runtime:"):
artifactID := strings.Replace(d, "runtime:", "camel-k-runtime-", 1)

deps.AddGAV("org.apache.camel.k", artifactID, version.Version)
ctx.Project.AddDependencyGAV("org.apache.camel.k", artifactID, version.Version)
default:
return fmt.Errorf("unknown dependency type: %s", d)
}
Expand Down
28 changes: 14 additions & 14 deletions pkg/builder/builder_steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ func TestGenerateProject(t *testing.T) {

assert.Nil(t, err)

assert.Equal(t, 1, len(ctx.Project.DependencyManagement.Dependencies.Dependencies))
assert.Equal(t, "org.apache.camel", ctx.Project.DependencyManagement.Dependencies.Dependencies[0].GroupID)
assert.Equal(t, "camel-bom", ctx.Project.DependencyManagement.Dependencies.Dependencies[0].ArtifactID)
assert.Equal(t, "2.22.1", ctx.Project.DependencyManagement.Dependencies.Dependencies[0].Version)
assert.Equal(t, "pom", ctx.Project.DependencyManagement.Dependencies.Dependencies[0].Type)
assert.Equal(t, "import", ctx.Project.DependencyManagement.Dependencies.Dependencies[0].Scope)

assert.Equal(t, 2, len(ctx.Project.Repositories.Repositories))
assert.Equal(t, "apache-snapshots", ctx.Project.Repositories.Repositories[0].ID)
assert.False(t, ctx.Project.Repositories.Repositories[0].Releases.Enabled)
assert.True(t, ctx.Project.Repositories.Repositories[0].Snapshots.Enabled)
assert.Equal(t, "ops4j-snapshots", ctx.Project.Repositories.Repositories[1].ID)
assert.False(t, ctx.Project.Repositories.Repositories[1].Releases.Enabled)
assert.True(t, ctx.Project.Repositories.Repositories[1].Snapshots.Enabled)
assert.Equal(t, 1, len(ctx.Project.DependencyManagement.Dependencies))
assert.Equal(t, "org.apache.camel", ctx.Project.DependencyManagement.Dependencies[0].GroupID)
assert.Equal(t, "camel-bom", ctx.Project.DependencyManagement.Dependencies[0].ArtifactID)
assert.Equal(t, "2.22.1", ctx.Project.DependencyManagement.Dependencies[0].Version)
assert.Equal(t, "pom", ctx.Project.DependencyManagement.Dependencies[0].Type)
assert.Equal(t, "import", ctx.Project.DependencyManagement.Dependencies[0].Scope)

assert.Equal(t, 2, len(ctx.Project.Repositories))
assert.Equal(t, "apache-snapshots", ctx.Project.Repositories[0].ID)
assert.False(t, ctx.Project.Repositories[0].Releases.Enabled)
assert.True(t, ctx.Project.Repositories[0].Snapshots.Enabled)
assert.Equal(t, "ops4j-snapshots", ctx.Project.Repositories[1].ID)
assert.False(t, ctx.Project.Repositories[1].Releases.Enabled)
assert.True(t, ctx.Project.Repositories[1].Snapshots.Enabled)
}
20 changes: 8 additions & 12 deletions pkg/builder/builder_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,16 @@ func NewProject(ctx *Context) maven.Project {
Version: version.Version,
Properties: ctx.Request.Platform.Build.Properties,
DependencyManagement: maven.DependencyManagement{
Dependencies: maven.Dependencies{
Dependencies: []maven.Dependency{
{
GroupID: "org.apache.camel",
ArtifactID: "camel-bom",
Version: ctx.Request.Platform.Build.CamelVersion,
Type: "pom",
Scope: "import",
},
Dependencies: []maven.Dependency{
{
GroupID: "org.apache.camel",
ArtifactID: "camel-bom",
Version: ctx.Request.Platform.Build.CamelVersion,
Type: "pom",
Scope: "import",
},
},
},
Dependencies: maven.Dependencies{
Dependencies: make([]maven.Dependency, 0),
},
Dependencies: make([]maven.Dependency, 0),
}
}
23 changes: 8 additions & 15 deletions pkg/builder/springboot/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,22 @@ func GenerateProject(ctx *builder.Context) error {
// Repositories
//

ctx.Project.Repositories = maven.Repositories{
Repositories: make([]maven.Repository, 0, len(ctx.Request.Repositories)),
}
ctx.Project.Repositories = make([]maven.Repository, 0, len(ctx.Request.Repositories))

for i, r := range ctx.Request.Repositories {
repo := maven.NewRepository(r)
if repo.ID == "" {
repo.ID = fmt.Sprintf("repo-%03d", i)
}

ctx.Project.Repositories.Repositories = append(ctx.Project.Repositories.Repositories, repo)
ctx.Project.Repositories = append(ctx.Project.Repositories, repo)
}

//
// set-up dependencies
//

deps := &ctx.Project.Dependencies

//
// common
//

deps.Add(maven.Dependency{
ctx.Project.AddDependency(maven.Dependency{
GroupID: "org.apache.camel.k",
ArtifactID: "camel-k-runtime-spring-boot",
Version: version.Version,
Expand Down Expand Up @@ -96,7 +88,7 @@ func GenerateProject(ctx *builder.Context) error {
artifactID = "camel-" + artifactID
}

deps.Add(maven.Dependency{
ctx.Project.AddDependency(maven.Dependency{
GroupID: "org.apache.camel",
ArtifactID: artifactID + "-starter",
Version: ctx.Request.Platform.Build.CamelVersion,
Expand Down Expand Up @@ -132,12 +124,12 @@ func GenerateProject(ctx *builder.Context) error {
artifactID = "camel-" + artifactID
}

deps.AddGAV("org.apache.camel.k", artifactID, version.Version)
ctx.Project.AddDependencyGAV("org.apache.camel.k", artifactID, version.Version)
case strings.HasPrefix(d, "mvn:"):
mid := strings.TrimPrefix(d, "mvn:")
gav := strings.Replace(mid, "/", ":", -1)

deps.AddEncodedGAV(gav)
ctx.Project.AddEncodedDependencyGAV(gav)
case strings.HasPrefix(d, "runtime:"):
if d == "runtime:jvm" {
// common
Expand All @@ -149,8 +141,9 @@ func GenerateProject(ctx *builder.Context) error {
}

artifactID := strings.Replace(d, "runtime:", "camel-k-runtime-", 1)
dependency := maven.NewDependency("org.apache.camel.k", artifactID, version.Version)

deps.AddGAV("org.apache.camel.k", artifactID, version.Version)
ctx.Project.AddDependency(dependency)
default:
return fmt.Errorf("unknown dependency type: %s", d)
}
Expand Down
158 changes: 10 additions & 148 deletions pkg/util/maven/maven_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,84 +18,34 @@ limitations under the License.
package maven

import (
"encoding/xml"
"strings"
)

// Project represent a maven project
type Project struct {
XMLName xml.Name
XMLNs string `xml:"xmlns,attr"`
XMLNsXsi string `xml:"xmlns:xsi,attr"`
XsiSchemaLocation string `xml:"xsi:schemaLocation,attr"`
ModelVersion string `xml:"modelVersion"`
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
Version string `xml:"version"`
Properties Properties `xml:"properties,omitempty"`
DependencyManagement DependencyManagement `xml:"dependencyManagement"`
Dependencies Dependencies `xml:"dependencies"`
Repositories Repositories `xml:"repositories"`
PluginRepositories PluginRepositories `xml:"pluginRepositories"`
}

// DependencyManagement represent maven's dependency management block
type DependencyManagement struct {
Dependencies Dependencies `xml:"dependencies"`
}

// Dependencies --
type Dependencies struct {
Dependencies []Dependency `xml:"dependency"`
}

// Add a dependency to maven's dependencies
func (deps *Dependencies) Add(dep Dependency) {
for _, d := range deps.Dependencies {
// AddDependency 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 == dep {
return
}
}

deps.Dependencies = append(deps.Dependencies, dep)
p.Dependencies = append(p.Dependencies, dep)
}

// AddGAV a dependency to maven's dependencies
func (deps *Dependencies) AddGAV(groupID string, artifactID string, version string) {
deps.Add(NewDependency(groupID, artifactID, version))
// AddDependencyGAV a dependency to maven's dependencies
func (p *Project) AddDependencyGAV(groupID string, artifactID string, version string) {
p.AddDependency(NewDependency(groupID, artifactID, version))
}

// AddEncodedGAV a dependency to maven's dependencies
func (deps *Dependencies) AddEncodedGAV(gav string) {
// AddEncodedDependencyGAV a dependency to maven's dependencies
func (p *Project) AddEncodedDependencyGAV(gav string) {
if d, err := ParseGAV(gav); err == nil {
// TODO: error handling
deps.Add(d)
p.AddDependency(d)
}
}

// Exclusion represent a maven's dependency exlucsion
type Exclusion struct {
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
}

// Exclusions --
type Exclusions struct {
Exclusions []Exclusion `xml:"exclusion"`
}

// Dependency represent a maven's dependency
type Dependency struct {
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
Version string `xml:"version,omitempty"`
Type string `xml:"type,omitempty"`
Classifier string `xml:"classifier,omitempty"`
Scope string `xml:"scope,omitempty"`
Exclusions *Exclusions `xml:"exclusions,omitempty"`
}

// NewDependency create an new dependency from the given gav info
func NewDependency(groupID string, artifactID string, version string) Dependency {
return Dependency{
Expand All @@ -107,25 +57,6 @@ func NewDependency(groupID string, artifactID string, version string) Dependency
}
}

// Repositories --
type Repositories struct {
Repositories []Repository `xml:"repository"`
}

// PluginRepositories --
type PluginRepositories struct {
Repositories []Repository `xml:"pluginRepository"`
}

// Repository --
type Repository struct {
ID string `xml:"id"`
Name string `xml:"name,omitempty"`
URL string `xml:"url"`
Snapshots RepositoryPolicy `xml:"snapshots,omitempty"`
Releases RepositoryPolicy `xml:"releases,omitempty"`
}

//
// NewRepository parse the given repo url ang generated the related struct.
//
Expand Down Expand Up @@ -164,72 +95,3 @@ func NewRepository(repo string) Repository {

return r
}

// RepositoryPolicy --
type RepositoryPolicy struct {
Enabled bool `xml:"enabled"`
UpdatePolicy string `xml:"updatePolicy,omitempty"`
}

// Build --
type Build struct {
Plugins Plugins `xml:"plugins,omitempty"`
}

// Plugin --
type Plugin struct {
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
Version string `xml:"version,omitempty"`
Executions Executions `xml:"executions"`
}

// Plugins --
type Plugins struct {
Plugins []Plugin `xml:"plugin"`
}

// Execution --
type Execution struct {
ID string `xml:"id"`
Phase string `xml:"phase"`
Goals Goals `xml:"goals,omitempty"`
}

// Executions --
type Executions struct {
Executions []Execution `xml:"execution"`
}

// Goals --
type Goals struct {
Goals []string `xml:"goal"`
}

// Properties --
type Properties map[string]string

type propertiesEntry struct {
XMLName xml.Name
Value string `xml:",chardata"`
}

// MarshalXML --
func (m Properties) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if len(m) == 0 {
return nil
}

err := e.EncodeToken(start)
if err != nil {
return err
}

for k, v := range m {
if err := e.Encode(propertiesEntry{XMLName: xml.Name{Local: k}, Value: v}); err != nil {
return err
}
}

return e.EncodeToken(start.End())
}
Loading

0 comments on commit 42dbe77

Please sign in to comment.