Clojure和Java互操作在现实世界中 [英] Clojure and Java interop in a real world

查看:177
本文介绍了Clojure和Java互操作在现实世界中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑开始使用(不使用)Clojure。有什么有用的指南吗?我不是问lein,javac或任何其他小手动工具。我需要知道如何在Eclipse中有Java和Clojure源代码在同一个项目中。如何让他们互相调用没有编译错误?如何配置maven?如何设置完全生产性开发环境?这是甚至可能在目前吗?什么插件可能有用?从哪里开始?

解决方案

我有一个完全工作的生产设置与Eclipse,Maven和Clojure, 。希望它有助于在Java IDE中良好的多语言设置的示例。



我不使用leiningen - 没有任何反对lein - 这是非常好的和理想的在一个纯Clojure / CLI世界。但是我发现纯Maven更适合在带有IDE的多语言Java + Clojure环境中工作,因为工具集成非常好。同样从生态系统/受众/社区的角度来看,如果你希望来自Java世界的人能够建立你的源,你将导致很少的混乱,如果你只是直接使用Maven。



这是我的设置:





实际上如何管理/设置项目本身:





最后一些多语言Java + Clojure的编码提示




  • 我发现Java对于低级数据结构,库和算法是更好的,而Clojure更适合将事物集成在一起和粘合代码。

  • Clojure调用Java通常比其他方式更容易/更优雅。这也是有意义的,因为你通常希望依赖关系流动的方式(较高级别的代码调用较低级别的代码)

  • 如果你使所有的Java类不可变,他们在Clojure

  • 有时,值得让一个或多个Java类实现一些Clojure接口,特别是 clojure.lang.IFn 。这样你的Java对象可以作为Clojure代码中的第一个类函数。



这里是一个混合Java和Clojure源代码的示例项目: / p>



我还写了一个小图书馆( clojure-utils ),其中包括一些从Java调用Clojure的示例代码,您可能会发现它们有用。


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.

这篇关于Clojure和Java互操作在现实世界中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