我试图在Grails 3项目的Intellij下将一些Clojure代码与Java和Groovy集成。我的最终目标是使用一些与Groovy代码混合的Clojure代码。
我在Groovy中编写了一个类,我试图在Clojure中实例化它。基本上我做的是:
1)我写了一个类app / server / src / main / groovy / mypackage / State.groovy
package mypackage
class State {
def Calendar date;
def static State stateBuilder() {
State state = new State()
state.date = Calendar.getInstance()
return state;
}
2)然后我写了一个名为app / server / src / main / clj / state.clj的文件,如下所示:
(ns mypackage.state)
(import mypackage.State)
(def groovystate (new State))
(println (bean groovystate))
(defn -main
[& args]
(println "Hello, World!"))
3)最后我写了以下app / server / project.clj
(defproject brkmopt "0.1.0-SNAPSHOT"
:description "Test Clojure and Grails integration"
:url ""
:plugins [[lein-localrepo "0.5.4"]]
:license {:name ""
:url ""}
:repositories [["bintray-grails-plugins" "https://dl.bintray.com/grails/plugins"]
["java.net" "https://download.java.net/maven/2"]
]
:dependencies [[org.clojure/clojure "1.9.0"]
[org.codehaus.groovy/groovy-all "3.0.0-alpha-4" :extension "pom"]
[org.grails/grails-datastore-gorm "6.1.9.RELEASE"]
]
:main mypackage.state
:source-paths ["src/main/clj"]
:java-source-paths ["src/main/java"]
:test-paths ["src/test/clj"]
:resource-paths ["resources"]
:target-path "out/production"
:aot :all)
请注意,我配置了:target-path为“out / production”并配置:依赖于下载某些依赖项lein无法在Groovy项目中找到。
有效。运行“lein run”我得到了:
{:date nil}
Hello, World!
正如所料。
但如果我更换:
(def groovystate (new State))
通过
(def groovystate (State/stateBuilder))
编译器开始要求更多的依赖关系,我花了几个小时在一个依赖地狱,最终导致我死路一条,要求在任何maven存储库中找不到依赖关系。
project.clj的最新版本是:
(defproject brkmopt "0.1.0-SNAPSHOT"
:description "Test Clojure and Grails integration"
:url ""
:plugins [[lein-localrepo "0.5.4"]]
:license {:name ""
:url ""}
:repositories [["bintray-grails-plugins" "https://dl.bintray.com/grails/plugins"]
["java.net" "https://download.java.net/maven/2"]
;["central" "https://central.maven.org/maven2/"]
;["sonatype" "https://oss.sonatype.org/content/repositories/releases"]
;["snapshots" "https://blueant.com/archiva/snapshots"]
]
:dependencies [[org.clojure/clojure "1.9.0"]
[org.clojure/data.priority-map "0.0.10"]
[com.google.code.gson/gson "2.7"]
[org.codehaus.groovy/groovy-all "3.0.0-alpha-4" :extension "pom"]
[org.grails/grails-datastore-gorm "6.1.9.RELEASE"]
[org.grails.plugins/events "3.3.2"]
[org.grails/grails-core "3.3.3"]
[org.grails/grails-plugin-domain-class "3.3.3"]
[org.grails/grails-web-databinding "3.3.3"]
[org.grails/grails-gsp "3.3.1"]
]
:main mypackage.state
:source-paths ["src/main/clj"]
:java-source-paths ["src/main/java"]
:test-paths ["src/test/clj"]
:resource-paths ["resources"]
:target-path "out/production"
:aot :all)
我找不到grails-gsp版本3.3.1。无论如何,我确信这不是要走的路。它应该使用Grails使用的相同库,而不是再次下载它们。
所有必要的依赖项都在.dr / .gradle /文件夹中,但我不知道如何让它们对Clojure可见。
请指教!
----------编辑1 ----------
正如Alex所说,我收集了更多信息。我将Clojure代码嵌入到Grail 3项目中,如下图所示:
当它工作时(第一种情况),我看到这个:
root@linuxkit-025000000001:~/workspace/brkm_app/server# lein run
Compiling com.nitryx.brkmopt.state
{:date nil}
Hello, World!
root@linuxkit-025000000001:~/workspace/brkm_app/server#
当我改为(println(bean(groovystate))并包含所有依赖项时,我看到了:
root@linuxkit-025000000001:~/workspace/brkm_app/server# lein run
Could not find artifact org.grails:grails-gsp:jar:3.3.1 in central (https://repo1.maven.org/maven2/)
Could not find artifact org.grails:grails-gsp:jar:3.3.1 in clojars (https://repo.clojars.org/)
Could not find artifact org.grails:grails-gsp:jar:3.3.1 in bintray-grails-plugins (https://dl.bintray.com/grails/plugins)
Could not find artifact org.grails:grails-gsp:jar:3.3.1 in java.net (https://download.java.net/maven/2)
This could be due to a typo in :dependencies, file system permissions, or network issues.
If you are behind a proxy, try setting the 'http_proxy' environment variable.
root@linuxkit-025000000001:~/workspace/brkm_app/server#
Grails在Gradle上运行。 Lein on Maven。我在docker容器上运行它,那里没有.m2文件夹。
root@linuxkit-025000000001:~# ls -la
total 20
drwxr-xr-x 1 root root 4096 Feb 16 18:41 .
drwxr-xr-x 1 root root 4096 Feb 12 15:18 ..
drwx------ 2 root root 4096 Feb 16 18:41 .cache
drwxr-xr-x 3 root root 4096 Feb 16 16:23 .lein
drwxr-xr-x 1 root root 4096 Feb 12 15:18 .vscode
drwxr-xr-x 15 root root 480 Feb 13 19:42 workspace
root@linuxkit-025000000001:~# cd workspace
root@linuxkit-025000000001:~/workspace# ls -la
total 36
drwxr-xr-x 15 root root 480 Feb 13 19:42 .
drwxr-xr-x 1 root root 4096 Feb 16 18:41 ..
-rw-r--r-- 1 root root 6148 Feb 13 19:27 .DS_Store
drwxr-xr-x 9 root root 288 Feb 15 14:08 .dr
drwxr-xr-x 18 root root 576 Feb 16 22:08 .git
-rw-r--r-- 1 root root 120 Feb 13 17:42 .gitignore
drwxr-xr-x 13 root root 416 Feb 16 17:09 brkm_app
drwxr-xr-x 4 root root 128 Feb 13 16:53 doc
-rw-r--r-- 1 root root 290 Feb 13 18:56 docker-compose.yml
drwxr-xr-x 9 root root 288 Feb 13 19:27 dockerfiles
-rwxr-xr-x 1 root root 994 Feb 13 17:42 run_container_linux.sh
-rwxr-xr-x 1 root root 949 Feb 15 18:47 run_container_mac.sh
-rwxr-xr-x 1 root root 37 Feb 13 17:50 run_idea.sh
-rwxr-xr-x 1 root root 20 Feb 13 17:49 run_visual_studio_code.sh
drwxr-xr-x 8 root root 256 Feb 15 11:51 tests
root@linuxkit-025000000001:~/workspace# cd brkm_app/
root@linuxkit-025000000001:~/workspace/brkm_app# ls -la
total 40
drwxr-xr-x 13 root root 416 Feb 16 17:09 .
drwxr-xr-x 15 root root 480 Feb 13 19:42 ..
-rw-r--r-- 1 root root 21 Feb 13 16:53 .gitignore
drwxr-xr-x 4 root root 128 Feb 13 17:37 .gradle
drwxr-xr-x 12 root root 384 Feb 16 22:13 .idea
-rw-r--r-- 1 root root 692 Feb 13 17:40 brkm_app.iml
drwxr-xr-x 13 root root 416 Feb 13 16:53 client
drwxr-xr-x 3 root root 96 Feb 13 16:53 gradle
-rwxr-xr-x 1 root root 4971 Feb 13 16:53 gradlew
-rwxr-xr-x 1 root root 2404 Feb 13 16:53 gradlew.bat
-rwxr-xr-x 1 root root 12540 Feb 15 11:51 lein
drwxr-xr-x 16 root root 512 Feb 16 22:08 server
-rw-r--r-- 1 root root 26 Feb 13 16:53 settings.gradle
我发现.dr文件夹下的依赖项运行了一个find命令。我真的不明白为什么他们在那里以及Grails / Gradle如何使用它们。
我看到了build.gradle和project.clj,但你确实需要选择一个。由于您似乎正在集成到现有的Gradle项目中,我建议您安装Gradle Clojure插件:https://github.com/gradle-clojure/gradle-clojure。不要按照关于clj工具的说明进行操作,如果您之前已经这样做,只需像其他插件一样添加它。
希望能够解决您遇到的任何依赖性问题。 clojure插件将能够编译您的clojure代码作为正常Gradle构建过程的一部分。
grails-web-databinding version 3.3.3在Maven central repo中,Leiningen默认包含它。也许包含您正在看到的输出以及您希望用于grails的lib列表会很好。
Leiningen在引擎盖下使用Maven工具并在〜/ .m2 / repository中缓存库。我不知道有什么方法可以使用.dr / .gradle /文件夹中的deps。