From 97bc009cce27e43c901d51ee3d00a2389f64d3d8 Mon Sep 17 00:00:00 2001 From: Mukhtar Haji Date: Tue, 6 Jun 2017 11:22:23 +0100 Subject: [PATCH] Revert "Build project/func on host machine for java-maven" This reverts commit 4ccb8fa33c8a5dc94e7262c3e5595ee4bced3d0b. --- fn/langs/base.go | 6 +++++ fn/langs/java_maven.go | 54 ++++++++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/fn/langs/base.go b/fn/langs/base.go index 37b994568..eaf26b782 100644 --- a/fn/langs/base.go +++ b/fn/langs/base.go @@ -66,6 +66,7 @@ type LangHelper interface { type BaseHelper struct { } +<<<<<<< 1918edacff928f017aa215c925d1e454c43f6320 func (h *BaseHelper) BuildFromImage() string { return "" } func (h *BaseHelper) RunFromImage() string { return h.BuildFromImage() } func (h *BaseHelper) IsMultiStage() bool { return true } @@ -78,6 +79,11 @@ func (h *BaseHelper) PreBuild() error { return nil } func (h *BaseHelper) AfterBuild() error { return nil } func (h *BaseHelper) HasBoilerplate() bool { return false } func (h *BaseHelper) GenerateBoilerplate() error { return nil } +======= +func (h *BaseHelper) Cmd() string { return "" } +func (h *BaseHelper) HasBoilerplate() bool { return false } +func (h *BaseHelper) GenerateBoilerplate() error { return nil } +>>>>>>> Revert "Build project/func on host machine for java-maven" // exists checks if a file exists func exists(name string) bool { diff --git a/fn/langs/java_maven.go b/fn/langs/java_maven.go index 110adb14a..bf1e9cb47 100644 --- a/fn/langs/java_maven.go +++ b/fn/langs/java_maven.go @@ -1,31 +1,59 @@ package langs -import "fmt" +import ( + "fmt" + "os" + "os/exec" + "path/filepath" +) // JavaMavenLangHelper provides a set of helper methods for the build lifecycle of Java Maven projects type JavaMavenLangHelper struct { BaseHelper } + // Entrypoint returns the Java runtime Docker entrypoint that will be executed when the function is run func (lh *JavaMavenLangHelper) Entrypoint() string { - // TODO need mechanism to determine the java user function dynamically - userFunction := "com.example.faas.ExampleFunction::itsOn" - return fmt.Sprintf("java -jar /function/target/function.jar %s", userFunction) + return fmt.Sprintf("java -jar /function/target/function.jar com.example.faas.ExampleFunction::itsOn") } -func (lh *JavaMavenLangHelper) HasLocalBuildCmd() bool { +// HasPreBuild returns whether the Java runtime has a pre-build step +func (lh *JavaMavenLangHelper) HasPreBuild() bool { return true } -func (lh *JavaMavenLangHelper) LocalBuildCmd() []string { - return []string{ - "mvn clean package", +// PreBuild runs "mvn clean package" in the root of the project and by default expects a jar at `target/function.jar` as the +// output. We mount `$HOME/.m2` in order to get maven the proxy config specified in .m2/settings.xml, but it has the nice +// side effect of making the users .m2/repository available. Any new deps downloaded will be owned by root :( +func (lh *JavaMavenLangHelper) PreBuild() error { + wd, err := os.Getwd() + if err != nil { + return err } + + if !exists(filepath.Join(wd, "pom.xml")) { + return fmt.Errorf("Could not find pom.xml - are you sure this is a maven project?") + } + + cmd := exec.Command( + "docker", "run", + "--rm", + "-v", wd+":/java", "-w", "/java", + "-v", os.Getenv("HOME")+"/.m2:/.m2", + "maven:3.5-jdk-8-alpine", + "/bin/sh", "-c", "mvn -gs /.m2/settings.xml clean package", + ) + + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + if err := cmd.Run(); err != nil { + return dockerBuildError(err) + } + return nil } -func (lh *JavaMavenLangHelper) HasPreBuild() bool { return false } - -func (lh *JavaMavenLangHelper) PreBuild() error { return nil } - -func (lh *JavaMavenLangHelper) AfterBuild() error { return nil } +// AfterBuild should remove the (root-owned) target dir. +func (lh *JavaMavenLangHelper) AfterBuild() error { + return nil +}