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
521 lines (464 loc) · 19.7 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
(ns hf.depstar.uberjar
(:require [clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str])
(:import (java.io File InputStream PushbackReader)
(java.nio.file CopyOption LinkOption OpenOption
StandardCopyOption StandardOpenOption
FileSystem FileSystems Files
FileVisitOption FileVisitResult FileVisitor
Path)
(java.nio.file.attribute FileAttribute FileTime)
(java.util.jar JarInputStream JarEntry))
(:gen-class))
(set! *warn-on-reflection* true)
(def ^:dynamic ^:private *debug* nil)
(def ^:dynamic ^:private *exclude* nil)
(def ^:dynamic ^:private *suppress-clash* 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")]))
(def visit-opts (doto
(java.util.HashSet.)
(.add (FileVisitOption/valueOf "FOLLOW_LINKS"))))
(defonce errors (atom 0))
(defonce multi-release? (atom false))
(defn path
^Path [s]
(.getPath FS s (make-array String 0)))
(def ^:private idiotic-log4j2-plugins-file
"Log4j2 has a very problematic binary plugins cache file that needs to
be merged -- but it's going away in 3.0.0 apparently because it keeps
breaking build tools... As a workaround, if we run into multiple copies
of it, we will let the larger file overwrite the smaller file so that
we are likely to end up with more plugins in the final artifact. This
is not a real solution -- we should rebuild the PluginsCache object
but I don't want to face that right now. What we actually do here is
just allow 'small' versions of this file to be overwritten."
"META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat")
(def ^:private idiotic-log4j2-plugins-size
"This is the threshold up to which we'll allow a duplicate Log4j2Plugins.dat
file to be overwritten. It's arbitrary based on current versions being about
3K in the log4j 1.2 bridge and about 20K in the log4j2 core file."
5000)
(def ^:private ok-to-overwrite-idiotic-log4j2-file
"Assume we can overwrite it until we hit a large version."
(atom true))
(defn clash-strategy
[filename]
(cond
(re-find #"data_readers.clj[sc]?$" filename)
:merge-edn
(re-find #"^META-INF/services/" filename)
:concat-lines
(= idiotic-log4j2-plugins-file filename)
:log4j2-surgery
:else
:noop))
(defmulti clash (fn [filename _in _target]
(let [strategy (clash-strategy filename)]
(when-not *suppress-clash*
(prn {:warning "clashing jar item"
:path filename
:strategy strategy}))
strategy)))
(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
:log4j2-surgery
[filename ^InputStream in ^Path target]
;; we should also set the last mod date/time here but it isn't passed in
;; and I'm not going to make that change just to make this hack perfect!
(if @ok-to-overwrite-idiotic-log4j2-file
(do
(when *debug*
(println "overwriting" filename))
(Files/copy in target ^"[Ljava.nio.file.CopyOption;" copy-opts)
(when (< idiotic-log4j2-plugins-size (Files/size target))
;; we've copied a big enough file, stop overwriting it!
(when *debug*
(println "big enough -- no more copying!"))
(reset! ok-to-overwrite-idiotic-log4j2-file false)))
(when *debug*
(println "ignoring" filename))))
(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"
#"\.keep"
#".*\.pom$" #"module-info\.class$"
#"(?i)META-INF/.*\.(?:MF|SF|RSA|DSA)"
#"(?i)META-INF/(?:INDEX\.LIST|DEPENDENCIES|NOTICE|LICENSE)(?:\.txt)?"])
(defn excluded?
[filename]
(or (some #(re-matches % filename) exclude-patterns)
(some #(re-matches % filename) *exclude*)))
(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 (= idiotic-log4j2-plugins-file filename)
(when *debug*
(println "copied" filename (Files/size target)))
(when (< idiotic-log4j2-plugins-size (Files/size target))
;; we've copied a big enough file, stop overwriting it!
(when *debug*
(println "big enough -- no more copying!"))
(reset! ok-to-overwrite-idiotic-log4j2-file false)))
(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-type options))
(when *verbose* (println src))
(consume-jar (path src)
(fn [inputstream ^JarEntry entry]
(let [^String 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
(when (.startsWith name "META-INF/versions/")
(reset! multi-release? true))
(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 visit-opts Integer/MAX_VALUE 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}))
(defmethod copy-source*
:unknown
[src _dest _options]
(if (excluded? src)
(when *debug* (prn {:excluded src}))
(prn {:warning "ignoring unknown file type" :path src})))
(defn copy-source
[src dest options]
(copy-source* src dest options))
(defn- parse-classpath [^String cp]
(vec (.split cp (System/getProperty "path.separator"))))
(defn- current-classpath
[]
(System/getProperty "java.class.path"))
(defn- depstar-itself?
"We ignore any classpath item that seems to be ourselves.
This might be overly aggressive but no one has complained so far."
[p]
(re-find #"depstar" p))
(defn- first-by-tag
[pom-text tag]
(-> (re-seq (re-pattern (str "<" (name tag) ">([^<]+)</" (name tag) ">")) pom-text)
(first)
(second)))
(defn- copy-pom
"Using the 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 ^File pom-file {:keys [jar-type main-class]}]
(let [pom-text (slurp pom-file)
jdk (str/replace (System/getProperty "java.version")
#"_.*" "")
group-id (first-by-tag pom-text :groupId)
artifact-id (first-by-tag pom-text :artifactId)
version (first-by-tag pom-text :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 @multi-release?
"Multi-Release: true\n")
(when-not (= :thin jar-type)
(str "Main-Class: "
(if main-class
(str/replace 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-file)]
(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 help-and-die []
(println "library usage:")
(println " clojure -A:depstar -m hf.depstar.jar MyProject.jar")
(println "uberjar usage:")
(println " clojure -A:depstar -m hf.depstar.uberjar MyProject.jar")
(println " clojure -A:depstar -m hf.depstar.uberjar -C -m project.core MyProject.jar")
(println "options:")
(println " -C / --compile -- enable AOT compilation for uberjar")
(println " -P / --classpath <cp> -- override classpath")
(println " -h / --help -- show this help (and exit)")
(println " -J / --jar -- an alternative way to specify the JAR file")
(println " -m / --main -- specify the main namespace (or class)")
(println " -n / --no-pom -- ignore pom.xml")
(println " -S / --suppress-clash")
(println " -- suppress warnings about clashing jar items")
(println " -v / --verbose -- explain what goes into the JAR file")
(println " -X / --exclude <regex> -- exclude files via regex")
(println "note: the -C and -m options require a pom.xml file")
(System/exit 1))
(defn run
"Generic entry point for uberjar invocations.
Can be used with `clojure -X`:
In `:aliases`:
```clojure
:depstar {:extra-deps {seancorfield/depstar {:mvn/version ...}}}
:uberjar {:fn hf.depstar.uberjar/run
:args {:aot true}}
```
Then run:
```
clojure -R:depstar -X:uberjar :jar MyProject.jar :main-class project.core
```
If the destination JAR file and main class are fixed, they could be
added to `:args` in `deps.edn`:
```clojure
:depstar {:extra-deps {seancorfield/depstar {:mvn/version ...}}}
:uberjar {:fn hf.depstar.uberjar/run
:args {:aot true :jar MyProject.jar :main-class project.core}}
```
Both `:jar` and `:main-class` can be specified as symbols or strings."
[{:keys [aot classpath exclude help jar jar-type main-class
no-pom suppress-clash verbose]
:or {jar-type :uber}
:as options}]
(when (or help (not jar)) (help-and-die))
(let [jar (some-> jar name) ; ensure we have a string
main-class (some-> main-class name) ; ensure we have a string
options (assoc options ; ensure defaulted/processed options present
:jar jar
:jar-type jar-type
:main-class main-class)
^File
pom-file (io/file "pom.xml")
do-aot
(if main-class
(cond (= :thin jar-type)
(println "Ignoring -m / --main because a 'thin' JAR was requested!")
no-pom
(println "Ignoring -m / --main because -n / --no-pom was specified!")
(not (.exists (io/file "pom.xml")))
(println "Ignoring -m / --main because no 'pom.xml' file is present!")
:else
aot)
(when aot
(println "Ignoring -C / --compile because -m / --main was not specified!")))
tmp-c-dir (when do-aot
(Files/createTempDirectory "depstarc" (make-array FileAttribute 0)))
tmp-z-dir (Files/createTempDirectory "depstarz" (make-array FileAttribute 0))
cp (or classpath (current-classpath))
cp (parse-classpath cp)
cp (into (cond-> [] do-aot (conj (str tmp-c-dir)))
(remove depstar-itself?)
cp)
dest-name (str/replace jar #"^.*[/\\]" "")
jar-path (.resolve tmp-z-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"))]
(when do-aot
(try
(println "Compiling" main-class "...")
(binding [*compile-path* (str tmp-c-dir)]
(compile (symbol main-class)))
(catch Throwable t
(throw (ex-info (str "Compilation of " main-class " failed!")
options
t)))))
(with-open [zfs (FileSystems/newFileSystem jar-file zip-opts)]
(let [tmp (.getPath zfs "/" (make-array String 0))]
(reset! errors 0)
(reset! multi-release? false)
(println "Building" (name jar-type) "jar:" jar)
(binding [*debug* (env-prop "debug")
*exclude* (mapv re-pattern exclude)
*suppress-clash* suppress-clash
*verbose* verbose]
(run! #(copy-source % tmp options) cp)
(when (and (not no-pom) (.exists pom-file))
(copy-pom tmp pom-file options)))))
(let [dest-path (path jar)
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 parse-args
"Returns a hash map with all the options set from command-line args.
This is to avoid an external dependency on `clojure.tools.cli`."
[args]
(let [merge-args (fn [opts new-arg]
(if (contains? new-arg :exclude)
(update opts :exclude (fnil conj []) (:exclude new-arg))
(merge opts new-arg)))]
(loop [opts {} args args]
(if (seq args)
(let [[arg more]
(case (first args)
("-C" "--compile") [{:aot true} (next args)]
("-P" "--classpath") [{:classpath (fnext args)} (nnext args)]
("-h" "--help") [{:help true} (next args)]
("-J" "--jar") [{:jar (fnext args)} (nnext args)]
("-m" "--main") [{:main-class (fnext args)} (nnext args)]
("-n" "--no-pom") [{:no-pom true} (next args)]
("-S" "--suppress-clash") [{:suppress-clash true} (next args)]
("-v" "--verbose") [{:verbose true} (next args)]
("-X" "--exclude") [{:exclude (fnext args)} (nnext args)]
(if (= \- (ffirst args))
(do
(println "Unknown option" (first args) "ignored!")
[{} (next args)])
[{:jar (first args)} (next args)]))]
(recur (merge-args opts arg) more))
opts))))
(defn -main
[& args]
(run (assoc (parse-args args) :jar-type :uber)))
(comment
(parse-args ["foo.jar" "-v"])
(parse-args ["foo.jar" "-V"])
(parse-args ["-X" "a" "foo.jar" "-X" "b"])
nil)