Skip to content

Malax/refactor maven invocation #257

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
10 changes: 7 additions & 3 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"

source "${BUILDPACK_DIR}/lib/output.sh"
source "${BUILDPACK_DIR}/lib/util.sh"
source "${BUILDPACK_DIR}/lib/common.sh"
source "${BUILDPACK_DIR}/lib/java_properties.sh"
source "${BUILDPACK_DIR}/lib/maven.sh"
source "${BUILDPACK_DIR}/lib/metrics.sh"
source "${BUILDPACK_DIR}/lib/openjdk.sh"
Expand All @@ -22,5 +22,9 @@ util::export_env_dir "${ENV_DIR}" "." "JAVA_OPTS|JAVA_TOOL_OPTIONS"

openjdk::install_openjdk_via_jvm_common_buildpack "${BUILD_DIR}" "${BUILDPACK_DIR}"

maven::run_mvn "compile" "${BUILD_DIR}" "${CACHE_DIR}"
maven::remove_mvn "${BUILD_DIR}" "${CACHE_DIR}"
maven::setup_maven_and_build_app \
"${BUILD_DIR}" \
"${CACHE_DIR}" \
"${MAVEN_JAVA_OPTS:-""}" \
"${MAVEN_CUSTOM_OPTS:-"-DskipTests"}" \
"${MAVEN_CUSTOM_GOALS:-"clean dependency:list install"}"
34 changes: 20 additions & 14 deletions bin/detect
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ set -euo pipefail

BUILD_DIR="${1}"

if [[ -f "${BUILD_DIR}/pom.xml" ]] ||
[[ -f "${BUILD_DIR}/pom.atom" ]] ||
[[ -f "${BUILD_DIR}/pom.clj" ]] ||
[[ -f "${BUILD_DIR}/pom.groovy" ]] ||
[[ -f "${BUILD_DIR}/pom.rb" ]] ||
[[ -f "${BUILD_DIR}/pom.scala" ]] ||
[[ -f "${BUILD_DIR}/pom.yaml" ]] ||
[[ -f "${BUILD_DIR}/pom.yml" ]]; then
echo "Java"
exit 0
else
(>&2 echo "Could not find a pom.xml file! Please check that it exists and is committed to Git.")
exit 1
fi
extensions=(
"xml"
"atom"
"clj"
"groovy"
"rb"
"scala"
"yaml"
"yml"
)

for extension in "${extensions[@]}"; do
if [[ -f "${BUILD_DIR}/pom.${extension}" ]]; then
echo "Java"
exit 0
fi
done

(>&2 echo "Could not find a pom.xml file or other supported POM format!")
exit 1
7 changes: 3 additions & 4 deletions bin/release
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ source "${BUILDPACK_DIR}/lib/frameworks.sh"
echo "---"

if frameworks::has_postgres "${BUILD_DIR}"; then
cat <<EOF
addons:
- heroku-postgresql
EOF
echo "addons:"
echo " - heroku-postgresql"
fi

if [[ ! -f "${BUILD_DIR}/Procfile" ]] && [[ -d "${BUILD_DIR}/target" ]]; then
Expand All @@ -24,6 +22,7 @@ if [[ ! -f "${BUILD_DIR}/Procfile" ]] && [[ -d "${BUILD_DIR}/target" ]]; then
echo " web: java -Dquarkus.http.port=\$PORT \$JAVA_OPTS -jar target/quarkus-app/quarkus-run.jar"
else
cd "${BUILD_DIR}"
# Using find in command substitution for jar file discovery
# shellcheck disable=SC2044
for jar_file in $(find target -maxdepth 1 -name "*.jar" -type f -exec ls -S {} +); do
if frameworks::is_spring_boot "${BUILD_DIR}"; then
Expand Down
12 changes: 4 additions & 8 deletions bin/test
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"

source "${BUILDPACK_DIR}/lib/output.sh"
source "${BUILDPACK_DIR}/lib/util.sh"
source "${BUILDPACK_DIR}/lib/common.sh"
source "${BUILDPACK_DIR}/lib/java_properties.sh"
source "${BUILDPACK_DIR}/lib/maven.sh"

util::export_env_dir "${ENV_DIR}" "." "JAVA_OPTS|JAVA_TOOL_OPTIONS"

cd "${BUILD_DIR}"

mvn_settings_opt="$(maven::mvn_settings_opt "${BUILD_DIR}" "${BUILD_DIR}")"

if maven::has_maven_wrapper "${BUILD_DIR}"; then
# shellcheck disable=SC2086
./mvnw -B ${mvn_settings_opt} "${MAVEN_HEROKU_CI_GOAL:-test}"
if maven::should_use_wrapper "${BUILD_DIR}"; then
./mvnw -B "${MAVEN_HEROKU_CI_GOAL:-test}"
else
# shellcheck disable=SC2086
mvn -B ${mvn_settings_opt} "${MAVEN_HEROKU_CI_GOAL:-test}"
mvn -B "${MAVEN_HEROKU_CI_GOAL:-test}"
fi
22 changes: 17 additions & 5 deletions bin/test-compile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"

