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

add Gradle build support, initial version (fixes #118) #121

Merged
merged 5 commits into from
May 15, 2018
Merged
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
2 changes: 2 additions & 0 deletions java/images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fish-pepper:
config:
base:
jboss:
withGradle: true
from: "jboss/base-jdk:8"
user: "jboss"
home: "/opt/jboss"
Expand All @@ -18,6 +19,7 @@ config:
jolokia: "1.5.0"
jmxexporter: "0.1.0"
rhel:
withGradle: false
from: "jboss/openjdk18-rhel7:1.1-7"
user: "jboss"
home: "/home/jboss"
Expand Down
83 changes: 79 additions & 4 deletions java/images/jboss/s2i/assemble
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function check_error() {
fi
}

function get_output_dir() {
function get_maven_output_dir() {
local dir=""

# If multi module build and no ARTIFACT_DIR is set --> error
Expand Down Expand Up @@ -74,7 +74,6 @@ function copy_artifacts() {
}

function setup_maven() {

if [ -n "$HTTP_PROXY_HOST" -a -n "$HTTP_PROXY_PORT" ]; then
xml="<proxy>\
<id>genproxy</id>\
Expand Down Expand Up @@ -131,14 +130,14 @@ function build_maven() {
fi

echo "Found pom.xml ... "
echo "Running 'mvn ${maven_env_args} ${maven_args} ${MAVEN_ARGS_APPEND}'"

local old_dir=$(pwd)
cd ${S2I_SOURCE_DIR}
check_error "changing directory to ${S2I_SOURCE_DIR}" $?

# =========
# Run Maven
echo "Running 'mvn ${maven_env_args} ${maven_args} ${MAVEN_ARGS_APPEND}'"
mvn ${maven_env_args} --version
mvn ${maven_env_args} ${maven_args} ${MAVEN_ARGS_APPEND}
check_error "Maven build" $?
Expand All @@ -160,14 +159,83 @@ function build_maven() {
cd ${old_dir}
}


# =========================================================================
# Gradle support

function get_gradle_output_dir() {
local dir=""

# If multi module build and no ARTIFACT_DIR is set --> error
if [ x"${ARTIFACT_DIR}" = x ]; then
dir="${S2I_SOURCE_DIR}/build/libs"
else
if [ "${ARTIFACT_DIR:0:1}" = "/" ]; then
echo "ARTIFACT_DIR \"${ARTIFACT_DIR}\" must not be absolute but relative to the source directory"
exit 1
fi
dir="${S2I_SOURCE_DIR}/${ARTIFACT_DIR}"
fi

echo ${dir}
}

function build_gradle() {
# Where artifacts are created during build
local build_dir=$1

# Where to put the artifacts
local app_dir=$2

# Default args
local gradle_args=${GRADLE_ARGS:-build -x test}

# If there is no user provided GRADLE_OPTS, use options from run-java.sh
if [ -f ${RUN_JAVA_DIR}/run-java.sh -a -z "${GRADLE_OPTS}" ]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using [ .... ] && [ .... ] proved to be more robust to me. -a fails in certain edge case (which I dont remember right now ;-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately my bash foo smartness is not nearly good enough 😃 to understand what exactly you mean here, but FYI I've just copy/pasted this from an almost identical line in build_maven() ... perhaps you would like to raise a follow-up PR, when this is in, to adjust both this and the original that I got inspired from for this however you see fit?

export GRADLE_OPTS=$(${RUN_JAVA_DIR}/run-java.sh options)
fi

if [ ! -z "${GRADLE_OPTS}" ]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-n ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly to above, build_maven() uses if [ ! -z "${MAVEN_OPTS}" ]; then - this should probably be kept consistent, and changed in both places, in a separate PR when this is in? I can raise one proposing that as a follow-up to this - if you would like?

echo "Using GRADLE_OPTS '${GRADLE_OPTS}'"
fi

local old_dir=$(pwd)
cd ${S2I_SOURCE_DIR}
check_error "changing directory to ${S2I_SOURCE_DIR}" $?

# =========
# Run Gradle
echo "Running './gradlew ${gradle_args} ${GRADLE_ARGS_APPEND}'"
./gradlew ${gradle_args} ${GRADLE_ARGS_APPEND}
check_error "Gradle build" $?

# ==============
# Copy artifacts
echo "Copying Gradle artifacts from ${build_dir} to ${app_dir} ..."
copy_artifacts ${build_dir} ${app_dir}
check_error "copying artifacts from ${build_dir} to ${app_dir}" $?

# ======================
# Remove repo if desired
if [ "x${GRADLE_CLEAR_REPO}" != "x" ]; then
rm -rf "${S2I_ARTIFACTS_DIR}/gradle"
check_error "Cannot remove local Gradle repository ${S2I_ARTIFACTS_DIR}/gradle" $?
fi

cd ${old_dir}
}



# =========================================================================
# Main

echo "=================================================================="
echo "Starting S2I Java Build ....."
mkdir -p ${DEPLOYMENTS_DIR}
if [ -f "${S2I_SOURCE_DIR}/pom.xml" ]; then
echo "S2I source build for Maven detected"
build_dir=$(get_output_dir)
build_dir=$(get_maven_output_dir)
check_error "Cannot get output dir: $build_dir" $?
# If a pom.xml is present use maven
setup_maven
Expand All @@ -188,6 +256,13 @@ elif [ -f "${S2I_SOURCE_DIR}/Dockerfile" ]; then
echo "Copying binaries from ${binary_dir} to ${DEPLOYMENTS_DIR} ..."
copy_dir ${binary_dir} ${DEPLOYMENTS_DIR}
check_error "copying ${binary_dir} to ${DEPLOYMENTS_DIR}" $?

elif ls ${S2I_SOURCE_DIR}/*.gradle* &> /dev/null; then
echo "S2I source build for Gradle detected, due to presence of a *.gradle* in ${S2I_SOURCE_DIR}"
build_dir=$(get_gradle_output_dir)
check_error "Cannot get output dir: $build_dir" $?
build_gradle ${build_dir} ${DEPLOYMENTS_DIR}

else
echo "S2I source build with plain binaries detected"
if [ -d "${S2I_SOURCE_DIR}/deployments" ]; then
Expand Down
2 changes: 1 addition & 1 deletion java/images/jboss/s2i/s2i-setup
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
S2I_DESTINATION=${S2I_DESTINATION:-/tmp}
S2I_SOURCE_DIR="${S2I_DESTINATION}/src"
S2I_ARTIFACTS_DIR="${S2I_DESTINATION}/artifacts"
DEPLOYMENTS_DIR="/deployments"
DEPLOYMENTS_DIR="${DEPLOYMENTS_DIR:-/deployments}"
RUN_JAVA_DIR=/opt/run-java
12 changes: 8 additions & 4 deletions java/images/rhel/s2i/assemble
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function check_error() {
fi
}

function get_output_dir() {
function get_maven_output_dir() {
local dir=""

# If multi module build and no ARTIFACT_DIR is set --> error
Expand Down Expand Up @@ -74,7 +74,6 @@ function copy_artifacts() {
}

function setup_maven() {

if [ -n "$HTTP_PROXY_HOST" -a -n "$HTTP_PROXY_PORT" ]; then
xml="<proxy>\
<id>genproxy</id>\
Expand Down Expand Up @@ -131,14 +130,14 @@ function build_maven() {
fi

echo "Found pom.xml ... "
echo "Running 'mvn ${maven_env_args} ${maven_args} ${MAVEN_ARGS_APPEND}'"

local old_dir=$(pwd)
cd ${S2I_SOURCE_DIR}
check_error "changing directory to ${S2I_SOURCE_DIR}" $?

# =========
# Run Maven
echo "Running 'mvn ${maven_env_args} ${maven_args} ${MAVEN_ARGS_APPEND}'"
mvn ${maven_env_args} --version
mvn ${maven_env_args} ${maven_args} ${MAVEN_ARGS_APPEND}
check_error "Maven build" $?
Expand All @@ -160,14 +159,18 @@ function build_maven() {
cd ${old_dir}
}




# =========================================================================
# Main

echo "=================================================================="
echo "Starting S2I Java Build ....."
mkdir -p ${DEPLOYMENTS_DIR}
if [ -f "${S2I_SOURCE_DIR}/pom.xml" ]; then
echo "S2I source build for Maven detected"
build_dir=$(get_output_dir)
build_dir=$(get_maven_output_dir)
check_error "Cannot get output dir: $build_dir" $?
# If a pom.xml is present use maven
setup_maven
Expand All @@ -188,6 +191,7 @@ elif [ -f "${S2I_SOURCE_DIR}/Dockerfile" ]; then
echo "Copying binaries from ${binary_dir} to ${DEPLOYMENTS_DIR} ..."
copy_dir ${binary_dir} ${DEPLOYMENTS_DIR}
check_error "copying ${binary_dir} to ${DEPLOYMENTS_DIR}" $?

else
echo "S2I source build with plain binaries detected"
if [ -d "${S2I_SOURCE_DIR}/deployments" ]; then
Expand Down
2 changes: 1 addition & 1 deletion java/images/rhel/s2i/s2i-setup
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
S2I_DESTINATION=${S2I_DESTINATION:-/tmp}
S2I_SOURCE_DIR="${S2I_DESTINATION}/src"
S2I_ARTIFACTS_DIR="${S2I_DESTINATION}/artifacts"
DEPLOYMENTS_DIR="/deployments"
DEPLOYMENTS_DIR="${DEPLOYMENTS_DIR:-/deployments}"
RUN_JAVA_DIR=/opt/run-java
83 changes: 79 additions & 4 deletions java/templates/s2i/assemble
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function check_error() {
fi
}

function get_output_dir() {
function get_maven_output_dir() {
local dir=""

# If multi module build and no ARTIFACT_DIR is set --> error
Expand Down Expand Up @@ -74,7 +74,6 @@ function copy_artifacts() {
}

function setup_maven() {

if [ -n "$HTTP_PROXY_HOST" -a -n "$HTTP_PROXY_PORT" ]; then
xml="<proxy>\
<id>genproxy</id>\
Expand Down Expand Up @@ -131,14 +130,14 @@ function build_maven() {
fi

echo "Found pom.xml ... "
echo "Running 'mvn ${maven_env_args} ${maven_args} ${MAVEN_ARGS_APPEND}'"

local old_dir=$(pwd)
cd ${S2I_SOURCE_DIR}
check_error "changing directory to ${S2I_SOURCE_DIR}" $?

# =========
# Run Maven
echo "Running 'mvn ${maven_env_args} ${maven_args} ${MAVEN_ARGS_APPEND}'"
mvn ${maven_env_args} --version
mvn ${maven_env_args} ${maven_args} ${MAVEN_ARGS_APPEND}
check_error "Maven build" $?
Expand All @@ -160,14 +159,83 @@ function build_maven() {
cd ${old_dir}
}

{{? fp.config.base.withGradle }}
# =========================================================================
# Gradle support

function get_gradle_output_dir() {
local dir=""

# If multi module build and no ARTIFACT_DIR is set --> error
if [ x"${ARTIFACT_DIR}" = x ]; then
dir="${S2I_SOURCE_DIR}/build/libs"
else
if [ "${ARTIFACT_DIR:0:1}" = "/" ]; then
echo "ARTIFACT_DIR \"${ARTIFACT_DIR}\" must not be absolute but relative to the source directory"
exit 1
fi
dir="${S2I_SOURCE_DIR}/${ARTIFACT_DIR}"
fi

echo ${dir}
}

function build_gradle() {
# Where artifacts are created during build
local build_dir=$1

# Where to put the artifacts
local app_dir=$2

# Default args
local gradle_args=${GRADLE_ARGS:-build -x test}

# If there is no user provided GRADLE_OPTS, use options from run-java.sh
if [ -f ${RUN_JAVA_DIR}/run-java.sh -a -z "${GRADLE_OPTS}" ]; then
export GRADLE_OPTS=$(${RUN_JAVA_DIR}/run-java.sh options)
fi

if [ ! -z "${GRADLE_OPTS}" ]; then
echo "Using GRADLE_OPTS '${GRADLE_OPTS}'"
fi

local old_dir=$(pwd)
cd ${S2I_SOURCE_DIR}
check_error "changing directory to ${S2I_SOURCE_DIR}" $?

# =========
# Run Gradle
echo "Running './gradlew ${gradle_args} ${GRADLE_ARGS_APPEND}'"
./gradlew ${gradle_args} ${GRADLE_ARGS_APPEND}
check_error "Gradle build" $?

# ==============
# Copy artifacts
echo "Copying Gradle artifacts from ${build_dir} to ${app_dir} ..."
copy_artifacts ${build_dir} ${app_dir}
check_error "copying artifacts from ${build_dir} to ${app_dir}" $?

# ======================
# Remove repo if desired
if [ "x${GRADLE_CLEAR_REPO}" != "x" ]; then
rm -rf "${S2I_ARTIFACTS_DIR}/gradle"
check_error "Cannot remove local Gradle repository ${S2I_ARTIFACTS_DIR}/gradle" $?
fi

cd ${old_dir}
}
{{?}}


# =========================================================================
# Main

echo "=================================================================="
echo "Starting S2I Java Build ....."
mkdir -p ${DEPLOYMENTS_DIR}
if [ -f "${S2I_SOURCE_DIR}/pom.xml" ]; then
echo "S2I source build for Maven detected"
build_dir=$(get_output_dir)
build_dir=$(get_maven_output_dir)
check_error "Cannot get output dir: $build_dir" $?
# If a pom.xml is present use maven
setup_maven
Expand All @@ -188,6 +256,13 @@ elif [ -f "${S2I_SOURCE_DIR}/Dockerfile" ]; then
echo "Copying binaries from ${binary_dir} to ${DEPLOYMENTS_DIR} ..."
copy_dir ${binary_dir} ${DEPLOYMENTS_DIR}
check_error "copying ${binary_dir} to ${DEPLOYMENTS_DIR}" $?
{{? fp.config.base.withGradle }}
elif ls ${S2I_SOURCE_DIR}/*.gradle* &> /dev/null; then
echo "S2I source build for Gradle detected, due to presence of a *.gradle* in ${S2I_SOURCE_DIR}"
build_dir=$(get_gradle_output_dir)
check_error "Cannot get output dir: $build_dir" $?
build_gradle ${build_dir} ${DEPLOYMENTS_DIR}
{{?}}
else
echo "S2I source build with plain binaries detected"
if [ -d "${S2I_SOURCE_DIR}/deployments" ]; then
Expand Down
2 changes: 1 addition & 1 deletion java/templates/s2i/s2i-setup
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
S2I_DESTINATION=${S2I_DESTINATION:-/tmp}
S2I_SOURCE_DIR="${S2I_DESTINATION}/src"
S2I_ARTIFACTS_DIR="${S2I_DESTINATION}/artifacts"
DEPLOYMENTS_DIR="/deployments"
DEPLOYMENTS_DIR="${DEPLOYMENTS_DIR:-/deployments}"
RUN_JAVA_DIR=/opt/run-java