forked from clarin-eric/VLO
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sh
executable file
·97 lines (81 loc) · 2.33 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env bash
#####################################################################################
# Script for building the VLO without depending on a local Java / Maven environment.
# Requires docker to be installed and available to the current user!
#
# The script will do a "mvn clean install". Any options passed to the script will be
# appended. For example, run as follows:
#
# ./build.sh -DskipTests=true -Pdocker
#
# By default a Maven cache is persisted through a volume. To reset the cache, export
# the CLEAN_CACHE property. For example:
#
# CLEAN_CACHE=true ./build.sh -Pdevelop
#
#####################################################################################
#configuration
APP_NAME="vlo"
MAVEN_IMAGE="maven:3.6.3-jdk-11-slim"
CLEAN_CACHE=${CLEAN_CACHE:-false}
SCRIPT_DIR="$( cd "$(dirname "$0")" ; pwd -P )"
MAVEN_CONFIG_IMAGE="${APP_NAME}-maven-build-cache"
MAVEN_CONFIG_DIR="/root/.m2"
BUILD_CONTAINER_NAME="${APP_NAME}-maven-build"
MAVEN_OPTS="$@"
MAVEN_CMD="mvn clean install ${MAVEN_OPTS}"
if ! [ "${JAVA_SRC_DIR}" ]; then
JAVA_SRC_DIR="$( cd ${SCRIPT_DIR} ; pwd -P )"
fi
if ! [ -d "${JAVA_SRC_DIR}" ]; then
echo "Source directory ${JAVA_SRC_DIR} not found"
exit 1
fi
#### MAIN FUNCTIONS
main() {
check_docker
pull_image
prepare_cache_volume
echo "Source dir: ${JAVA_SRC_DIR}"
echo "Maven command: ${MAVEN_CMD}"
echo "Build image: ${MAVEN_IMAGE}"
docker_run
}
#### HELPER FUNCTIONS
check_docker() {
if ! which docker > /dev/null; then
echo "Docker command not found"
exit 1
fi
}
pull_image() {
if ! docker pull "${MAVEN_IMAGE}"; then
echo "Failed to pull Maven image for build"
exit 1
fi
}
prepare_cache_volume() {
if docker volume ls -f "name=${MAVEN_CONFIG_IMAGE}"|grep "${MAVEN_CONFIG_IMAGE}"; then
if ${CLEAN_CACHE}; then
echo "Removing Maven cache volume ${MAVEN_CONFIG_IMAGE}"
docker volume rm "${MAVEN_CONFIG_IMAGE}"
else
echo "Using existing Maven cache volume ${MAVEN_CONFIG_IMAGE}"
fi
else
echo "Creating Maven cache volume ${MAVEN_CONFIG_IMAGE}"
docker volume create "${MAVEN_CONFIG_IMAGE}"
fi
}
docker_run() {
docker run \
--rm \
--name "${BUILD_CONTAINER_NAME}" \
-v "${MAVEN_CONFIG_IMAGE}":"${MAVEN_CONFIG_DIR}" \
-e MAVEN_CONFIG="${MAVEN_CONFIG_DIR}" \
-v "${JAVA_SRC_DIR}":/var/src \
-w /var/src \
"${MAVEN_IMAGE}" ${MAVEN_CMD}
}
# Execute main
main