-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
120 lines (96 loc) · 2.43 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# START SECTION
start () {
NAME=spring-boot-hot-reload
if [ "$1$2" == "" ]; then jarDev; fi
if [ "$1$2" == "dev" ]; then jarDev; fi
if [ "$1$2" == "prod" ]; then jarProd; fi
if [ "$1$2" == "jar" ]; then jarDev; fi
if [ "$1$2" == "docker" ]; then dockerDev; fi
if [ "$1 $2" == "jar dev" ]; then jarDev; fi
if [ "$1 $2" == "docker dev" ]; then dockerDev; fi
if [ "$1 $2" == "jar prod" ]; then jarProd; fi
if [ "$1 $2" == "docker prod" ]; then dockerProd; fi
if [ "$1 $2" == "--help " ]; then
usage
else
notACommand "$@"
fi
exit
}
# SCRIPT CONFIGURATION SECTION
jarDev () {
ENV="JAR"
MODE="DEV"
build
}
jarProd () {
ENV="JAR"
MODE="PROD"
build
}
dockerDev () {
ENV="DOCKER"
MODE="DEV"
build
}
dockerProd () {
ENV="DOCKER"
MODE="PROD"
build
}
# BUILD SECTION
build () {
echo "Env: $ENV Mode: $MODE";
if [ $MODE == "DEV" ]; then ./mvnw -Dexclude.devtools=false clean package; fi
if [ $MODE == "PROD" ]; then ./mvnw clean package; fi
if [ $ENV == "JAR" ]; then startJar;
else
buildDocker
fi
}
buildDocker () {
if ! [ -d "target/dependency" ]; then mkdir -p "target/dependency"; fi
cd "target/dependency" || exit
if [ -f "../$NAME-0.0.1-SNAPSHOT.jar" ]; then
jar -xf ../"$NAME"-0.0.1-SNAPSHOT.jar
cd ../.. || exit
docker build -t "$NAME" .
startDocker
fi
}
# START SECTION
startJar () {
if [ $MODE == "DEV" ]; then java -Dspring.profiles.active=dev -jar target/"$NAME"-0.0.1-SNAPSHOT.jar; fi
if [ $MODE == "PROD" ]; then java -jar target/"$NAME"-0.0.1-SNAPSHOT.jar; fi
read -s -r -n 1 -p "Press any key to continue . . .";
}
startDocker () {
docker stop -t 10 "$NAME"
if [ $MODE == "DEV" ]; then docker run --publish=8080:8080 --rm=true --name="$NAME" -e "SPRING_PROFILES_ACTIVE=dev" "$NAME"; fi
if [ $MODE == "PROD" ]; then docker run --publish=8080:8080 --rm=true --name="$NAME" "$NAME"; fi
read -s -r -n 1 -p "Press any key to continue . . ."
docker stop -t 10 "$NAME"
}
# USAGE SECTION
usage () {
echo
echo "Usage: $0 [{jar}|docker] [{dev}|prod]"
echo
echo "Build the application as jar or docker container and run it in development or production mode."
}
# NOT A COMMAND SECTION
notACommand () {
NOT_A_COMMAND="$0: '"
for arg in "$@"; do
i+=1
if [ $i == 1 ]; then
NOT_A_COMMAND+="$arg"
else
NOT_A_COMMAND+=" $arg"
fi
done
NOT_A_COMMAND+="' is not a $0 command."
echo "$NOT_A_COMMAND"
echo "See '$0 --help'"
}
start "$@"