Skip to content

Commit

Permalink
Support new env. variable BP_MAVEN_ACTIVE_PROFILES
Browse files Browse the repository at this point in the history
* append its profiles last
  • Loading branch information
anthonydahanne committed May 1, 2023
1 parent 357d72f commit 7a392ea
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions buildpack.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ api = "0.7"
description = "the additionnal arguments (appended to BP_MAVEN_BUILD_ARGUMENTS) to pass to Maven"
name = "BP_MAVEN_ADDITIONAL_BUILD_ARGUMENTS"

[[metadata.configurations]]
build = true
default = ""
description = "the active profiles (comma separated: such as: p1,!p2,?p3) to pass to Maven"
name = "BP_MAVEN_ACTIVE_PROFILES"

[[metadata.configurations]]
build = true
default = "target/*.[ejw]ar"
Expand Down
9 changes: 9 additions & 0 deletions maven/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ func (b Build) configureMaven(context libcnb.BuildContext) (libbs.ArtifactResolv
args = append(args, additionalArgs...)
}

profiles, err := libbs.ResolveArguments("BP_MAVEN_ACTIVE_PROFILES", b.configResolver)
if err != nil {
return libbs.ArtifactResolver{}, map[string]interface{}{}, []string{},
fmt.Errorf("unable to resolve profiles build arguments\n%w", err)
} else if len(profiles) > 0 {
profiles = append([]string{"-P"}, profiles...)
args = append(args, profiles...)
}

return libbs.ArtifactResolver{
ArtifactConfigurationKey: "BP_MAVEN_BUILT_ARTIFACT",
ConfigurationResolver: b.configResolver,
Expand Down
31 changes: 31 additions & 0 deletions maven/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,37 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})
})

context("BP_MAVEN_ACTIVE_PROFILES adds active profiles", func() {
it.Before(func() {
Expect(os.Setenv("BP_MAVEN_BUILD_ARGUMENTS", "--batch-mode user-provided-argument")).To(Succeed())
Expect(os.Setenv("BP_MAVEN_ADDITIONAL_BUILD_ARGUMENTS", "-Dgpg.skip -Dmaven.javadoc.skip=true")).To(Succeed())
Expect(os.Setenv("BP_MAVEN_ACTIVE_PROFILES", "native,?prod,!aot,-dev")).To(Succeed())
})

it.After(func() {
Expect(os.Unsetenv(("BP_MAVEN_BUILD_ARGUMENTS"))).To(Succeed())
Expect(os.Unsetenv(("BP_MAVEN_ADDITIONAL_BUILD_ARGUMENTS"))).To(Succeed())
Expect(os.Unsetenv(("BP_MAVEN_ACTIVE_PROFILES"))).To(Succeed())
})
it("the profiles native,?prod,!aot,-dev got appended after all the other maven arguments", func() {
Expect(os.WriteFile(mvnwFilepath, []byte{}, 0644)).To(Succeed())
ctx.StackID = "test-stack-id"
mavenBuild.TTY = false

result, err := mavenBuild.Build(ctx)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers[1].(libbs.Application).Arguments).To(Equal([]string{
"--batch-mode",
"user-provided-argument",
"-Dgpg.skip",
"-Dmaven.javadoc.skip=true",
"-P",
"native,?prod,!aot,-dev",
}))
})
})

context("BP_MAVEN_SETTINGS_PATH configuration is set", func() {
it.Before(func() {
ctx.Buildpack.Metadata = map[string]interface{}{
Expand Down

0 comments on commit 7a392ea

Please sign in to comment.