-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.clj
93 lines (84 loc) · 3.82 KB
/
build.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
(ns build
"
https://cljdoc.org/d/polylith/clj-poly/0.2.19/doc/build
The build script for the example of the poly documentation.
Targets:
* uberjar :project PROJECT
- creates an uberjar for the given project
For help, run:
clojure -A:deps -T:build help/doc
Create uberjar for command-line:
clojure -T:build uberjar :project command-line"
(:require [clojure.java.io :as io]
[clojure.tools.build.api :as b]
[clojure.tools.deps :as t]
[clojure.tools.deps.util.dir :refer [with-dir]]))
(defn- get-project-aliases
[]
(let [edn-fn (juxt :root-edn :project-edn)]
(-> (t/find-edn-maps)
(edn-fn)
(t/merge-edns)
:aliases)))
(defn- ensure-project-root
"Given a task name and a project name, ensure the project
exists and seems valid, and return the absolute path to it."
[task project]
(let [project-root (str (System/getProperty "user.dir") "/projects/" project)]
(when-not (and project
(.exists (io/file project-root))
(.exists (io/file (str project-root "/deps.edn"))))
(throw (ex-info (str task " task requires a valid :project option")
{:project project})))
project-root))
(defn uberjar
"Builds an uberjar for the specified project.
Options:
* :project - required, the name of the project to build,
* :uber-file - optional, the path of the JAR file to build,
relative to the project folder; can also be specified in
the :uberjar alias in the project's deps.edn file; will
default to target/PROJECT.jar if not specified.
Returns:
* the input opts with :class-dir, :compile-opts, :main, and :uber-file
computed.
The project's deps.edn file must contain an :uberjar alias
which must contain either :main, to build a jar with a main function to run on startup,
or :ns-compile, to build a library jar"
[{:keys [project uber-file], :as opts}]
(let [project-root (ensure-project-root "uberjar" project)
aliases (with-dir (io/file project-root) (get-project-aliases))
{:keys [main ns-compile]} (:uberjar aliases)
to-compile (if (seq ns-compile) ns-compile [main])]
(when-not (or main (seq ns-compile))
(throw
(ex-info
(str
"the "
project
" project's deps.edn file did not specify the :main namespace or any :ns-compile namespaces in its :uberjar alias")
{:aliases aliases})))
(b/with-project-root project-root
(let [class-dir "target/classes"
uber-file (or uber-file
(-> aliases
:uberjar
:uber-file)
(str "target/" project ".jar"))
opts (merge opts
{:basis (b/create-basis),
:class-dir class-dir,
:compile-opts {:direct-linking
true},
:ns-compile to-compile,
:uber-file uber-file}
(when main {:main main}))]
(b/delete {:path class-dir})
;; no src or resources to copy
(println "\nCompiling" to-compile)
(b/compile-clj opts)
(println "Building uberjar" (str uber-file "..."))
(b/uber opts)
(b/delete {:path class-dir})
(println "Uberjar is built.")
opts))))