java与maven和eclipse中的clojure混合 [英] java mixed with clojure in maven and eclipse
问题描述
- src / main / java / clojuretest
|
DistanceSensor.java
AI.clj(使用DistanceSensor)
Robot.java(使用AI)
DistanceSensor.java:
package clojuretest;
public class DistanceSensor {
public int getValue(){return 5;}
}
AI.clj:
ns clojuretest.AI
(:gen-class:methods [[isObstacleAhead [] boolean]]))
(defn -isObstacleAhead [this](<(.getValue(clojuretest.DistanceSensor 。))10))
Robot.java:
package clojuretest;
public class Robot {
public boolean shouldStop(){
return new AI()。isObstacleAhead();
}
}
我甚至可以手动强制maven编译它:
mvn clean clojure:compile
产生错误 - 没有DistanceSensor类(但是由于某种原因创建了AI.class)。所以然后
mvn compile
看到AI.class并正确编译一切并测试通过。但是我可以做什么来使 mvn clean compile
pass?我的pom.xml应该怎么样?还有什么可以让eclipse停止抱怨不存在的AI.class?
我认为 :gen-class
通常是一个代码的气味,正如从新的AI()的Java代码中尝试实例化一个类。 >
这是一种可以解决循环依赖问题的替代方法:
- 定义
AI
作为Java代码中的Java接口 - 编写一个Clojure函数,使用
创建一个符合接口的实例reify
- 从Java动态调用Clojure函数(例如使用这个博文)
- 你现在有一个AI接口的实例你可以使用你喜欢的Java
优点是这种方法是一切都会平滑hly,特别是:
- Java代码库可以独立于Clojure代码编译
- Clojure代码库可以访问所有定义的Java类和接口
- 您不需要任何特殊的IDE / Maven配置。实际上,你可以看待的只是一个常规的Java应用程序,恰好包括
clojure.jar
作为依赖。
i created a sample polyglot program. i have a sensor and a robot implemented in java and AI implemented in clojure. and i can't connect maven properly
--src/main/java/clojuretest
|
DistanceSensor.java
AI.clj (uses DistanceSensor)
Robot.java (uses AI)
DistanceSensor.java:
package clojuretest;
public class DistanceSensor {
public int getValue() {return 5;}
}
AI.clj:
(ns clojuretest.AI
(:gen-class :methods [[isObstacleAhead [] boolean]]))
(defn -isObstacleAhead [this] (< (.getValue (clojuretest.DistanceSensor.)) 10))
Robot.java:
package clojuretest;
public class Robot {
public boolean shouldStop() {
return new AI().isObstacleAhead();
}
}
i can even manually force maven to compile it:
mvn clean clojure:compile
produces error - no DistanceSensor class (but for some reason creates AI.class). so then
mvn compile
sees AI.class and compiles everything correctly and tests pass. but what can i do to make mvn clean compile
pass? how should my pom.xml look like? also what can i do to make eclipse stop complaining about non existing AI.class?
I think :gen-class
is usually a code smell, as is trying to instantiate such a class from Java code with new AI()
.
Here's an alternative approach that can solve this problem of cyclic dependencies:
- Define
AI
as a Java interface in your Java code - Write a Clojure function to create an instance conforming to the interface using
reify
- Dynamically invoke the Clojure function from Java (e.g. using the technique outlined in this blog post)
- You now have an instance of the AI interface that you can use however you like in Java
The advantage is this approach is that everything will work smoothly, in particular:
- The Java code base can be compiled independently of the Clojure code
- The Clojure code base can access all the defined Java classes and interfaces
- You don't need any special IDE / Maven config. In fact, you can treat is as just a regular Java app that happens to include
clojure.jar
as a dependency.
这篇关于java与maven和eclipse中的clojure混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!