Skip to content

Commit

Permalink
add support for cache maven dependencies
Browse files Browse the repository at this point in the history
Signed-off-by: wanjunlei <[email protected]>
  • Loading branch information
wanjunlei committed Jun 25, 2023
1 parent 4bfe1b0 commit aed5bff
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
4 changes: 2 additions & 2 deletions builders/java/stack/build.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM ${from_image}
ARG cnb_uid=${CNB_USER_ID}
ENV cnb_gid=${CNB_GROUP_ID}

ARG maven_version=3.8.6
ARG maven_version=3.8.8
ARG gradle_version=7.4.2

COPY licenses/ /usr/local/share/licenses/buildpacks/
Expand All @@ -29,7 +29,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
unzip \
&& apt-get clean && rm -rf /var/lib/apt/lists/*

ADD https://downloads.apache.org/maven/maven-3/${maven_version}/binaries/apache-maven-${maven_version}-bin.tar.gz /
ADD https://dlcdn.apache.org/maven/maven-3/${maven_version}/binaries/apache-maven-${maven_version}-bin.tar.gz /
ADD https://services.gradle.org/distributions/gradle-${gradle_version}-bin.zip /

RUN mkdir /usr/local/maven && \
Expand Down
33 changes: 13 additions & 20 deletions cmd/java/functions_framework/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"path/filepath"
"strings"

"github.com/GoogleCloudPlatform/buildpacks/pkg/devmode"
"github.com/GoogleCloudPlatform/buildpacks/pkg/env"
gcp "github.com/GoogleCloudPlatform/buildpacks/pkg/gcpbuildpack"
"github.com/beevik/etree"
Expand All @@ -32,10 +31,11 @@ import (
const (
layerName = "functions-framework"

defaultMavenRepository = "https://repo.maven.apache.org/maven2/"
defaultFrameworkGroup = "dev.openfunction.functions"
defaultFrameworkArtifactID = "functions-framework-invoker"
defaultFrameworkVersion = "1.0.0"
defaultMavenRepository = "https://repo.maven.apache.org/maven2/"
defaultMavenSnapshotRepository = "https://s01.oss.sonatype.org/content/repositories/snapshots"
defaultFrameworkGroup = "dev.openfunction.functions"
defaultFrameworkArtifactID = "functions-framework-invoker"
defaultFrameworkVersion = "1.2.0"
)

func main() {
Expand Down Expand Up @@ -131,22 +131,12 @@ func classpath(ctx *gcp.Context) (string, error) {
// mavenClasspath determines the --classpath when there is a pom.xml. This will consist of the jar file built
// from the pom.xml itself, plus all jar files that are dependencies mentioned in the pom.xml.
func mavenClasspath(ctx *gcp.Context) (string, error) {

mvn := "mvn"

// If this project has the Maven Wrapper, we should use it
if ctx.FileExists("mvnw") {
mvn = "./mvnw"
}

command := []string{mvn, "--batch-mode", "dependency:copy-dependencies"}
if !ctx.Debug() && !devmode.Enabled(ctx) {
command = append(command, "--quiet")
}

// Copy the dependencies of the function (`<dependencies>` in pom.xml) into target/dependency.
ctx.Exec(command, gcp.WithUserAttribution)

// Extract the artifact/version coordinates from the user's pom.xml definitions.
// mvn help:evaluate is quite slow so we do it this way rather than calling it twice.
// The name of the built jar file will be <artifact>-<version>.jar, for example myfunction-0.9.jar.
Expand Down Expand Up @@ -217,11 +207,6 @@ func installFunctionsFramework(ctx *gcp.Context, layer *libcnb.Layer) error {
}
}

mavenRepository := os.Getenv(env.MavenRepository)
if mavenRepository == "" {
mavenRepository = defaultMavenRepository
}

frameworkGroup := os.Getenv(env.FunctionFrameworkGroup)
if frameworkGroup == "" {
frameworkGroup = defaultFrameworkGroup
Expand All @@ -238,6 +223,14 @@ func installFunctionsFramework(ctx *gcp.Context, layer *libcnb.Layer) error {
frameworkVersion = defaultFrameworkVersion
}

mavenRepository := os.Getenv(env.MavenRepository)
if mavenRepository == "" {
mavenRepository = defaultMavenRepository
if strings.HasSuffix(frameworkVersion, "-SNAPSHOT") {
mavenRepository = defaultMavenSnapshotRepository
}
}

artifact := fmt.Sprintf("%s-jar-with-dependencies.jar", frameworkArtifactID)
version := frameworkVersion
if strings.HasSuffix(frameworkVersion, "-SNAPSHOT") {
Expand Down
1 change: 0 additions & 1 deletion cmd/java/gradle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func detectFn(ctx *gcp.Context) (gcp.DetectResult, error) {
}

func buildFn(ctx *gcp.Context) error {

var gradle string
if ctx.FileExists("gradlew") {
gradle = "./gradlew"
Expand Down
13 changes: 12 additions & 1 deletion cmd/java/maven/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/GoogleCloudPlatform/buildpacks/pkg/devmode"
"github.com/GoogleCloudPlatform/buildpacks/pkg/env"
gcp "github.com/GoogleCloudPlatform/buildpacks/pkg/gcpbuildpack"
"github.com/GoogleCloudPlatform/buildpacks/pkg/java"
)

const (
Expand All @@ -51,6 +53,15 @@ func detectFn(ctx *gcp.Context) (gcp.DetectResult, error) {
}

func buildFn(ctx *gcp.Context) error {
m2CachedRepo := ctx.Layer(m2Layer, gcp.CacheLayer, gcp.LaunchLayerIfDevMode)
java.CheckCacheExpiration(ctx, m2CachedRepo)
homeM2 := filepath.Join(os.Getenv("HOME"), ".m2")
// Symlink the m2 layer into ~/.m2. If ~/.m2 already exists, delete it first.
// If it exists as a symlink, RemoveAll will remove the link, not anything it's linked to.
// We can't just use `-Dmaven.repo.local`. It does set the path to `m2/repo` but it fails
// to set the path to `m2/wrapper` which is used by mvnw to download Maven.
ctx.RemoveAll(homeM2)
ctx.Symlink(m2CachedRepo.Path, homeM2)

addJvmConfig(ctx)

Expand All @@ -69,7 +80,7 @@ func buildFn(ctx *gcp.Context) error {

ctx.Exec([]string{mvn, "-v"}, gcp.WithUserAttribution)

command := []string{mvn, "clean", "package", "--batch-mode", "-DskipTests", "-Dhttp.keepAlive=false"}
command := []string{mvn, "clean", "package", "dependency:copy-dependencies", "--batch-mode", "-DskipTests", "-Dhttp.keepAlive=false"}

if buildArgs := os.Getenv(env.BuildArgs); buildArgs != "" {
if strings.Contains(buildArgs, "maven.repo.local") {
Expand Down
2 changes: 1 addition & 1 deletion tools/create-builder.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ mkdir "$HOME"
echo "Extracting builder tar:"
tar xvf "$tar" -C "$temp"

pack create-builder "$name" --config="${temp}/builder.toml" --pull-policy=never
pack builder create "$name" --config="${temp}/builder.toml" --pull-policy=never
docker inspect --format='{{index .Id}}' "$name" > "$sha"

0 comments on commit aed5bff

Please sign in to comment.