From 357d72f2c85f4797aacfb19846470448d29e3f8a Mon Sep 17 00:00:00 2001 From: Anthony Dahanne Date: Sat, 29 Apr 2023 23:54:57 -0400 Subject: [PATCH 1/3] Support new env. variable BP_MAVEN_ADDITIONAL_BUILD_ARGUMENTS * append its arguments after the BP_MAVEN_BUILD_ARGUMENTS --- buildpack.toml | 6 ++++++ maven/build.go | 8 ++++++++ maven/build_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/buildpack.toml b/buildpack.toml index 25befcd..10759c6 100644 --- a/buildpack.toml +++ b/buildpack.toml @@ -37,6 +37,12 @@ api = "0.7" description = "the arguments to pass to Maven" name = "BP_MAVEN_BUILD_ARGUMENTS" + [[metadata.configurations]] + build = true + default = "" + 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 = "target/*.[ejw]ar" diff --git a/maven/build.go b/maven/build.go index 21f8fe1..6d7246e 100644 --- a/maven/build.go +++ b/maven/build.go @@ -192,6 +192,14 @@ func (b Build) configureMaven(context libcnb.BuildContext) (libbs.ArtifactResolv } } + additionalArgs, err := libbs.ResolveArguments("BP_MAVEN_ADDITIONAL_BUILD_ARGUMENTS", b.configResolver) + if err != nil { + return libbs.ArtifactResolver{}, map[string]interface{}{}, []string{}, + fmt.Errorf("unable to resolve additionnal build arguments\n%w", err) + } else { + args = append(args, additionalArgs...) + } + return libbs.ArtifactResolver{ ArtifactConfigurationKey: "BP_MAVEN_BUILT_ARTIFACT", ConfigurationResolver: b.configResolver, diff --git a/maven/build_test.go b/maven/build_test.go index ab1603f..6becc5c 100644 --- a/maven/build_test.go +++ b/maven/build_test.go @@ -128,6 +128,33 @@ func testBuild(t *testing.T, context spec.G, it spec.S) { }) }) + context("BP_MAVEN_ADDITIONAL_BUILD_ARGUMENTS adds additional build arguments", 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()) + }) + + it.After(func() { + Expect(os.Unsetenv(("BP_MAVEN_BUILD_ARGUMENTS"))).To(Succeed()) + Expect(os.Unsetenv(("BP_MAVEN_ADDITIONAL_BUILD_ARGUMENTS"))).To(Succeed()) + }) + it("-Dgpg.skip and Dmaven.javadoc.skip=true got appended after 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", + })) + }) + }) + context("BP_MAVEN_SETTINGS_PATH configuration is set", func() { it.Before(func() { ctx.Buildpack.Metadata = map[string]interface{}{ From 7a392ea4008a348d403f10dfc47d79122f98cdda Mon Sep 17 00:00:00 2001 From: Anthony Dahanne Date: Sun, 30 Apr 2023 00:18:57 -0400 Subject: [PATCH 2/3] Support new env. variable BP_MAVEN_ACTIVE_PROFILES * append its profiles last --- buildpack.toml | 6 ++++++ maven/build.go | 9 +++++++++ maven/build_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/buildpack.toml b/buildpack.toml index 10759c6..2d5cc3d 100644 --- a/buildpack.toml +++ b/buildpack.toml @@ -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" diff --git a/maven/build.go b/maven/build.go index 6d7246e..76e1a55 100644 --- a/maven/build.go +++ b/maven/build.go @@ -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, diff --git a/maven/build_test.go b/maven/build_test.go index 6becc5c..122d2af 100644 --- a/maven/build_test.go +++ b/maven/build_test.go @@ -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{}{ From 273d3ce7b2bc12db1535270287f946ad724628be Mon Sep 17 00:00:00 2001 From: Anthony Dahanne Date: Mon, 1 May 2023 22:27:03 -0400 Subject: [PATCH 3/3] Fix #226: Add README documentation --- README.md | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 5981227..e88c298 100644 --- a/README.md +++ b/README.md @@ -34,19 +34,21 @@ The buildpack will do the following: ## Configuration -| Environment Variable | Description | -| --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `$BP_MAVEN_VERSION` | Configure the major Maven version (e.g. `3`, `4`). Since the buildpack only ships a single version of each supported line, updates to the buildpack can change the exact version of Maven installed. If you require a specific minor/patch version of Maven, use the Maven wrapper instead. | -| `$BP_MAVEN_BUILD_ARGUMENTS` | Configure the arguments to pass to Maven. Defaults to `-Dmaven.test.skip=true --no-transfer-progress package`. `--batch-mode` will be prepended to the argument list in environments without a TTY. | -| `$BP_MAVEN_BUILT_MODULE` | Configure the module to find application artifact in. Defaults to the root module (empty). | -| `$BP_MAVEN_BUILT_ARTIFACT` | Configure the built application artifact explicitly. Supersedes `$BP_MAVEN_BUILT_MODULE` Defaults to `target/*.[ejw]ar`. Can match a single file, multiple files or a directory. Can be one or more space separated patterns. | -| `$BP_MAVEN_POM_FILE` | Specifies a custom location to the project's `pom.xml` file. It should be a full path to the file under the `/workspace` directory or it should be relative to the root of the project (i.e. `/workspace'). Defaults to `pom.xml`. | -| `$BP_MAVEN_DAEMON_ENABLED` | Triggers apache maven-mvnd to be installed and configured for use instead of Maven. The default value is `false`. Set to `true` to use the Maven Daemon. | -| `$BP_MAVEN_SETTINGS_PATH` | Specifies a custom location to Maven's `settings.xml` file. If `$BP_MAVEN_SETTINGS_PATH` is set and a Maven binding is provided, the binding takes the higher precedence. | -| `$BP_INCLUDE_FILES` | Colon separated list of glob patterns to match source files. Any matched file will be retained in the final image. Defaults to `` (i.e. nothing). | -| `$BP_EXCLUDE_FILES` | Colon separated list of glob patterns to match source files. Any matched file will be specifically removed from the final image. If include patterns are also specified, then they are applied first and exclude patterns can be used to further reduce the fileset. | -| `$BP_JAVA_INSTALL_NODE` | Configure whether to request that `yarn` and `node` are installed by another buildpack**. If set to `true`, the buildpack will check the app root or path set by `$BP_NODE_PROJECT_PATH` for either: A `yarn.lock` file, which requires that `yarn` and `node` are installed or, a `package.json` file, which requires that `node` is installed. Defaults to `false` -| `$BP_NODE_PROJECT_PATH` | Configure a project subdirectory to look for `package.json` and `yarn.lock` files +| Environment Variable | Description | +|----------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `$BP_MAVEN_VERSION` | Configure the major Maven version (e.g. `3`, `4`). Since the buildpack only ships a single version of each supported line, updates to the buildpack can change the exact version of Maven installed. If you require a specific minor/patch version of Maven, use the Maven wrapper instead. | +| `$BP_MAVEN_BUILD_ARGUMENTS` | Configure the arguments to pass to Maven. Defaults to `-Dmaven.test.skip=true --no-transfer-progress package`. `--batch-mode` will be prepended to the argument list in environments without a TTY. | +| `$BP_MAVEN_ADDITIONAL_BUILD_ARGUMENTS` | Configure the additionnal arguments (e.g. `-DskipJavadoc`; appended to BP_MAVEN_BUILD_ARGUMENTS) to pass to Maven. Defaults to `` (empty string). | +| `$BP_MAVEN_ACTIVE_PROFILES` | Configure the active profiles (comma separated: e.g. `p1,!p2,?p3`) to pass to Maven. Defaults to `` (empty string). | +| `$BP_MAVEN_BUILT_MODULE` | Configure the module to find application artifact in. Defaults to the root module (empty). | +| `$BP_MAVEN_BUILT_ARTIFACT` | Configure the built application artifact explicitly. Supersedes `$BP_MAVEN_BUILT_MODULE` Defaults to `target/*.[ejw]ar`. Can match a single file, multiple files or a directory. Can be one or more space separated patterns. | +| `$BP_MAVEN_POM_FILE` | Specifies a custom location to the project's `pom.xml` file. It should be a full path to the file under the `/workspace` directory or it should be relative to the root of the project (i.e. `/workspace'). Defaults to `pom.xml`. | +| `$BP_MAVEN_DAEMON_ENABLED` | Triggers apache maven-mvnd to be installed and configured for use instead of Maven. The default value is `false`. Set to `true` to use the Maven Daemon. | +| `$BP_MAVEN_SETTINGS_PATH` | Specifies a custom location to Maven's `settings.xml` file. If `$BP_MAVEN_SETTINGS_PATH` is set and a Maven binding is provided, the binding takes the higher precedence. | +| `$BP_INCLUDE_FILES` | Colon separated list of glob patterns to match source files. Any matched file will be retained in the final image. Defaults to `` (i.e. nothing). | +| `$BP_EXCLUDE_FILES` | Colon separated list of glob patterns to match source files. Any matched file will be specifically removed from the final image. If include patterns are also specified, then they are applied first and exclude patterns can be used to further reduce the fileset. | +| `$BP_JAVA_INSTALL_NODE` | Configure whether to request that `yarn` and `node` are installed by another buildpack**. If set to `true`, the buildpack will check the app root or path set by `$BP_NODE_PROJECT_PATH` for either: A `yarn.lock` file, which requires that `yarn` and `node` are installed or, a `package.json` file, which requires that `node` is installed. Defaults to `false` | +| `$BP_NODE_PROJECT_PATH` | Configure a project subdirectory to look for `package.json` and `yarn.lock` files | ### Note ** If the node and/or yarn requirements are met and the [Node Engine](https://github.com/paketo-buildpacks/node-engine) or [Yarn](https://github.com/paketo-buildpacks/yarn) participate in the build, environment variables related to these buildpacks can be set, such as `BP_NODE_PROJECT_PATH` or `BP_NODE_VERSION`. See the [Paketo Node.js docs](https://paketo.io/docs/howto/nodejs/) for more info.