source "${BUILDPACK_DIR}/lib/output.sh"
source "${BUILDPACK_DIR}/lib/util.sh"
source "${BUILDPACK_DIR}/lib/common.sh"
source "${BUILDPACK_DIR}/lib/maven.sh"
source "${BUILDPACK_DIR}/lib/openjdk.sh"

Expand All @@ -20,7 +19,20 @@ openjdk::install_openjdk_via_jvm_common_buildpack "${BUILD_DIR}" "${BUILDPACK_DI

cd "${BUILD_DIR}"

common::cache_copy ".m2" "${CACHE_DIR}" "${BUILD_DIR}"
maven::run_mvn "test-compile" "${BUILD_DIR}" "${BUILD_DIR}"
maven::write_mvn_profile "${BUILD_DIR}"
common::cache_copy ".m2" "${BUILD_DIR}" "${CACHE_DIR}"
util::cache_copy ".m2" "${CACHE_DIR}" "${BUILD_DIR}"

maven::setup_maven_and_build_app \
"${BUILD_DIR}" \
"${CACHE_DIR}" \
"" \
"" \
"${MAVEN_CUSTOM_GOALS:-"clean dependency:resolve-plugins test-compile"}"

mkdir -p "${BUILD_DIR}/.profile.d"
cat <<-EOF >"${BUILD_DIR}/.profile.d/maven.sh"
export M2_HOME="\$HOME/.maven"
export MAVEN_OPTS="-Xmx1024m -Duser.home=\$HOME -Dmaven.repo.local=\$HOME/.m2/repository"
export PATH="\$M2_HOME/bin:\$PATH"
EOF

util::cache_copy ".m2" "${BUILD_DIR}" "${CACHE_DIR}"
93 changes: 0 additions & 93 deletions lib/common.sh

This file was deleted.

45 changes: 45 additions & 0 deletions lib/frameworks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,73 @@
# however, it helps Shellcheck realise the options under which these functions will run.
set -euo pipefail

# Detects if the application is a Spring Boot project by checking for Spring Boot
# group and artifact IDs in the pom.xml file.
#
# Usage:
# ```
# if frameworks::is_spring_boot "${BUILD_DIR}"; then
# echo "Spring Boot application detected"
# fi
# ```
frameworks::is_spring_boot() {
local build_dir="${1}"

grep -qs "<groupId>org.springframework.boot" "${build_dir}/pom.xml" &&
grep -qs "<artifactId>spring-boot" "${build_dir}/pom.xml"
}

# Detects if the application uses WildFly Swarm by checking for WildFly Swarm
# group ID in the pom.xml file.
#
# Usage:
# ```
# if frameworks::is_wildfly_swarm "${BUILD_DIR}"; then
# echo "WildFly Swarm application detected"
# fi
# ```
frameworks::is_wildfly_swarm() {
local build_dir="${1}"
grep -qs "<groupId>org.wildfly.swarm" "${build_dir}/pom.xml"
}

# Detects if the application uses Micronaut by checking for Micronaut
# group ID in the pom.xml file.
#
# Usage:
# ```
# if frameworks::is_micronaut "${BUILD_DIR}"; then
# echo "Micronaut application detected"
# fi
# ```
frameworks::is_micronaut() {
local build_dir="${1}"
grep -qs "<groupId>io.micronaut" "${build_dir}/pom.xml"
}

# Detects if the application uses Quarkus by checking for Quarkus
# group ID in the pom.xml file.
#
# Usage:
# ```
# if frameworks::is_quarkus "${BUILD_DIR}"; then
# echo "Quarkus application detected"
# fi
# ```
frameworks::is_quarkus() {
local build_dir="${1}"
grep -qs "<groupId>io.quarkus" "${build_dir}/pom.xml"
}

# Detects if the application has PostgreSQL dependencies by checking for
# PostgreSQL-related group IDs in the pom.xml file.
#
# Usage:
# ```
# if frameworks::has_postgres "${BUILD_DIR}"; then
# echo "PostgreSQL dependency detected"
# fi
# ```
frameworks::has_postgres() {
local build_dir="${1}"

Expand Down
21 changes: 21 additions & 0 deletions lib/java_properties.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

# Reads the value of a key from a Java properties file
#
# ```
# java_properties:get "system.properties" "java.runtime.version"
# ```
java_properties::get() {
local file=${1:?}
local key=${2:?}

if [ -f "${file}" ]; then
local escaped_key
escaped_key="${key//\./\\.}"

grep -E "^${escaped_key}[[:space:]=]+" "${file}" |
sed -E -e "s/${escaped_key}([\ \t]*=[\ \t]*|[\ \t]+)([_A-Za-z0-9\.-]*).*/\2/g"
else
echo ""
fi
}
Loading
Loading