Skip to content

Commit 7abb66d

Browse files
committed
add javac task
1 parent ec5d273 commit 7abb66d

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

boot/core/src/boot/task/built_in.clj

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
[boot.task-helpers :as helpers]
1414
[boot.from.table.core :as table])
1515
(:import
16+
[java.io File]
17+
[java.util Arrays]
18+
[javax.tools ToolProvider DiagnosticCollector Diagnostic$Kind]
1619
[java.util.concurrent LinkedBlockingQueue TimeUnit]))
1720

1821
;; Tasks ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -324,6 +327,39 @@
324327
(util/info "Compiling %s...\n" ns)
325328
(compile ns)))))))
326329

330+
(core/deftask javac
331+
"Compile java sources."
332+
[]
333+
(let [tgt (core/mktgtdir! ::javac-tgt)]
334+
(core/with-pre-wrap
335+
(let [throw? (atom nil)
336+
diag-coll (DiagnosticCollector.)
337+
compiler (ToolProvider/getSystemJavaCompiler)
338+
file-mgr (.getStandardFileManager compiler diag-coll nil nil)
339+
opts (->> ["-d" (.getPath tgt)] (into-array String) Arrays/asList)
340+
handler {Diagnostic$Kind/ERROR util/fail
341+
Diagnostic$Kind/WARNING util/warn
342+
Diagnostic$Kind/MANDATORY_WARNING util/warn}
343+
srcs (some->> (core/src-files)
344+
(core/by-ext [".java"])
345+
seq
346+
(into-array File)
347+
Arrays/asList
348+
(.getJavaFileObjectsFromFiles file-mgr))]
349+
(when srcs
350+
(util/info "Compiling Java classes...\n")
351+
(-> compiler (.getTask *err* file-mgr diag-coll opts nil srcs) .call)
352+
(doseq [d (.getDiagnostics diag-coll) :let [k (.getKind d)]]
353+
(when (= Diagnostic$Kind/ERROR k) (reset! throw? true))
354+
((handler k util/info)
355+
"%s: %s, line %d: %s\n"
356+
(.toString k)
357+
(.. d getSource getName)
358+
(.getLineNumber d)
359+
(.getMessage d nil)))
360+
(.close file-mgr)
361+
(when @throw? (throw (Exception. "java compiler error"))))))))
362+
327363
(core/deftask jar
328364
"Build a jar file for the project."
329365

0 commit comments

Comments
 (0)