-
Notifications
You must be signed in to change notification settings - Fork 79
/
build.clj
67 lines (64 loc) · 2.73 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
(ns build
"The build script for the Polylith project.
Primary targets:
* uberjar :project PROJECT
- creates an uberjar for the given project
For help, run:
clojure -A:deps -T:build help/doc"
(: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]]
[org.corfield.build :as bb]))
(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 at least :main, specifying the main ns
(to compile and to invoke)."
[{:keys [project uber-file] :as opts}]
(let [project-root (ensure-project-root "uberjar" project)
aliases (with-dir (io/file project-root) (get-project-aliases))
main (-> aliases :uberjar :main)]
(when-not main
(throw (ex-info (str "the " project " project's deps.edn file does not specify the :main namespace in its :uberjar alias")
{:aliases aliases})))
(binding [b/*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
{:class-dir class-dir
:compile-opts {:direct-linking true}
:main main
:uber-file uber-file})]
(b/delete {:path class-dir})
(bb/uber opts)
(b/delete {:path class-dir})
(println "Uberjar is built.")
opts))))