Leiningen compiling error with Java files that reference classes generated from Clojure files

纵饮孤独 提交于 2019-12-12 15:28:34

问题


I have a Leiningen project that is compiling both Java and Clojure files. The Java files import some of the classes that are generated from the Clojure files (with gen-class).

When I do lein compile, I get compilation errors on the imports in the Java files (saying it can't find the classes). If I remove the Java files from the project, do lein compile, add the Java files back, and do lein compile again, everything works fine.

So, it's like the Clojure files need to be compiled before the Java files.

By the way, I am including the Clojure files that gen classes in the :aot list.

What is going wrong here?


回答1:


(The code in this answer is untested, although it should hopefully work with Leiningen 1.x (for recent values of x).)

Leiningen's compile task runs the javac task automatically if your project.clj specifies a :java-source-path. It does so prior to compiling Clojure sources, because I suppose that's the usual direction of the dependency.

To get around this, you could use a hook:

;;; in leiningen.hooks.clj_first.clj

(ns leiningen.hooks.clj-first
  (:require [leiningen.compile :as leinc]
            [leiningen.javac :as javac]))

(defn compile-clj-first-hook [compile-task project & args]
  (apply compile-task
         (dissoc project :java-source-path)
         args))
  (javac/javac project))

(add-hook #'leiningen.compile/compile compile-clj-first-hook)

Place this somewhere on your build-time classpath and add

:hooks [clj-first-hook]

to your project map.

Note that javac, when called directly, will still not call compile. You could also make it equivalent to compile e.g. by hooking it with the following function:

(defn javac-hook [javac-task project]
  (if (project ::clj-compiled?)
    (javac/javac project)
    (leinc/compile project)))

The last form of compile-clj-first-hook would then need to be

(javac/javac (assoc project ::clj-compiled? true))

(Making compile not call javac at all would probably break jar / uberjar.)



来源:https://stackoverflow.com/questions/9336202/leiningen-compiling-error-with-java-files-that-reference-classes-generated-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!