clojure java 交互,Clojure和Java互操作在现实世界中

I'm thinking about start using (not playing with) Clojure. Are there any useful guides? I'm not asking about lein, javac or any other 'small' manual tools. I need to know how to have Java and Clojure sources in eclipse in the same project. How to make them calling each other without compilation errors? How to configure maven? How to set up fully productive development environment? Is it even possible at the moment? What plugins may be useful? Where to start?

解决方案

I have a fully working production setup with Eclipse, Maven and Clojure that is working very nicely at the moment. Hopefully it is helpful as an example of a good polyglot setup within a Java IDE.

I don't use leiningen - Nothing against lein at all - it's very nice and ideal in a pure Clojure/CLI world. But I've found that pure Maven is nicer to work with in a polyglot Java+Clojure environment with an IDE since the tool integration is so much better. Also from an ecosystem / audience / community perspective if you want people from the Java world to be able to build your source you are going to cause a lot less confusion if you just use Maven directly.

Here is my setup:

Eclipse 4.2 as main IDE

Counterclockwise Eclipse plugin - very good, takes care of REPL, Clojure editing etc.

Maven used to manage all projects (I use the built in Eclipse Maven integration mostly, but occasionally use the CLI version as well)

cljunit used to enable JUnit tests to run on Clojure parts of the project

Github / Travis CI used for SCM and Continuous integration, accessed using the built-in EGit team provider in Eclipse

In terms of actually how I manage / set up the project itself:

I configure everything with Maven, using standard Maven directory layout. Polyglot Java+Clojure Projects typically have both src/main/java and src/main/clojure

Clojure is just a Maven dependency, like any other Java library.

I make the Clojure source directories into resource directories in the Maven setup. This means that the .clj files get bundled in any jars and can be loaded / run dynamically at runtime.

I usually make the entry point on the Java side with a public static void main(...) as usual, but call quite quickly into the Clojure code. See this blog post on calling Clojure from Java.

Finally some coding tips for polyglot Java+Clojure

I find that Java is better for low level data structures, libraries and algorithms, while Clojure is better for integrating things together and "glue" code.

Clojure calling Java is usually easier / more elegant than the other way round. Also it makes sense since you generally want the dependencies to flow that way (higher level code calling lower level code)

If you make all your Java classes immutable, they play very nicely in a Clojure world with minimal effort.

Sometimes it is worth making one or more of your Java classes implement some of the Clojure interfaces, particularly clojure.lang.IFn for example. This way your Java objects can act as first class functions in Clojure code.

Here's an example project that mixes Java and Clojure source:

I also wrote a small library (clojure-utils) that includes some example code for calling Clojure from Java, which you may find useful.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值