diff --git a/pkg/builder/builder_steps.go b/pkg/builder/builder_steps.go index 555befc46f..fe90b78d6b 100644 --- a/pkg/builder/builder_steps.go +++ b/pkg/builder/builder_steps.go @@ -47,9 +47,7 @@ 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) @@ -57,15 +55,14 @@ func GenerateProject(ctx *Context) error { 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 { @@ -76,7 +73,7 @@ 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:") @@ -84,16 +81,16 @@ func GenerateProject(ctx *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:"): 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) } diff --git a/pkg/builder/builder_steps_test.go b/pkg/builder/builder_steps_test.go index ad31eb9ec7..eebf28cf40 100644 --- a/pkg/builder/builder_steps_test.go +++ b/pkg/builder/builder_steps_test.go @@ -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) } diff --git a/pkg/builder/builder_utils.go b/pkg/builder/builder_utils.go index 1c5153d8da..493728b73e 100644 --- a/pkg/builder/builder_utils.go +++ b/pkg/builder/builder_utils.go @@ -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), } } diff --git a/pkg/builder/springboot/generator.go b/pkg/builder/springboot/generator.go index e107a51b0f..670c2134bb 100644 --- a/pkg/builder/springboot/generator.go +++ b/pkg/builder/springboot/generator.go @@ -34,9 +34,7 @@ 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) @@ -44,20 +42,14 @@ func GenerateProject(ctx *builder.Context) error { 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, @@ -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, @@ -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 @@ -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) } diff --git a/pkg/util/maven/maven_project.go b/pkg/util/maven/maven_project.go index e965677456..3c5aa4d00e 100644 --- a/pkg/util/maven/maven_project.go +++ b/pkg/util/maven/maven_project.go @@ -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{ @@ -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. // @@ -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()) -} diff --git a/pkg/util/maven/maven_project_types.go b/pkg/util/maven/maven_project_types.go new file mode 100644 index 0000000000..999b91ea51 --- /dev/null +++ b/pkg/util/maven/maven_project_types.go @@ -0,0 +1,129 @@ +/* +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 ( + "encoding/xml" +) + +// 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 []Dependency `xml:"dependencies>dependency,omitempty"` + Repositories []Repository `xml:"repositories>repository,omitempty"` + PluginRepositories []Repository `xml:"pluginRepositories>pluginRepository,omitempty"` +} + +// 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"` +} + +// DependencyManagement represent maven's dependency management block +type DependencyManagement struct { + Dependencies []Dependency `xml:"dependencies>dependency,omitempty"` +} + +// 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"` +} + +// 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"` +} + +// RepositoryPolicy -- +type RepositoryPolicy struct { + Enabled bool `xml:"enabled"` + UpdatePolicy string `xml:"updatePolicy,omitempty"` +} + +// Build -- +type Build struct { + Plugins []Plugin `xml:"plugins>plugin,omitempty"` +} + +// Plugin -- +type Plugin struct { + GroupID string `xml:"groupId"` + ArtifactID string `xml:"artifactId"` + Version string `xml:"version,omitempty"` + Executions []Execution `xml:"executions>execution,omitempty"` +} + +// Execution -- +type Execution struct { + ID string `xml:"id"` + Phase string `xml:"phase"` + Goals []string `xml:"goals>goal,omitempty"` +} + +// 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()) +} diff --git a/pkg/util/maven/maven_test.go b/pkg/util/maven/maven_test.go index c09485c466..ff2ce0d808 100644 --- a/pkg/util/maven/maven_test.go +++ b/pkg/util/maven/maven_test.go @@ -88,54 +88,46 @@ func TestPomGeneration(t *testing.T) { ArtifactID: "camel-k-integration", Version: "1.0.0", DependencyManagement: DependencyManagement{ - Dependencies: Dependencies{ - Dependencies: []Dependency{ - { - GroupID: "org.apache.camel", - ArtifactID: "camel-bom", - Version: "2.22.1", - Type: "pom", - Scope: "import", - }, - }, - }, - }, - Dependencies: Dependencies{ Dependencies: []Dependency{ { - GroupID: "org.apache.camel.k", - ArtifactID: "camel-k-runtime-jvm", - Version: "1.0.0", + GroupID: "org.apache.camel", + ArtifactID: "camel-bom", + Version: "2.22.1", + Type: "pom", + Scope: "import", }, }, }, - Repositories: Repositories{ - Repositories: []Repository{ - { - ID: "central", - URL: "https://repo.maven.apache.org/maven2", - Snapshots: RepositoryPolicy{ - Enabled: false, - }, - Releases: RepositoryPolicy{ - Enabled: true, - UpdatePolicy: "never", - }, + Dependencies: []Dependency{ + { + GroupID: "org.apache.camel.k", + ArtifactID: "camel-k-runtime-jvm", + Version: "1.0.0", + }, + }, + Repositories: []Repository{ + { + ID: "central", + URL: "https://repo.maven.apache.org/maven2", + Snapshots: RepositoryPolicy{ + Enabled: false, + }, + Releases: RepositoryPolicy{ + Enabled: true, + UpdatePolicy: "never", }, }, }, - PluginRepositories: PluginRepositories{ - Repositories: []Repository{ - { - ID: "central", - URL: "https://repo.maven.apache.org/maven2", - Snapshots: RepositoryPolicy{ - Enabled: false, - }, - Releases: RepositoryPolicy{ - Enabled: true, - UpdatePolicy: "never", - }, + PluginRepositories: []Repository{ + { + ID: "central", + URL: "https://repo.maven.apache.org/maven2", + Snapshots: RepositoryPolicy{ + Enabled: false, + }, + Releases: RepositoryPolicy{ + Enabled: true, + UpdatePolicy: "never", }, }, },