Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distribute binaries #161

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions buildpack.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ api = "0.7"
default = "false"
description = "use maven daemon"
name = "BP_MAVEN_DAEMON_ENABLED"

[[metadata.configurations]]
build = true
default = "false"
description = "distribute maven binary"
name = "BP_MAVEN_COMMAND"

[[metadata.configurations]]
build = true
Expand Down
220 changes: 137 additions & 83 deletions maven/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ import (
"github.com/paketo-buildpacks/libpak/bindings"
)

const (
Command = "Command"
RunBuild = "RunBuild"
)

type Build struct {
Logger bard.Logger
ApplicationFactory ApplicationFactory
Expand All @@ -49,95 +54,72 @@ type ApplicationFactory interface {
cache libbs.Cache, command string, bom *libcnb.BOM, applicationPath string, bomScanner sbom.SBOMScanner) (libbs.Application, error)
}

func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {
b.Logger.Title(context.Buildpack)
result := libcnb.NewBuildResult()

cr, err := libpak.NewConfigurationResolver(context.Buildpack, &b.Logger)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create configuration resolver\n%w", err)
}

func install(b Build, context libcnb.BuildContext, artifact string, securityArgs []string) (string, libcnb.LayerContributor, libcnb.BOMEntry, error) {
dr, err := libpak.NewDependencyResolver(context)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create dependency resolver\n%w", err)
return "", nil, libcnb.BOMEntry{}, fmt.Errorf("unable to create dependency resolver\n%w", err)
}

dc, err := libpak.NewDependencyCache(context)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create dependency cache\n%w", err)
return "", nil, libcnb.BOMEntry{}, fmt.Errorf("unable to create dependency cache\n%w", err)
}
dc.Logger = b.Logger

command := ""
if cr.ResolveBool("BP_MAVEN_DAEMON_ENABLED") {
dep, err := dr.Resolve("mvnd", "")
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to find dependency\n%w", err)
}
dep, err := dr.Resolve(artifact, "")
if err != nil {
return "", nil, libcnb.BOMEntry{}, fmt.Errorf("unable to find dependency\n%w", err)
}

dist, be := NewMvndDistribution(dep, dc)
if artifact == "maven" {
dist, be := NewDistribution(dep, dc)
dist.SecurityArgs = securityArgs
dist.Logger = b.Logger
result.Layers = append(result.Layers, dist)
result.BOM.Entries = append(result.BOM.Entries, be)

command = filepath.Join(context.Layers.Path, dist.Name(), "bin", "mvnd")
} else {
command = filepath.Join(context.Application.Path, "mvnw")
if _, err := os.Stat(command); os.IsNotExist(err) {
dep, err := dr.Resolve("maven", "")
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to find dependency\n%w", err)
}

dist, be := NewDistribution(dep, dc)
dist.Logger = b.Logger
result.Layers = append(result.Layers, dist)
result.BOM.Entries = append(result.BOM.Entries, be)
command := filepath.Join(context.Layers.Path, dist.Name(), "bin", "mvn")
return command, dist, be, nil
}
dist, be := NewDistribution(dep, dc)
dist.SecurityArgs = securityArgs
dist.Logger = b.Logger
command := filepath.Join(context.Layers.Path, dist.Name(), "bin", "mvnd")
return command, dist, be, nil
}

command = filepath.Join(context.Layers.Path, dist.Name(), "bin", "mvn")
} else if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to stat %s\n%w", command, err)
} else {
if err := os.Chmod(command, 0755); err != nil {
b.Logger.Bodyf("WARNING: unable to chmod %s:\n%s", command, err)
}
func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {
b.Logger.Title(context.Buildpack)
result := libcnb.NewBuildResult()

if err = b.CleanMvnWrapper(command); err != nil {
b.Logger.Bodyf("WARNING: unable to clean mvnw file: %s\n%s", command, err)
}
pr := libpak.PlanEntryResolver{
Plan: context.Plan,
}
runBuild := true
entry, ok, err := pr.Resolve(PlanEntryMaven)
if ok && err == nil {
if runBuildValue, ok := entry.Metadata[RunBuild].(bool); ok {
runBuild = runBuildValue
}
}

u, err := user.Current()
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to determine user home directory\n%w", err)
mavenCommand := ""
if command, ok := entry.Metadata[Command].(string); ok {
mavenCommand = command
}

c := libbs.Cache{Path: filepath.Join(u.HomeDir, ".m2")}
c.Logger = b.Logger
result.Layers = append(result.Layers, c)

args, err := libbs.ResolveArguments("BP_MAVEN_BUILD_ARGUMENTS", cr)
cr, err := libpak.NewConfigurationResolver(context.Buildpack, &b.Logger)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to resolve build arguments\n%w", err)
}

pomFile, userSet := cr.Resolve("BP_MAVEN_POM_FILE")
if userSet {
args = append([]string{"--file", pomFile}, args...)
return libcnb.BuildResult{}, fmt.Errorf("unable to create configuration resolver\n%w", err)
}

if !b.TTY && !contains(args, []string{"-B", "--batch-mode"}) {
// terminal is not tty, and the user did not set batch mode; let's set it
args = append([]string{"--batch-mode"}, args...)
// no install requested and no build requested
if mavenCommand == "" && !runBuild {
return libcnb.BuildResult{}, nil
}

md := map[string]interface{}{}
securityArgs := []string{}
if binding, ok, err := bindings.ResolveOne(context.Platform.Bindings, bindings.OfType("maven")); err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to resolve binding\n%w", err)
} else if ok {
args, err = handleMavenSettings(binding, args, md)
securityArgs, err = handleMavenSettings(binding, securityArgs, md)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to process maven settings from binding\n%w", err)
}
Expand All @@ -148,31 +130,103 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {
}
}

