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

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

问题描述

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

解决方案

我有一个完全正式的生产设置与Eclipse,Maven和Clojure,现在工作非常好。希望它是一个很好的例子,在Java IDE中的一个很好的多边形设置。



我不使用leiningen - 没有任何反对lein - 这是非常好的和理想的在一个纯粹的Clojure / CLI世界。但是,我发现纯Maven在使用IDE的多沟槽Java + Clojure环境中更好,因为工具集成更好。从生态系统/观众/社区角度来看,如果您希望Java世界的人们能够构建您的源代码,那么如果您直接使用Maven,您将会造成更多的困惑。



这是我的设置:




  • Eclipse 4.2作为主IDE

  • 逆时针 Eclipse插件 - 非常好,照顾REPL,Clojure编辑等。

  • Maven用于管理所有项目(我主要使用内置的Eclipse Maven集成,但偶尔也使用CLI版本)

  • cljunit 用于启用JUnit测试在项目的Clojure部分运行

  • Github / Travis CI用于SCM和持续集成,使用内置的EGit团队提供程序在Eclipse中访问



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




  • 我用Maven配置所有内容,使用标准的Maven目录布局。 Polyglot Java + Clojure项目通常同时具有 src / main / java src / main / clojure

  • Clojure只是一个Maven依赖,就像任何其他Java库一样。

  • 我将Clojure源目录放入Maven设置中的资源目录。这意味着 .clj 文件在任何jar包中捆绑,可以在运行时加载/运行。

  • 我通常会一般来说,Java侧的入口点是一个 public static void main(...),但是很快就调用了Clojure代码。请参阅从Java调用Clojure的博客文章



最后一些编码提示为多边形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天全站免登陆