Skip to content

Commit

Permalink
maven buildpack environment configuration (#508)
Browse files Browse the repository at this point in the history
* Honor buildpack provided env vars

Lifecycle sets buildpack provided build time env vars in the current
environment. This change reads from both the platform directory (user or
platform provided) as well as now the current environment (buildpack
provided). This fixes compatibility with a nexus/artifactory type
buildpack that sets `MAVEN_SETTINGS_PATH` or `MAVEN_SETTINGS_URL`.

* Recognize `JAVA_HOME` env var being set

Currently, `JAVA_HOME` is ignored even if set by a buildpack, user, or
operator since `clear-env` is set to true. This change sets it in the
maven environment if it's present.

* add changelog entry
  • Loading branch information
hone authored Jun 13, 2023
1 parent a21ad07 commit fed88a5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
2 changes: 2 additions & 0 deletions buildpacks/maven/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

* This buildpack now declares to be compatible with the `*` stack. While the buildpack cannot guarantee it works with any stack conceivable, it should be compatible with some stacks that are not maintained by Heroku. Use of this buildpack on such stacks is unsupported. ([#498](https://github.com/heroku/buildpacks-jvm/pull/498))
* Allow `JAVA_HOME` to be set by user or operator via `<platform>/env`. ([#508](https://github.com/heroku/buildpacks-jvm/pull/508))
* `MAVEN_SETTINGS_PATH`, `MAVEN_ESTTINGS_URL`, `MAVEN_CUSTOM_GOALS`, and `MAVEN_CUSTOM_OPTS` can be set by a previous buildpack. ([#508](https://github.com/heroku/buildpacks-jvm/pull/508))

## [1.0.4] 2023/05/11

Expand Down
23 changes: 14 additions & 9 deletions buildpacks/maven/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ impl Buildpack for MavenBuildpack {

#[allow(clippy::too_many_lines)]
fn build(&self, context: BuildContext<Self>) -> libcnb::Result<BuildResult, Self::Error> {
let mut current_or_platform_env = Env::from_current();
for (key, value) in context.platform.env().iter() {
current_or_platform_env.insert(key, value);
}

let maven_repository_layer =
context.handle_layer(layer_name!("repository"), MavenRepositoryLayer)?;

Expand All @@ -122,7 +127,7 @@ impl Buildpack for MavenBuildpack {

log_header("Installing Maven");

let (mvn_executable, mvn_env) = match maven_mode {
let (mvn_executable, mut mvn_env) = match maven_mode {
Mode::UseWrapper => {
log_info("Maven wrapper detected, skipping installation.");

Expand Down Expand Up @@ -179,10 +184,11 @@ impl Buildpack for MavenBuildpack {
)
}
};
if let Some(java_home) = current_or_platform_env.get("JAVA_HOME") {
mvn_env.insert("JAVA_HOME", java_home);
}

let maven_goals = context
.platform
.env()
let maven_goals = current_or_platform_env
.get("MAVEN_CUSTOM_GOALS")
.map_or_else(
|| Ok(default_maven_goals()),
Expand All @@ -192,9 +198,7 @@ impl Buildpack for MavenBuildpack {
},
)?;

let mut maven_options = context
.platform
.env()
let mut maven_options = current_or_platform_env
.get("MAVEN_CUSTOM_OPTS")
.map_or_else(
|| Ok(default_maven_opts()),
Expand All @@ -207,8 +211,9 @@ impl Buildpack for MavenBuildpack {
},
)?;

let settings_xml_path = resolve_settings_xml_path(&context.app_dir, context.platform.env())
.map_err(MavenBuildpackError::SettingsError)?;
let settings_xml_path =
resolve_settings_xml_path(&context.app_dir, &current_or_platform_env)
.map_err(MavenBuildpackError::SettingsError)?;

if let Some(settings_xml_path) = settings_xml_path {
maven_options.push(String::from("-s"));
Expand Down

0 comments on commit fed88a5

Please sign in to comment.