如何在leiningen中的Clojure代码之后编译Java代码

时间:2014-10-17 07:37:51

标签: java clojure compilation leiningen gen-class

在我的莱宁根项目中:

(defproject com.stackoverflow.clojure/tests "0.1.0-SNAPSHOT"
  :description "Tests of Clojure test-framework."
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.6.0"]
                 [instaparse "1.3.4"]]
  :aot [com.stackoverflow.clojure.testGenClass]
  :source-paths      ["src/main/clojure"]
  :java-source-paths ["src/main/java"]
  :test-paths        ["src/test/clojure"]
  :java-test-paths   ["src/test/java"]
  )

我使用gen-class生成Java类:

(ns com.stackoverflow.clojure.testGenClass
  (:gen-class
     :name com.stackoverflow.clojure.TestGenClass
     :implements [com.stackoverflow.clojure.TestGenClassInterface]
     :prefix "java-"))

(def ^:private pre "START: ")

(defn java-addToString [this text post]
  (str pre text post))

我想在Java中使用:

package com.stackoverflow.clojure;

public class TestGenClassTest {

    private TestGenClassTest() {
    }

    public static void main(String[] args) {
        TestGenClassInterface gc = new TestGenClass();
        System.out.println(gc.addToString("Called from Java!", " :END"));
    }
}

启动lein compile会引发以下错误:

Compiling 4 source files to /home/eddy/workspace/TestSkripts/target/classes
/home/eddy/workspace/TestSkripts/src/main/java/com/stackoverflow/clojure/TestGenClassTest.java:9: error: cannot find symbol
        TestGenClassInterface gc = new TestGenClass();
                                       ^
  symbol:   class TestGenClass
  location: class TestGenClassTest
1 error

在我看来,在编译Java代码期间(这里:TestGenClassTest),Class不可用。我通常做的是

  1. 注释掉那些使用gen-class生成类的部分(这里是TestGenClass
  2. 运行lein compile(生成类文件)
  3. 然后再次使用已注释的代码
  4. 并再次运行lein compile
  5. 我'确保有更好的方法,这使得所有手动步骤都被裁减。

3 个答案:

答案 0 :(得分:1)

您想要添加预编译步骤。运行预编译...然后你很高兴。

  1. 将您的界面置于单独的文件路径src/main/pre/interface/clojure

  2. 将以下内容添加到:profiles

    (defproject com.stackoverflow.clojure/tests "0.1.0-SNAPSHOT"
     :description "Tests of Clojure test-framework."
     :url "http://example.com/FIXME"
     :dependencies [[org.clojure/clojure "1.6.0"]
                 [instaparse "1.3.4"]]
     :source-paths      ["src/main/clojure"]
     :java-source-paths ["src/main/java"]
     :test-paths        ["src/test/clojure"]
     :java-test-paths   ["src/test/java"]
     :profiles { :precomp { :source-paths ["src/main/pre/interface/clojure"]
                         :aot [parser.ast] } })
    
  3. 然后,您可以lein with-profile precomp compile运行lein test

答案 1 :(得分:0)

让我再次推荐给Polyglot (Clojure, Java) Projects With Leiningen,尤其是这部分:Interleaving Compilation Steps

默认情况下,Leiningen正在执行javac,然后执行compile。您还可以实现compile - > javac - > compile

答案 2 :(得分:0)

这是一个项目的示例project.clj,其中clojure代码依赖于Java代码,AOT编译以便上游测试Java代码。所以这里我们有三层编译依赖:Java→Clojure→Java,它们都可以工作:

(defproject myproject "0.1.0-SNAPSHOT"
  :min-lein-version "2.0.0"
  :source-paths      ["src/clojure"]/
  :java-source-paths ["src/java"]
  :javac-options     ["-target" "1.8" "-source" "1.8"]
  :dependencies [[org.clojure/clojure "1.8.0"]]
  :aot [the class that java-test needs]
  :profiles {:java-tests-compile
    {:java-source-paths ["src/java-test"]}}
  :aliases {
    "java-tests" ["do" "compile," "with-profile" "java-tests-compile" "javac," "run" "-m" "myorg.myProject.java-test-code"]
    "all-tests" ["do" "test," "java-tests"]
  })

; https://github.com/technomancy/leiningen/issues/847#issuecomment-289943710

上游java测试代码在编译和运行时找到所需的全部内容。 (此示例来自的项目中的上游java层恰好是java测试代码,但这里没有任何特定于测试代码的技术,当您希望Java代码使用生成的gen-class时,该技术应该一般工作clojure代码)。

原始问题的代码只是缺少此处显示的配置文件和别名部分,告诉leiningen如何在clojure编译之后编译Java。这是必需的,因为默认情况下,在leiningen的当前版本中,leiningen在编译clojure之前编译Java;它不会尝试跨语言编写源文件的编译。

因此,在此解决方案中,我们通过使用别名将另一个编译步骤添加为单独的leiningen任务。有了这个配置,我们只需要发出首先编译clojure的lein java-tests,然后是上游Java层。瞧。

P.S。也许我们甚至可以覆盖lein的编译任务而不是添加一个新任务,但这对我来说不值得玩......

相关问题