如何在Clojure REPL中从maven项目加载java类?

时间:2014-09-25 17:50:45

标签: java maven clojure leiningen clojure-maven-plugin

有没有人知道在Clojure REPL中从maven项目加载java类的简单方法?

我看了一下lein docs,他们似乎不支持maven。

clojure-maven-plugin提供了一个clojure:repl目标,但它似乎只是加载了项目的依赖项,而不是我的src / main / java中的类。

理想情况下,我不愿意为此任务设置lein项目,因为我只想玩我项目中的一些java类。

任何?

2 个答案:

答案 0 :(得分:3)

lein支持要求来自maven repos的依赖项。事实上,这是中心lein功能之一,如果不是。

您可以使用pallet/alembic来获取和加载maven依赖项是来自repl的运行时。

由于我~/.lein/profiles.clj

中的以下内容,我可以使用托盘/蒸馏器
 {:user
  {:dependencies [[clojure-complete "0.2.3"]
                  [lein-pdo "0.1.1"]
                  [criterium "0.4.2"]
                  [org.clojure/tools.trace "0.7.6"]
                  [alembic "0.2.0"]]}}

这意味着我可以使用任何repl中的alembic

user> (require '[alembic.still :as still])
nil
user> (org.apache.giraph.GiraphRunner.) ; create a new GiraphRunner
CompilerException java.lang.ClassNotFoundException: org.apache.giraph.GiraphRunner, compiling:(/tmp/form-init5282725623346658314.clj:1:50) 
哎呀,我实际上没有那个班级

user> (still/distill '[[org.apache.giraph/giraph-core "1.0.0"]] :repositories [["apache" "http://obelix.ics.uci.edu/nexus/content/groups/hyracks-public-releases"]])
WARN: com.google.guava/guava version 12.0 requested, but 14.0.1 already on classpath.
Loaded dependencies:
[[commons-cli "1.2"]
 [commons-io "2.1"]
 [jline "0.9.94"]
 [junit "3.8.1"]
 [log4j "1.2.15"]
 [com.google.code.findbugs/jsr305 "1.3.9"]
 [com.yammer.metrics/metrics-core
  "2.2.0"
  :exclusions
  [[org.slf4j/slf4j-api]]]
 [io.netty/netty "3.5.3.Final"]
 [it.unimi.dsi/fastutil "6.5.3"]
 [javax.activation/activation "1.1"]
 [javax.mail/mail "1.4"]
 [net.iharder/base64 "2.3.8"]
 [org.apache.giraph/giraph-core "1.0.0"]
 [org.apache.zookeeper/zookeeper
  "3.3.3"
  :exclusions
  [[com.sun.jmx/jmxri] [com.sun.jdmk/jmxtools] [javax.jms/jms]]]
 [org.codehaus.jackson/jackson-core-asl "1.8.0"]
 [org.codehaus.jackson/jackson-mapper-asl "1.8.0"]
 [org.json/json "20090211"]
 [org.slf4j/slf4j-api "1.7.2"]
 [org.slf4j/slf4j-log4j12 "1.7.2"]]
Dependencies not loaded due to conflict with previous jars :
[[com.google.guava/guava "12.0"]]
nil

好的,现在我们有了giraph

user> (org.apache.giraph.GiraphRunner.) ; create a new GiraphRunner
CompilerException java.lang.NoClassDefFoundError: org/apache/hadoop/util/Tool, compiling:(/tmp/form-init5282725623346658314.clj:1:50) 
user> (still/distill '[[org.apache.hadoop/hadoop-core "0.20.2"]])
WARN: commons-logging version 1.0.3 requested, but 1.1.1 already on classpath.
WARN: commons-codec version 1.3 requested, but 1.6 already on classpath.
Loaded dependencies:
[[ant "1.6.5"]
 [commons-cli "1.2"]
 [commons-el "1.0"]
 [commons-httpclient "3.0.1"]
 [commons-net "1.4.1"]
 [hsqldb "1.8.0.10"]
 [junit "4.5"]
 [oro "2.0.8"]
 [xmlenc "0.52"]
 [net.java.dev.jets3t/jets3t "0.7.1"]
 [net.sf.kosmosfs/kfs "0.3"]
 [org.apache.hadoop/hadoop-core "0.20.2"]
 [org.eclipse.jdt/core "3.1.1"]
 [org.mortbay.jetty/jetty "6.1.14"]
 [org.mortbay.jetty/jetty-util "6.1.14"]
 [org.mortbay.jetty/jsp-2.1 "6.1.14"]
 [org.mortbay.jetty/jsp-api-2.1 "6.1.14"]
 [org.mortbay.jetty/servlet-api-2.5 "6.1.14"]
 [tomcat/jasper-compiler "5.5.12"]
 [tomcat/jasper-runtime "5.5.12"]]
Dependencies not loaded due to conflict with previous jars :
[[commons-codec "1.3"] [commons-logging "1.0.3"]]
nil
user> (org.apache.giraph.GiraphRunner.) ; create a new GiraphRunner
#<GiraphRunner org.apache.giraph.GiraphRunner@2dbfe923>
user> (bean *1)
{:conf nil, :class org.apache.giraph.GiraphRunner}

因此,在获取必需的依赖项之后,我们可以通过maven存储库创建和试验大多数可用的类。

在您的特定情况下,您可以通过maven在本地安装您的Java项目,然后使用alembic将其加载到repl中进行实验。

答案 1 :(得分:0)

事实证明,设置lein项目比我想象的要简单。

要在REPL上加载我的类,我只需要:

1。创建一个以myproject作为依赖项的project.clj文件:

(defproject test "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :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"]
             [com.mycompany/myproject "0.0.1-SNAPSHOT"]
            ]
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})

2。在本地maven repo上安装我的项目:

$ mvn install

3。启动repl

$ lein repl

4。从我的项目中导入任何java类

(import 'com.myproject.MyFile)

重要:这是有效的,因为我的maven项目使用maven-shade-plugin,它将所有依赖项捆绑到mvn install生成的jar中。