-
Notifications
You must be signed in to change notification settings - Fork 0
/
package
executable file
·202 lines (166 loc) · 4.18 KB
/
package
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
# [OPTIONS]
# --main-class or -mc <class> : Set main class
# --war or -w : Package as war
# --verbose or -v : Print all logs
#
# [EXIT CODES]
# 0 : Success
# 1 : Compile Error
# 2 : Invalid Argument
# 3 : Unknown Option
# 4 : Invalid Dependency
optwar=0
optverbose=0
dep_group=""
dep_artifact=""
dep_version=""
dep_scope=""
dep_config=""
dep_group_path=""
dep_dir=""
dep_jar=""
dep_pom=""
dep_download_root=""
remote_repo="https://repo1.maven.org/maven2"
info() {
if [ $optverbose -ne 0 ]; then
echo "[INFO] $1"
fi
}
error() {
echo "[ERROR] $1" >&2
}
parse_dependency() {
dep_group=$(xmlstarlet sel -t -v "$1/groupId" $2)
dep_artifact=$(xmlstarlet sel -t -v "$1/artifactId" $2)
dep_version=$(xmlstarlet sel -t -v "$1/version" $2)
dep_scope=$(xmlstarlet sel -t -v "$1/scope" $2)
dep_group_path=${dep_group//.//}
dep_dir="repository/$dep_group_path/$dep_artifact/$dep_version"
dep_jar="$dep_artifact-$dep_version.jar"
dep_pom="$dep_artifact-$dep_version.pom"
dep_download_root="$remote_repo/$dep_group_path/$dep_artifact/$dep_version"
}
keep() {
buf_dep_group=$dep_group
buf_dep_artifact=$dep_artifact
buf_dep_version=$dep_version
buf_dep_group_path=$dep_group_path
buf_dep_dir=$dep_dir
buf_dep_jar=$dep_jar
buf_dep_pom=$dep_pom
buf_dep_download_root=$dep_download_root
}
restore() {
dep_group=$buf_dep_group
dep_artifact=$buf_dep_artifact
dep_version=$buf_dep_version
dep_group_path=$buf_dep_group_path
dep_dir=$buf_dep_dir
dep_jar=$buf_dep_jar
dep_pom=$buf_dep_pom
dep_download_root=$buf_dep_download_root
}
load_props() {
keep
parse_dependency "project/parent" "$dep_dir/$dep_pom"
restore
}
# Get Options
while [ "$1" != "" ]; do
if [ "$1" == "--main-class" -o "$1" == "-mc" ]; then
shift
optmc="--main-class $1"
elif [ "$1" == "--war" -o "$1" == "-w" ]; then
optwar=1
elif [ "$1" == "--verbose" -o "$1" == "-v" ]; then
optverbose=1
elif [[ "$1" == -* ]]; then
error "unknown option '$1'"
exit 3
else
module=$1
break
fi
shift
done
# Get Module Name
if [ "$module" == "" ]; then
error "module name must be given."
echo "Usage: package <module>"
exit 2
fi
info "packaging module '$module'"
# Set Pathes
if [ $optwar -eq 0 ]; then
info "packaging jar"
classpath=target/classes/$module
root=target/classes/$module
dep_root=repository
ext=jar
else
info "packaging war"
classpath=target/webapp/$module/WEB-INF/classes
root=target/webapp/$module
dep_root=target/webapp/$module/WEB-INF/lib
ext=war
webapp_dir=src/$module/webapp
if [ ! -d $webapp_dir ]; then
error "webapp directory does not exsists."
exit 4
fi
mkdir -p $root
cp -R src/$module/webapp/ $root
fi
if [ $optwar -eq 1 ]; then
rm -rf $dep_root
fi
# Resolve Dependencies
dep_config="src/$module/config/dependencies.xml"
if [ -e $dep_config ]; then
mkdir -p $dep_root
index=1
parse_dependency $index
while [ "$dep_group" != "" ]; do
dep_group_path=${dep_group//.//}
dep_dir="repository/$dep_group_path/$dep_artifact/$dep_version"
dep_jar="$dep_artifact-$dep_version.jar"
dep_pom="$dep_artifact-$dep_version.pom"
info "dep_dir: $dep_dir"
create_index $dep_pom
if [ ! -e "$dep_dir/$dep_jar" ]; then
info "dependency not found '$dep_dir/$dep_jar'"
mkdir -p $dep_dir
download_url="$dep_download_root/$dep_jar"
wget -O "$dep_dir/$dep_jar" "$download_url"
if [ $? -eq 8 ]; then
error "Dependency not found: '$download_url'"
exit 5
fi
fi
if [ $optwar -eq 1 ]; then
cp $dep_dir/$dep_jar $dep_root
fi
((index++))
parse_dependency $index
done
fi
# Compilation
java_files=$(find src/$module/java -name *.java)
dep_jars=$(find $dep_root -type f)
dep_jars=${dep_jars//$'\n'/:}
info "dep_jars: $dep_jars"
javac -d $classpath -cp "$dep_jars" $java_files
resources_dir=src/$module/resources
if [ -d $resources_dir ]; then
cp -R $resources_dir/ $classpath
fi
if [ $? -ne 0 ]; then
error "compilation failed."
exit 1
fi
info "compilation completed."
# Packaging
jar --create --file target/$module.$ext $optmc -C $root .
info "archiving completed."