Problems adding a Clojure project to Java

I am working on a Java project that uses Maven to create it. A bit of the functionality that I am developing would be rather cumbersome in Java, but would be simple in Clojure, so I would like to implement it in Clojure and use Java as a result of the generated classes without problems.

Here's the unit test I need to go through (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; } } 

And here is the Clojure code (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. [])) 

I added the following to pom.xml to create the Clojure code:

 <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> 

However, when trying to run my unit test, the compiler complains that it "cannot find the character variable DataProcessor", so it seems that my Clojure code is not compiled properly.

One thing I noticed is my src / main / clojure directory does not seem to be considered the source directory in my IDE (IntelliJ). I right-click on src / main / clojure in the project view and select "Mark directory as> Source root", but after re-importing from Maven dependencies (i.e. "Update dependencies" in Eclipse) the directory is no longer marked as the original root .

This makes me think that my problem is in my Maven configuration.

Can anyone help?

+6
source share
1 answer

I use the following in my pom.xml, which works:

  <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> 

In addition, I found that Clojure works fine if you include Clojure source files in the resource directory (of course, you will need to load them from Java code).

You may also find this question helpful: Clojure call from java

+2
source

Source: https://habr.com/ru/post/909929/


All Articles