art := libbs.ArtifactResolver{
ArtifactConfigurationKey: "BP_MAVEN_BUILT_ARTIFACT",
ConfigurationResolver: cr,
ModuleConfigurationKey: "BP_MAVEN_BUILT_MODULE",
InterestingFileDetector: libbs.JARInterestingFileDetector{},
if mavenCommand == "maven" || mavenCommand == "mvnd" {
cmd, layer, bomEntry, err := install(b, context, mavenCommand, securityArgs)
if cmd == "" {
return libcnb.BuildResult{}, fmt.Errorf("unable to install dependency\n%w", err)
}
result.Layers = append(result.Layers, layer)
result.BOM.Entries = append(result.BOM.Entries, bomEntry)
}

bomScanner := sbom.NewSyftCLISBOMScanner(context.Layers, effect.NewExecutor(), b.Logger)

a, err := b.ApplicationFactory.NewApplication(
md,
args,
art,
c,
command,
result.BOM,
context.Application.Path,
bomScanner,
)
u, err := user.Current()
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create application layer\n%w", err)
return libcnb.BuildResult{}, fmt.Errorf("unable to determine user home directory\n%w", err)
}

a.Logger = b.Logger
result.Layers = append(result.Layers, a)
c := libbs.Cache{Path: filepath.Join(u.HomeDir, ".m2")}
c.Logger = b.Logger

if runBuild {
command := ""
if cr.ResolveBool("BP_MAVEN_DAEMON_ENABLED") && mavenCommand != "mvnd" {
cmd, layer, bomEntry, err := install(b, context, "mvnd", securityArgs)
if err != nil {
return libcnb.BuildResult{}, err
}
result.Layers = append(result.Layers, layer)
result.BOM.Entries = append(result.BOM.Entries, bomEntry)
command = cmd
} else {
command = filepath.Join(context.Application.Path, "mvnw")
if _, err := os.Stat(command); os.IsNotExist(err) && mavenCommand != "maven" {
cmd, layer, bomEntry, err := install(b, context, "maven", securityArgs)
if err != nil {
return libcnb.BuildResult{}, err
}
result.Layers = append(result.Layers, layer)
result.BOM.Entries = append(result.BOM.Entries, bomEntry)
command = cmd
} else if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to stat %s\n%w", command, err)
} else {
if err := os.Chmod(command, 0755); err != nil {
b.Logger.Bodyf("WARNING: unable to chmod %s:\n%s", command, err)
}

if err = b.CleanMvnWrapper(command); err != nil {
b.Logger.Bodyf("WARNING: unable to clean mvnw file: %s\n%s", command, err)
}
}
}

result.Layers = append(result.Layers, c)

args, err := libbs.ResolveArguments("BP_MAVEN_BUILD_ARGUMENTS", cr)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to resolve build arguments\n%w", err)
}

pomFile, userSet := cr.Resolve("BP_MAVEN_POM_FILE")
if userSet {
args = append([]string{"--file", pomFile}, args...)
}

if !b.TTY && !contains(args, []string{"-B", "--batch-mode"}) {
// terminal is not tty, and the user did not set batch mode; let's set it
args = append([]string{"--batch-mode"}, args...)
}

args = append(securityArgs, args...)

art := libbs.ArtifactResolver{
ArtifactConfigurationKey: "BP_MAVEN_BUILT_ARTIFACT",
ConfigurationResolver: cr,
ModuleConfigurationKey: "BP_MAVEN_BUILT_MODULE",
InterestingFileDetector: libbs.JARInterestingFileDetector{},
}

bomScanner := sbom.NewSyftCLISBOMScanner(context.Layers, effect.NewExecutor(), b.Logger)

a, err := b.ApplicationFactory.NewApplication(
md,
args,
art,
c,
command,
result.BOM,
context.Application.Path,
bomScanner,
)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create application layer\n%w", err)
}

a.Logger = b.Logger
result.Layers = append(result.Layers, a)
} else {
result.Layers = append(result.Layers, c)
}

return result, nil
}
Expand Down
Loading