This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
forked from healthfinch/depstar
-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
uberjar.clj
324 lines (287 loc) · 11.5 KB
/
uberjar.clj
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
(ns hf.depstar.uberjar
(:require [clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.xml :as xml])
(:import (java.io InputStream PushbackReader)
(java.nio.file CopyOption LinkOption OpenOption
StandardCopyOption StandardOpenOption
FileSystem FileSystems Files
FileVisitResult FileVisitor
Path)
(java.nio.file.attribute FileAttribute FileTime)
(java.util.jar JarInputStream JarEntry)))
(set! *warn-on-reflection* true)
(def ^:dynamic ^:private *debug* nil)
(def ^:dynamic ^:private *verbose* nil)
(defn env-prop
"Given a setting name, get its Boolean value from the environment,
validate it, and return the value (or nil if no setting is present)."
[setting]
(let [env-setting (str "DEPSTAR_" (str/upper-case setting))
prop-setting (str "depstar." (str/lower-case setting))]
(when-let [level (or (System/getenv env-setting)
(System/getProperty prop-setting))]
(case level
"true" true
"false" false ;; because (if (Boolean. "false") :is-truthy :argh!)
(throw (ex-info (str "depstar " setting
" should be true or false")
{:level level
:env (System/getenv env-setting)
:property (System/getProperty prop-setting)}))))))
(defonce ^FileSystem FS (FileSystems/getDefault))
;; could add (StandardOpenOption/valueOf "SYNC") here as well but that
;; could slow things down (and hasn't yet proved to be necessary)
(def open-opts (into-array OpenOption [(StandardOpenOption/valueOf "CREATE")]))
(def copy-opts (into-array CopyOption [(StandardCopyOption/valueOf "REPLACE_EXISTING")]))
(defonce errors (atom 0))
(defn path
^Path [s]
(.getPath FS s (make-array String 0)))
(defn clash-strategy
[filename]
(cond
(= "data_readers.clj" filename)
:merge-edn
(re-find #"^META-INF/services/" filename)
:concat-lines
:else
:noop))
(defmulti clash (fn [filename in target]
(let [stategy (clash-strategy filename)]
(prn {:warning "clashing jar item" :path filename :strategy stategy})
stategy)))
(defmethod clash
:merge-edn
[_ in target]
(let [;; read but do not close input stream
f1 (edn/read (PushbackReader. (io/reader in)))
;; read and then close target since we will rewrite it
f2 (with-open [r (PushbackReader. (Files/newBufferedReader target))]
(edn/read r))]
(with-open [w (Files/newBufferedWriter target open-opts)]
(binding [*out* w]
(prn (merge f1 f2))))))
(defmethod clash
:concat-lines
[_ in target]
(let [f1 (line-seq (io/reader in))
f2 (Files/readAllLines target)]
(with-open [w (Files/newBufferedWriter target open-opts)]
(binding [*out* w]
(run! println (-> (vec f1)
(conj "\n")
(into f2)))))))
(defmethod clash
:default
[_ in target]
;; do nothing, first file wins
nil)
(def ^:private exclude-patterns
"Filename patterns to exclude. These are checked with re-matches and
should therefore be complete filename matches including any path."
[#"project.clj"
#"LICENSE"
#"COPYRIGHT"
#".*\.pom$" #"module-info\.class$"
#"(?i)META-INF/.*\.(?:MF|SF|RSA|DSA)"
#"(?i)META-INF/(?:INDEX\.LIST|DEPENDENCIES|NOTICE|LICENSE)(?:\.txt)?"])
(defn excluded?
[filename]
(some #(re-matches % filename) exclude-patterns))
(defn copy!
;; filename drives strategy
[filename ^InputStream in ^Path target last-mod]
(if-not (excluded? filename)
(if (Files/exists target (make-array LinkOption 0))
(clash filename in target)
(do
(Files/copy in target ^"[Ljava.nio.file.CopyOption;" copy-opts)
(when last-mod
(Files/setLastModifiedTime target last-mod))))
(when *debug*
(prn {:excluded filename}))))
(defn consume-jar
[^Path path f]
(with-open [is (-> path
(Files/newInputStream (make-array OpenOption 0))
java.io.BufferedInputStream.
JarInputStream.)]
(loop []
(when-let [entry (.getNextJarEntry is)]
(f is entry)
(recur)))))
(defn classify
[entry]
(let [p (path entry)
symlink-opts (make-array LinkOption 0)]
(if (Files/exists p symlink-opts)
(cond
(Files/isDirectory p symlink-opts)
:directory
(and (Files/isRegularFile p symlink-opts)
(re-find #"\.jar$" (.toString p)))
:jar
:else :unknown)
:not-found)))
(defmulti copy-source*
(fn [src dest options]
(classify src)))
(defmethod copy-source*
:jar
[src dest options]
(when-not (= :thin (:jar options))
(when *verbose* (println src))
(consume-jar (path src)
(fn [inputstream ^JarEntry entry]
(let [name (.getName entry)
last-mod (.getLastModifiedTime entry)
target (.resolve ^Path dest name)]
(if (.isDirectory entry)
(Files/createDirectories target (make-array FileAttribute 0))
(do (Files/createDirectories (.getParent target) (make-array FileAttribute 0))
(try
(copy! name inputstream target last-mod)
(catch Throwable t
(prn {:error "unable to copy file"
:name name
:exception (class t)
:message (.getMessage t)})
(swap! errors inc))))))))))
(defn copy-directory
[^Path src ^Path dest]
(let [copy-dir
(reify FileVisitor
(visitFile [_ p attrs]
(let [f (.relativize src p)
last-mod (Files/getLastModifiedTime p (make-array LinkOption 0))]
(when *verbose* (println (str (.toString src) "/" (.toString f))))
(with-open [is (Files/newInputStream p (make-array OpenOption 0))]
(copy! (.toString f) is (.resolve dest (.toString f)) last-mod)))
FileVisitResult/CONTINUE)
(preVisitDirectory [_ p attrs]
(Files/createDirectories (.resolve dest (.toString (.relativize src p)))
(make-array FileAttribute 0))
FileVisitResult/CONTINUE)
(postVisitDirectory [_ p ioexc]
(if ioexc (throw ioexc) FileVisitResult/CONTINUE))
(visitFileFailed [_ p ioexc] (throw (ex-info "Visit File Failed" {:p p} ioexc))))]
(Files/walkFileTree src copy-dir)
:ok))
(defmethod copy-source*
:directory
[src dest options]
(when *verbose* (println src))
(copy-directory (path src) dest))
(defmethod copy-source*
:not-found
[src _dest _options]
(prn {:warning "could not find classpath entry" :path src}))
(defn copy-source
[src dest options]
(copy-source* src dest options))
(defn current-classpath
[]
(vec (.split ^String
(System/getProperty "java.class.path")
(System/getProperty "path.separator"))))
(defn depstar-itself?
[p]
(re-find #"depstar" p))
(defn- first-by-tag
[pom-data tag]
(some-> (filter #(= tag (:tag %)) pom-data)
(first)
(:content)
(first)
(str/trim)))
(defn- maybe-copy-pom
"If there is a pom.xml file in the current directory, build a manifest
and pom.properties, and add both those and the pom.xml file to the JAR."
[^Path dest options]
(let [pom (io/file "pom.xml")]
(when (.exists pom)
(let [pom-data (:content (xml/parse pom))
jdk (str/replace (System/getProperty "java.version")
#"_.*" "")
group-id (first-by-tag pom-data :groupId)
artifact-id (first-by-tag pom-data :artifactId)
version (first-by-tag pom-data :version)
build-now (java.util.Date.)
last-mod (FileTime/fromMillis (.getTime build-now))
manifest (str "Manifest-Version: 1.0\n"
"Built-By: depstar\n"
"Build-Jdk: " jdk "\n"
(when-not (= :thin (:jar options))
"Main-Class: clojure.main\n"))
properties (str "#Generated by depstar\n"
"#" build-now "\n"
"version: " version "\n"
"groupId: " group-id "\n"
"artifactId: " artifact-id "\n")
maven-dir (str "META-INF/maven/" group-id "/" artifact-id "/")]
(when-not (and group-id artifact-id version)
(throw (ex-info "Unable to read pom.xml file!"
{:group-id group-id
:artifact-id artifact-id
:version version})))
(when *verbose* (println ""))
(println "Processing pom.xml for"
(str "{" group-id "/" artifact-id
" {:mvn/version \"" version "\"}}"))
(with-open [is (io/input-stream (.getBytes manifest))]
(when *verbose*
(println "\nGenerating META-INF/MANIFEST.MF:\n")
(print manifest))
(let [target (.resolve dest "META-INF/MANIFEST.MF")]
(Files/createDirectories (.getParent target) (make-array FileAttribute 0))
(copy! "MANIFEST.MF" is target last-mod)))
(with-open [is (io/input-stream (.getBytes properties))]
(when *verbose*
(println (str "\nGenerating " maven-dir "pom.properties:\n"))
(print properties))
(let [target (.resolve dest (str maven-dir "pom.properties"))]
(Files/createDirectories (.getParent target) (make-array FileAttribute 0))
(copy! "pom.properties" is target last-mod)))
(with-open [is (io/input-stream pom)]
(when *verbose*
(println (str "\nCopying pom.xml to " maven-dir "pom.xml")))
;; we don't need the createDirectories call here
(copy! "pom.xml" is
(.resolve dest (str maven-dir "pom.xml"))
last-mod))))))
(defn run
[{:keys [dest jar verbose no-pom]
:or {jar :uber}
:as options}]
(let [cp (into [] (remove depstar-itself?) (current-classpath))
tmp-dir (Files/createTempDirectory "depstar" (make-array FileAttribute 0))
dest-name (str/replace dest #"^.*[/\\]" "")
jar-path (.resolve tmp-dir ^String dest-name)
jar-file (java.net.URI. (str "jar:" (.toUri jar-path)))
zip-opts (doto (java.util.HashMap.)
(.put "create" "true")
(.put "encoding" "UTF-8"))]
(with-open [zfs (FileSystems/newFileSystem jar-file zip-opts)]
(let [tmp (.getPath zfs "/" (make-array String 0))]
(reset! errors 0)
(println "Building" (name jar) "jar:" dest)
(binding [*debug* (env-prop "debug")
*verbose* verbose]
(run! #(copy-source % tmp options) cp)
(when-not no-pom
(maybe-copy-pom tmp options)))))
(let [dest-path (path dest)
parent (.getParent dest-path)]
(when parent (.. parent toFile mkdirs))
(Files/move jar-path dest-path copy-opts))
(when (pos? @errors)
(println "\nCompleted with errors!")
(System/exit 1))))
(defn -main
[destination & args]
(let [verbose (some #(#{"-v" "--verbose"} %) args)
no-pom (some #(#{"-n" "--no-pom"} %) args)]
(run {:dest destination :jar :uber
:verbose verbose :no-pom no-pom})))