【问题标题】:Problems adding Clojure to Java project将 Clojure 添加到 Java 项目的问题
【发布时间】:2012-03-04 09:16:02
【问题描述】:

我正在开发一个使用 Maven 构建它的 Java 项目。我正在开发的一些功能在 Java 中会很麻烦,但在 Clojure 中很简单,所以我想在 Clojure 中实现它,并让 Java 无缝地使用生成的类。

这是我需要通过的单元测试(src/test/java/com/foo/weather/DataProcessorTest.java):

package com.foo.weather;

import static org.junit.Assert.*;
import org.junit.Test;
import java.util.*;

public class DataProcessorCljTest {
   @Test
   public void processWithTotalsAndSubTotals() throws Exception {
      assertEquals(2, DataProcessor.process(createRawData()).size());
   }

   private List<List<String>> createRawData() {
      List<List<String>> data = new ArrayList<List<String>>();
      data.add(new ArrayList<String>(Arrays.asList("2011-11-01", "Temperature", "19.2")));
      data.add(new ArrayList<String>(Arrays.asList("2011-11-01", "Pressure", "1.1")));
      return data;
   }
}

这里是 Clojure 代码 (src/main/clojure/com/foo/weather/data-processor.clj):

(ns com.foo.weather.DataProcessor
  (:gen-class
    :methods [#^{:static true} [process [java.util.List] java.util.List]]))

(defn process [raw-data]
  (java.util.ArrayList. []))

我已将以下内容添加到我的 pom.xml 以构建 Clojure 代码:

<project>
   <!-- lots of stuff omitted -->
   <build>
      <plugins>
         <!-- ... -->
         <plugin>
            <groupId>com.theoryinpractise</groupId>
            <artifactId>clojure-maven-plugin</artifactId>
            <version>1.3.6</version>
            <executions>
               <execution>
                 <id>compile</id>
                 <phase>compile</phase>
                 <goals>
                   <goal>compile</goal>
                 </goals>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>
   <repositories>
      <!-- ... -->
      <repository>
         <id>clojure-releases</id>
         <url>http://build.clojure.org/releases</url>
       </repository>
   </repositories>
   <dependencies>
      <!-- ... -->
      <dependency>
         <groupId>org.clojure</groupId>
         <artifactId>clojure</artifactId>
         <version>1.2.1</version>
       </dependency>
   </dependencies>
</project>

但是,当尝试运行我的单元测试时,编译器会报错“找不到符号变量 DataProcessor”,所以我的 Clojure 代码似乎没有被正确编译。

我注意到的一件事是我的 src/main/clojure 目录在我的 IDE (IntelliJ) 中似乎没有被视为源目录。我在项目视图中右键src/main/clojure,选择“Mark directory as > Source root”,但是在重新导入Maven依赖(即Eclipse中的“Update dependencies”)后,该目录不再被标记为source root .

这让我相信我的问题出在我的 Maven 配置中。

谁能帮忙?

【问题讨论】:

  • 使用 Maven 的 Java + Clojure 项目的另一个选择是 Zi

标签: java maven clojure


【解决方案1】:

我在我的 pom.xml 中使用了以下内容,这似乎有效:

        <plugin>
            <groupId>com.theoryinpractise</groupId>
            <artifactId>clojure-maven-plugin</artifactId>
            <version>1.3.8</version>

            <executions>
                <!-- ... -->
                <execution>
                    <id>test-clojure</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>

            <configuration>
                <sourceDirectories>
                    <sourceDirectory>src/main/clojure</sourceDirectory>
                </sourceDirectories>
                <testSourceDirectories>
                    <testSourceDirectory>src/test/clojure</testSourceDirectory>
                </testSourceDirectories>
            </configuration>
        </plugin>

另外,我发现如果将 Clojure 源文件包含在资源目录中,Clojure 可以正常工作(当然,您显然需要从 Java 代码中加载它们)。

这个问题可能对您也有帮助:Calling clojure from java

【讨论】:

  • 这似乎对我不起作用;我的 JUnit 测试仍然没有看到 Clojure 生成的类。 Calling clojure from java 实际上是我弄清楚如何做到这一点的起点。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-21
  • 1970-01-01
  • 1970-01-01
  • 2020-03-18
  • 2019-08-16
  • 2014-12-19
  • 2021-07-27
相关资源
最近更新 更多