You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by GitBox <gi...@apache.org> on 2018/09/11 16:08:47 UTC

[GitHub] nicolaferraro closed pull request #27: generate pom using go's xml encoder

nicolaferraro closed pull request #27: generate pom using go's xml encoder
URL: https://github.com/apache/camel-k/pull/27
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pkg/build/local/local_builder.go b/pkg/build/local/local_builder.go
index 17529b2..daa5483 100644
--- a/pkg/build/local/local_builder.go
+++ b/pkg/build/local/local_builder.go
@@ -19,17 +19,19 @@ package local
 
 import (
 	"context"
+	"encoding/xml"
+	"io/ioutil"
+	"time"
+
 	buildv1 "github.com/openshift/api/build/v1"
 	imagev1 "github.com/openshift/api/image/v1"
 	"github.com/operator-framework/operator-sdk/pkg/sdk"
 	"github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
 	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
-	"io/ioutil"
 	"k8s.io/api/core/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
-	"time"
 
 	build "github.com/apache/camel-k/pkg/build/api"
 	"github.com/apache/camel-k/pkg/util/kubernetes"
@@ -105,10 +107,26 @@ func (b *localBuilder) buildCycle(ctx context.Context) {
 
 func (b *localBuilder) execute(source build.BuildSource) (string, error) {
 
-	project := maven.Project{
-		GroupId:    "org.apache.camel.k.integration",
-		ArtifactId: "camel-k-integration",
-		Version:    "1.0.0",
+	project := maven.ProjectDefinition{
+		Project: maven.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",
+			GroupId:           "org.apache.camel.k.integration",
+			ArtifactId:        "camel-k-integration",
+			Version:           "1.0.0",
+			Dependencies: maven.Dependencies{
+				Dependencies: []maven.Dependency{
+					{
+						GroupId:    "org.apache.camel.k",
+						ArtifactId: "camel-k-runtime-jvm",
+						Version:    version.Version,
+					},
+				},
+			},
+		},
 		JavaSources: map[string]string{
 			"kamel/Routes.java": source.Code,
 		},
@@ -116,13 +134,6 @@ func (b *localBuilder) execute(source build.BuildSource) (string, error) {
 			"JAVA_MAIN_CLASS":    "org.apache.camel.k.jvm.Application",
 			"CAMEL_K_ROUTES_URI": "classpath:kamel.Routes",
 		},
-		Dependencies: []maven.Dependency{
-			{
-				GroupId:    "org.apache.camel.k",
-				ArtifactId: "camel-k-runtime-jvm",
-				Version:    version.Version,
-			},
-		},
 	}
 
 	tarFileName, err := maven.Build(project)
diff --git a/pkg/util/maven/maven.go b/pkg/util/maven/maven.go
index cb0d8f5..262f275 100644
--- a/pkg/util/maven/maven.go
+++ b/pkg/util/maven/maven.go
@@ -19,14 +19,16 @@ package maven
 
 import (
 	"archive/tar"
-	"github.com/pkg/errors"
-	"github.com/sirupsen/logrus"
+	"bytes"
+	"encoding/xml"
 	"io"
 	"io/ioutil"
 	"os"
 	"os/exec"
 	"path"
-	"strings"
+
+	"github.com/pkg/errors"
+	"github.com/sirupsen/logrus"
 )
 
 const (
@@ -35,7 +37,7 @@ const (
 )
 
 // Takes a project description and returns a binary tar with the built artifacts
-func Build(project Project) (string, error) {
+func Build(project ProjectDefinition) (string, error) {
 	buildDir, err := ioutil.TempDir("", buildDirPrefix)
 	if err != nil {
 		return "", errors.Wrap(err, "could not create temporary dir for maven source files")
@@ -84,13 +86,13 @@ func mavenExtraOptions() string {
 	return ""
 }
 
-func createTar(buildDir string, project Project) (string, error) {
+func createTar(buildDir string, project ProjectDefinition) (string, error) {
 	artifactDir, err := ioutil.TempDir("", artifactDirPrefix)
 	if err != nil {
 		return "", errors.Wrap(err, "could not create temporary dir for maven artifacts")
 	}
 
-	tarFileName := path.Join(artifactDir, project.ArtifactId+".tar")
+	tarFileName := path.Join(artifactDir, project.Project.ArtifactId+".tar")
 	tarFile, err := os.Create(tarFileName)
 	if err != nil {
 		return "", errors.Wrap(err, "cannot create tar file "+tarFileName)
@@ -98,7 +100,7 @@ func createTar(buildDir string, project Project) (string, error) {
 	defer tarFile.Close()
 
 	writer := tar.NewWriter(tarFile)
-	err = appendToTar(path.Join(buildDir, "target", project.ArtifactId+"-"+project.Version+".jar"), "", writer)
+	err = appendToTar(path.Join(buildDir, "target", project.Project.ArtifactId+"-"+project.Project.Version+".jar"), "", writer)
 	if err != nil {
 		return "", err
 	}
@@ -163,8 +165,12 @@ func appendToTar(filePath string, tarPath string, writer *tar.Writer) error {
 	return nil
 }
 
-func createMavenStructure(buildDir string, project Project) error {
-	err := writeFile(buildDir, "pom.xml", pomFileContent(project))
+func createMavenStructure(buildDir string, project ProjectDefinition) error {
+	pom, err := pomFileContent(project.Project)
+	if err != nil {
+		return err
+	}
+	err = writeFile(buildDir, "pom.xml", pom)
 	if err != nil {
 		return err
 	}
@@ -226,35 +232,17 @@ func envFileContent(env map[string]string) string {
 	return content
 }
 
-func pomFileContent(project Project) string {
-	basePom := `<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0"
-  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-  <modelVersion>4.0.0</modelVersion>
-
-  <groupId>` + project.GroupId + `</groupId>
-  <artifactId>` + project.ArtifactId + `</artifactId>
-  <version>` + project.Version + `</version>
-
-  <dependencies>
-    #dependencies#
-  </dependencies>
-
-</project>
-`
-	depStr := ""
-	for _, dep := range project.Dependencies {
-		depStr += "\t\t<dependency>"
-		depStr += "\t\t\t<groupId>" + dep.GroupId + "</groupId>"
-		depStr += "\t\t\t<artifactId>" + dep.ArtifactId + "</artifactId>"
-		if dep.Version != "" {
-			depStr += "\t\t\t<version>" + dep.Version + "</version>"
-		}
-		depStr += "\t\t</dependency>"
-		depStr += "\n"
+func pomFileContent(project Project) (string, error) {
+	w := &bytes.Buffer{}
+	w.WriteString(xml.Header)
+
+	e := xml.NewEncoder(w)
+	e.Indent("", "  ")
+
+	err := e.Encode(project)
+	if err != nil {
+		return "", err
 	}
 
-	pom := strings.Replace(basePom, "#dependencies#", depStr, 1)
-	return pom
+	return w.String(), nil
 }
diff --git a/pkg/util/maven/mavent_test.go b/pkg/util/maven/mavent_test.go
new file mode 100644
index 0000000..8a84910
--- /dev/null
+++ b/pkg/util/maven/mavent_test.go
@@ -0,0 +1,69 @@
+/*
+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"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+const expectedPom = `<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.camel.k.integration</groupId>
+  <artifactId>camel-k-integration</artifactId>
+  <version>1.0.0</version>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.camel.k</groupId>
+      <artifactId>camel-k-runtime-jvm</artifactId>
+      <version>1.0.0</version>
+    </dependency>
+  </dependencies>
+</project>`
+
+func TestPomGeneration(t *testing.T) {
+	project := 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",
+		GroupId:           "org.apache.camel.k.integration",
+		ArtifactId:        "camel-k-integration",
+		Version:           "1.0.0",
+		Dependencies: Dependencies{
+			Dependencies: []Dependency{
+				{
+					GroupId:    "org.apache.camel.k",
+					ArtifactId: "camel-k-runtime-jvm",
+					Version:    "1.0.0",
+				},
+			},
+		},
+	}
+
+	pom, err := pomFileContent(project)
+
+	assert.Nil(t, err)
+	assert.NotNil(t, pom)
+
+	assert.Equal(t, pom, expectedPom)
+}
diff --git a/pkg/util/maven/types.go b/pkg/util/maven/types.go
index 501f737..68796aa 100644
--- a/pkg/util/maven/types.go
+++ b/pkg/util/maven/types.go
@@ -17,18 +17,33 @@ limitations under the License.
 
 package maven
 
+import "encoding/xml"
+
+type ProjectDefinition struct {
+	Project     Project
+	JavaSources map[string]string
+	Resources   map[string]string
+	Env         map[string]string
+}
+
 type Project struct {
-	GroupId      string
-	ArtifactId   string
-	Version      string
-	Dependencies []Dependency
-	JavaSources  map[string]string
-	Resources    map[string]string
-	Env          map[string]string
+	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"`
+	Dependencies      Dependencies `xml:"dependencies"`
+}
+
+type Dependencies struct {
+	Dependencies []Dependency `xml:"dependency"`
 }
 
 type Dependency struct {
-	GroupId    string
-	ArtifactId string
-	Version    string
+	GroupId    string `xml:"groupId"`
+	ArtifactId string `xml:"artifactId"`
+	Version    string `xml:"version"`
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services