-
Notifications
You must be signed in to change notification settings - Fork 0
/
jar.clj
351 lines (293 loc) · 12 KB
/
jar.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
(ns ^{:author "Jeremy Schoffen"
:doc "
Apis providing the jar sources used by default and the utilities to build jars and uberjars.
"}
fr.jeremyschoffen.mbt.alpha.default.jar
(:require
[clojure.spec.alpha :as s]
[clojure.data.xml :as xml]
[fr.jeremyschoffen.java.nio.alpha.file :as fs]
[fr.jeremyschoffen.mbt.alpha.core :as mbt-core]
[fr.jeremyschoffen.mbt.alpha.core.specs]
[fr.jeremyschoffen.mbt.alpha.default.specs]
[fr.jeremyschoffen.mbt.alpha.utils :as u]))
(u/pseudo-nss
build
build.jar
build.uberjar
classpath
cleaning
jar
jar.entry
jar.manifest
maven
maven.pom
project
project.deps
project.license)
;;----------------------------------------------------------------------------------------------------------------------
;; Jar staple srcs construction
;;----------------------------------------------------------------------------------------------------------------------
;; Manifest
(def meta-dir "META-INF")
(def manifest-name "MANIFEST.MF")
(def manifest-path (fs/path meta-dir manifest-name))
(defn make-manifest-entry
"Make a `:fr...mbt.alpha.jar/entry` for a jar manifest."
[{manifest ::jar/manifest}]
{::jar.entry/src manifest
::jar.entry/dest manifest-path})
(u/spec-op make-manifest-entry
:param {:req [::jar/manifest]}
:ret ::jar/entry)
;;----------------------------------------------------------------------------------------------------------------------
;; Pom
(def maven-dir "maven")
(defn- make-jar-maven-path [group-id artefact-id]
(fs/path meta-dir maven-dir (str group-id) (str artefact-id)))
(defn- make-pom-path [group-id artefact-id]
(fs/path (make-jar-maven-path group-id artefact-id)
"pom.xml"))
(defn make-pom-entry
"Make a `:fr...mbt.alpha.jar/entry` for a `pom.xml` file."
[{pom-path ::maven.pom/path
group-id ::maven/group-id
artefact-id ::maven/artefact-name}]
{::jar.entry/src pom-path
::jar.entry/dest (make-pom-path group-id artefact-id)})
(u/spec-op make-pom-entry
:param {:req [::maven.pom/path
::maven/group-id
::maven/artefact-name]})
(defn- make-pom-props-path [group-id artefact-id]
(fs/path (make-jar-maven-path group-id artefact-id)
"pom.properties"))
(defn make-pom-properties-entry [{pom-props ::maven.pom/properties
group-id ::maven/group-id
artefact-id ::maven/artefact-name}]
{::jar.entry/src pom-props
::jar.entry/dest (make-pom-props-path group-id artefact-id)})
(u/spec-op make-pom-properties-entry
:param {:req [::maven.pom/properties
::maven/group-id
::maven/artefact-name]})
;;----------------------------------------------------------------------------------------------------------------------
;; Deps.edn
(def deps-dir "deps")
(defn- make-jar-deps-path [group-id artefact-id]
(fs/path meta-dir deps-dir (str group-id) (str artefact-id) "deps.edn"))
(defn make-deps-entry
"Make a `:fr...mbt.alpha.jar/entry` for a `deps.edn` file."
[{deps-file ::project.deps/file
group-id ::maven/group-id
artefact-id ::maven/artefact-name}]
{::jar.entry/src deps-file
::jar.entry/dest (make-jar-deps-path group-id artefact-id)})
(u/spec-op make-deps-entry
:param {:req [::project.deps/file
::maven/group-id
::maven/artefact-name]})
;;----------------------------------------------------------------------------------------------------------------------
;; License file
(defn- license-file->entry [group-id artefact-id p]
(let [name (-> p fs/file-name str)]
{::jar.entry/src p
::jar.entry/dest (fs/path meta-dir "licenses" (str group-id) (str artefact-id) name)}))
(defn make-license-entries [{group-id ::maven/group-id
artefect-name ::maven/artefact-name
licenses ::project/licenses}]
(into []
(comp
(keep ::project.license/file)
(map #(license-file->entry group-id artefect-name %)))
(or licenses [])))
;; Pom + manifest + deps
(defn make-staples-entries
"Make a `:fr...mbt.alpha.jar/src` containing the usual manifest, pom.xml and deps.edn `jar/entry`s."
[param]
(into [(-> param make-manifest-entry)
(-> param make-pom-entry)
(-> param make-pom-properties-entry)
(-> param make-deps-entry)]
(make-license-entries param)))
(u/spec-op make-staples-entries
:deps [make-manifest-entry
make-pom-entry
make-pom-properties-entry
make-deps-entry
make-license-entries]
:param {:req [::jar/manifest
::maven/artefact-name
::maven/group-id
::maven.pom/xml
::maven.pom/properties
::project/deps]
:opt [::project/licenses]}
:ret ::jar/src)
;;----------------------------------------------------------------------------------------------------------------------
;; Src from classpath
;;----------------------------------------------------------------------------------------------------------------------
(defn- warn-wayward-files [{cp ::classpath/index}]
(let [wayward-files (::classpath/file cp)]
(when-let [files (seq wayward-files)]
(binding [*out* *err*]
(println "Wayward files on classpath")
(doseq [f files]
(println f))))))
(defn- classpath->sources [cp ks]
(-> cp
(select-keys ks)
vals
(->> (into [] (comp
cat
(map u/safer-path))))))
(defn simple-jar-srcs
"Make the jar srcs used in a skinny jar. Basically all the project local sources and resources directories present in
the classpath."
[{cp ::classpath/index
:as param}]
(warn-wayward-files param)
(into [(make-staples-entries param)]
(classpath->sources cp #{::classpath/dir})))
(u/spec-op simple-jar-srcs
:deps [make-staples-entries]
:param {:req [::classpath/index
::jar/manifest
::maven/artefact-name
::maven/group-id
::maven.pom/xml
::maven.pom/properties
::project/deps]
:opt [::project/licenses]}
:ret ::jar/srcs)
(defn uber-jar-srcs
"Make the jar srcs that will go into an uberjar. Similar to
[[fr.jeremyschoffen.mbt.alpha.default.jar/simple-jar-srcs]] but also adds the other sources present in the classpath."
[{cp ::classpath/index
:as param}]
(warn-wayward-files param)
(into [(make-staples-entries param)]
(classpath->sources cp #{::classpath/dir
::classpath/ext-dep
::classpath/jar})))
(u/spec-op uber-jar-srcs
:deps [make-staples-entries classpath->sources]
:param {:req [::classpath/index
::jar/manifest
::maven/artefact-name
::maven/group-id
::maven.pom/xml
::maven.pom/properties
::project/deps]
:opt [:fr.jeremyschoffen.mbt.alpha.project/licenses]}
:ret ::jar/srcs)
;;----------------------------------------------------------------------------------------------------------------------
;; Jar building
;;----------------------------------------------------------------------------------------------------------------------
(defn ensure-jar-defaults
"Ensure the presence in the config map of some necessary keys to make a jar. Computes
values for those keys if necessary.
Namely:
- :fr...mbt.alpha.classpath/index
- :fr...mbt.alpha.maven.pom/xml
- :fr...mbt.alpha.maven.pom/properties
- :fr...mbt.alpha.jar/manifest"
[p]
(u/ensure-computed p
::classpath/index mbt-core/classpath-indexed
::maven.pom/xml mbt-core/maven-read-pom
::maven.pom/properties mbt-core/maven-new-pom-properties
::jar/manifest mbt-core/manifest))
(u/spec-op ensure-jar-defaults
:deps [mbt-core/classpath-indexed
mbt-core/maven-read-pom
mbt-core/maven-new-pom-properties
mbt-core/manifest]
:param {:req #{::maven/artefact-name
::maven/group-id
::maven.pom/path
::project/deps
::project/working-dir
::project/version}
:opt #{::jar/main-ns
::jar.manifest/overrides
::maven/scm
::project/author
::project.deps/aliases
::project/licenses}}
:ret (s/keys :req [::classpath/index
::maven.pom/xml
::maven.pom/properties
::jar/manifest]))
(defn make-jar&clean!
"Create a jar, simplifying the process by handling the creation and deletion of the temporary output put that will
be zipped into the resulting jar."
[{jar-out ::jar/output
:as param}]
(let [res (atom nil)
out-dir (-> jar-out fs/parent u/ensure-dir!)
_ (fs/delete-if-exists! jar-out)
temp-out (fs/create-temp-directory! "temp-out_" {:dir out-dir})]
(-> param
(assoc ::jar/temp-output temp-out
::cleaning/target temp-out)
(u/side-effect! #(reset! res (mbt-core/jar-add-srcs! %)))
(u/do-side-effect! mbt-core/jar-make-archive!)
(u/do-side-effect! mbt-core/clean!))
@res))
(u/spec-op make-jar&clean!
:deps [mbt-core/jar-add-srcs!
mbt-core/jar-make-archive!
mbt-core/clean!]
:param {:req [::jar/output
::jar/srcs
::project/working-dir]
:opt [::jar/exclude?]})
(defn jar!
"Create a skinny jar. The jar sources are determined using
[[fr.jeremyschoffen.mbt.alpha.default.jar/simple-jar-srcs]].
This function takes care of generating a deleting a temporary directory used to group the
jar files that end up compressed into the jar archive."
[param]
(-> param
(u/assoc-computed ::jar/output ::build.jar/path)
(u/ensure-computed ::jar/srcs simple-jar-srcs)
make-jar&clean!))
(u/spec-op jar!
:deps [simple-jar-srcs
make-jar&clean!]
:param {:req [::build.jar/path
::classpath/index
::jar/manifest
::maven/artefact-name
::maven/group-id
::maven.pom/xml
::maven.pom/properties
::project/deps
::project/working-dir]
:opt [::jar/exclude?
::project/licences]})
(defn uberjar!
"Build an uberjar. The jar sources are determined using
[[fr.jeremyschoffen.mbt.alpha.default.jar/uber-jar-srcs]].
This function takes care of generating a deleting a temporary directory used to group the
jar files that end up compressed into the jar archive."
[param]
(-> param
(u/ensure-computed ::jar/srcs uber-jar-srcs)
(u/assoc-computed ::jar/output ::build.uberjar/path)
make-jar&clean!))
(u/spec-op uberjar!
:deps [uber-jar-srcs
make-jar&clean!]
:param {:req [::build.uberjar/path
::classpath/index
::jar/manifest
::maven/artefact-name
::maven/group-id
::maven.pom/xml
::maven.pom/properties
::project/deps
::project/working-dir]
:opt [::jar/exclude?
::project/licenses]})