camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nferr...@apache.org
Subject [camel-k] 05/07: maven: refactor how dependencies are used
Date Thu, 20 Sep 2018 21:05:25 GMT
This is an automated email from the ASF dual-hosted git repository.

nferraro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit cad0003c6b589ad15da77b5affcb2e5a7bcfbc43
Author: lburgazzoli <lburgazzoli@gmail.com>
AuthorDate: Thu Sep 20 17:26:33 2018 +0200

    maven: refactor how dependencies are used
---
 pkg/apis/camel/v1alpha1/types.go                 |  8 +++-
 pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go | 18 ++++++-
 pkg/build/build_types.go                         |  6 ++-
 pkg/build/local/local_builder.go                 |  4 +-
 pkg/util/maven/maven.go                          | 60 +++++++++++-------------
 pkg/util/maven/maven_project.go                  | 52 ++++++++++++++++++++
 6 files changed, 109 insertions(+), 39 deletions(-)

diff --git a/pkg/apis/camel/v1alpha1/types.go b/pkg/apis/camel/v1alpha1/types.go
index ee89870..4dc5a0a 100644
--- a/pkg/apis/camel/v1alpha1/types.go
+++ b/pkg/apis/camel/v1alpha1/types.go
@@ -125,7 +125,7 @@ type IntegrationContext struct {
 // IntegrationContextSpec --
 type IntegrationContextSpec struct {
 	Dependencies  []string            `json:"dependencies,omitempty"`
-	Classpath     []string            `json:"classpath,omitempty"`
+	Classpath     []ClasspathEntry    `json:"classpath,omitempty"`
 	Configuration []ConfigurationSpec `json:"configuration,omitempty"`
 }
 
@@ -136,6 +136,12 @@ type IntegrationContextStatus struct {
 	Digest string                  `json:"digest,omitempty"`
 }
 
+// ClasspathEntry --
+type ClasspathEntry struct {
+	ID       string `json:"id" yaml:"id"`
+	Location string `json:"location,omitempty" yaml:"location,omitempty"`
+}
+
 // IntegrationContextPhase --
 type IntegrationContextPhase string
 
diff --git a/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
index dfd91cb..9f88f0e 100644
--- a/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
+++ b/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
@@ -26,6 +26,22 @@ import (
 )
 
 // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into
out. in must be non-nil.
+func (in *ClasspathEntry) DeepCopyInto(out *ClasspathEntry) {
+	*out = *in
+	return
+}
+
+// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClasspathEntry.
+func (in *ClasspathEntry) DeepCopy() *ClasspathEntry {
+	if in == nil {
+		return nil
+	}
+	out := new(ClasspathEntry)
+	in.DeepCopyInto(out)
+	return out
+}
+
+// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into
out. in must be non-nil.
 func (in *ConfigurationSpec) DeepCopyInto(out *ConfigurationSpec) {
 	*out = *in
 	return
@@ -140,7 +156,7 @@ func (in *IntegrationContextSpec) DeepCopyInto(out *IntegrationContextSpec)
{
 	}
 	if in.Classpath != nil {
 		in, out := &in.Classpath, &out.Classpath
-		*out = make([]string, len(*in))
+		*out = make([]ClasspathEntry, len(*in))
 		copy(*out, *in)
 	}
 	if in.Configuration != nil {
diff --git a/pkg/build/build_types.go b/pkg/build/build_types.go
index 0fd7309..ef48312 100644
--- a/pkg/build/build_types.go
+++ b/pkg/build/build_types.go
@@ -17,6 +17,8 @@ limitations under the License.
 
 package build
 
+import "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+
 // Request represent a request to build a specific code
 type Request struct {
 	Identifier   Identifier
@@ -30,7 +32,7 @@ type Identifier struct {
 	Qualifier string
 }
 
-// Request represent the integration code
+// Source represent the integration code
 type Source struct {
 	Name     string
 	Content  string
@@ -43,7 +45,7 @@ type Result struct {
 	Status    Status
 	Image     string
 	Error     error
-	Classpath []string
+	Classpath []v1alpha1.ClasspathEntry
 }
 
 // Builder is supertype of all builders
diff --git a/pkg/build/local/local_builder.go b/pkg/build/local/local_builder.go
index 364c244..232360b 100644
--- a/pkg/build/local/local_builder.go
+++ b/pkg/build/local/local_builder.go
@@ -90,7 +90,7 @@ func (b *localBuilder) buildCycle(ctx context.Context) {
 			if res.Error != nil {
 				logrus.Error("Error during build (total time ", elapsed.Seconds(), " seconds): ", res.Error)
 			} else {
-				logrus.Info("Build completed in ", elapsed.Seconds(), " seconds")
+				logrus.Info("Process completed in ", elapsed.Seconds(), " seconds")
 			}
 
 			op.output <- res
@@ -107,7 +107,7 @@ func (b *localBuilder) execute(request *build.Request) build.Result {
 		}
 	}
 
-	res, err := maven.Build(project)
+	res, err := maven.Process(project)
 	if err != nil {
 		return build.Result{
 			Error:  err,
diff --git a/pkg/util/maven/maven.go b/pkg/util/maven/maven.go
index d008162..6b9224e 100644
--- a/pkg/util/maven/maven.go
+++ b/pkg/util/maven/maven.go
@@ -19,9 +19,9 @@ package maven
 
 import (
 	"archive/tar"
-	"bufio"
 	"bytes"
 	"encoding/xml"
+	"fmt"
 	"io"
 	"io/ioutil"
 	"os"
@@ -30,6 +30,11 @@ import (
 	"regexp"
 	"strings"
 
+	"github.com/apache/camel-k/version"
+
+	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+	"gopkg.in/yaml.v1"
+
 	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
 )
@@ -42,11 +47,11 @@ const (
 // BuildResult --
 type BuildResult struct {
 	TarFilePath string
-	Classpath   []string
+	Classpath   []v1alpha1.ClasspathEntry
 }
 
-// Build takes a project description and returns a binary tar with the built artifacts
-func Build(project Project) (BuildResult, error) {
+// Process takes a project description and returns a binary tar with the built artifacts
+func Process(project Project) (BuildResult, error) {
 	res := BuildResult{}
 	buildDir, err := ioutil.TempDir("", buildDirPrefix)
 	if err != nil {
@@ -73,31 +78,30 @@ func Build(project Project) (BuildResult, error) {
 }
 
 func runMavenBuild(buildDir string, result *BuildResult) error {
-	// file where the classpath is listed
-	out := path.Join(buildDir, "integration.classpath")
-
-	cmd := exec.Command("mvn", mavenExtraOptions(), "-Dmdep.outputFile="+out, "dependency:build-classpath")
+	goal := fmt.Sprintf("org.apache.camel.k:camel-k-runtime-dependency-lister:%s:generate-dependency-list",
version.Version)
+	cmd := exec.Command("mvn", mavenExtraOptions(), goal)
 	cmd.Dir = buildDir
 	cmd.Stdout = os.Stdout
 	cmd.Stderr = os.Stderr
 
-	logrus.Infof("determine classpath: mvn: %v", cmd.Args)
+	logrus.Infof("determine classpath: %v", cmd.Args)
 	if err := cmd.Run(); err != nil {
 		return errors.Wrap(err, "failure while determining classpath")
 	}
 
-	lines, err := readLines(out)
+	dependencies := path.Join(buildDir, "target", "dependencies.yaml")
+	content, err := ioutil.ReadFile(dependencies)
 	if err != nil {
 		return err
 	}
 
-	result.Classpath = make([]string, 0)
-	for _, line := range lines {
-		for _, item := range strings.Split(line, string(os.PathListSeparator)) {
-			result.Classpath = append(result.Classpath, item)
-		}
+	cp := make(map[string][]v1alpha1.ClasspathEntry)
+	if err := yaml.Unmarshal(content, &cp); err != nil {
+		return err
 	}
 
+	result.Classpath = cp["dependencies"]
+
 	logrus.Info("Maven build completed successfully")
 	return nil
 }
@@ -126,8 +130,14 @@ func createTar(project Project, result *BuildResult) (string, error)
{
 	defer writer.Close()
 
 	cp := ""
-	for _, filePath := range result.Classpath {
-		fileName, err := appendFileToTar(filePath, "dependencies", writer)
+	for _, entry := range result.Classpath {
+		gav, err := ParseGAV(entry.ID)
+		if err != nil {
+			return "", nil
+		}
+
+		tarPath := path.Join("dependencies/", gav.GroupID)
+		fileName, err := appendFileToTar(entry.Location, tarPath, writer)
 		if err != nil {
 			return "", err
 		}
@@ -224,22 +234,6 @@ func writeFile(buildDir string, relativePath string, content string)
error {
 	return nil
 }
 
-//TODO: move to a file utility package
-func readLines(path string) ([]string, error) {
-	file, err := os.Open(path)
-	if err != nil {
-		return nil, err
-	}
-	defer file.Close()
-
-	var lines []string
-	scanner := bufio.NewScanner(file)
-	for scanner.Scan() {
-		lines = append(lines, scanner.Text())
-	}
-	return lines, scanner.Err()
-}
-
 // GeneratePomFileContent generate a pom.xml file from the given project definition
 func GeneratePomFileContent(project Project) (string, error) {
 	w := &bytes.Buffer{}
diff --git a/pkg/util/maven/maven_project.go b/pkg/util/maven/maven_project.go
index 0a6e2b8..95cf928 100644
--- a/pkg/util/maven/maven_project.go
+++ b/pkg/util/maven/maven_project.go
@@ -116,3 +116,55 @@ type Releases struct {
 	Enabled      bool   `xml:"enabled"`
 	UpdatePolicy string `xml:"updatePolicy"`
 }
+
+// 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"`
+}
+
+/*
+ <plugin>
+        <groupId>org.apache.camel.k</groupId>
+        <artifactId>camel-k-runtime-dependency-lister</artifactId>
+        <version>0.0.3-SNAPSHOT</version>
+        <executions>
+          <execution>
+            <id>generate-dependency-list</id>
+            <phase>initialize</phase>
+            <goals>
+              <goal>generate-dependency-list</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+*/


Mime
View raw message