talios / clojure-maven-plugin

Apache Maven Mojo for compiling clojure scripts to class files
252 stars 74 forks source link

Use AOT compiled clojure code in Java #85

Open ishan opened 10 years ago

ishan commented 10 years ago

Currently, even with packaging specified as Clojure, the maven compiler plugin is run before clojure compiler plugin, making classes generated from clojure using :gen-class unavailable for linking.

Would it be possible to generate the clojure class files first and add them to the maven java compiler classpath when it is running?

talios commented 10 years ago

...and then the Java classes won't be able to refer to the clojure. Catch22.

If you add a <plugin> declaration for the maven-compiler-plugin after the clojure-maven-plugin it should, in theory run the clojure compilation first.

Does this work for you?

ishan commented 10 years ago

I agree it is a catch 22, but a thread reports something funny here:

Even if a java class that is needed is not present, the clojure class is generated (albeit with a warning/error). I looked at the compilation section on clojure's website, and looks like gen-class only generates a stub class, and the implementation is only compiled or linked at runtime.

I guess I am trying to hint towards this: Given the dynamic nature of clojure and the delayed compilation, it is possible to generate the :gen-class stub, not compile the clojure code without any warnings since the java code is not present but it is not really needed also, and then compile the java code that depends on the clojure gen-class.

My understand is limited here, but does clojure:compile goal AOT compile all the clojure code? I remember reading that mostly clojure code is submitted as source in jar and compiled at runtime.

Adding maven-compiler-plugin after clojure-maven-plugin doesn't work since maven-compiler-plugin binds automatically to the compile phase when packaging is jar or clojure and always comes first somehow. The only option is to change packing to pom and then this works.