本文介绍了使用 gradle/clojuresq 构建 clojure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 gradle/Clojuresque 来构建 clojure 代码,运行它,然后获取 uberjar.我使用来自 http://dev.clojure.org/display/的提示doc/Getting+Started+with+Gradlehttps://bitbucket.org/kotarak/clojuresque/wiki/Getting%20Started'Could not find us.bpsm:edn-java:0.4.3' Clojure 的 Gradle (Clojuresque) 错误.

这是成绩单.

buildscript {存储库{maven { url "http://clojars.org/repo" }MavenCentral()}依赖{类路径clojuresque:clojuresque:1.7.0"}}应用插件:'clojure'clojure.aotCompile = true存储库{flatDir 目录:project.file("lib/runtime")maven { url "http://clojars.org/repo" }}

使用gradle build任务,没有错误,我有一个jar文件,但我没有看到生成任何类文件;我认为生成的 jar 不包含任何内容,尤其是当我比较手动构建的结果时 (从命令行将 clojure 源代码编译成类(AOT)(不使用 lein)).

.├──构建│ ├── libs│ │ └── clojure.jar│ └── tmp│ └── 罐子│ └── MANIFEST.MF├── build.gradle└── src└── 你好└── core.clj

这是core.clj

(ns hello.core(:gen-class))(定义 -main这应该很简单."[](println "Hello, World!"))

可能有什么问题?另外,如何像 lein runlein uberjar 那样运行代码并获得 uberjar?

我压缩了 https://dl.dropboxusercontent 中的目录.com/u/10773282/share/2015/clojure_test.zip

解决方案

创建类文件

源代码应该位于 ./src/main/clojure 因为它是默认目录.

不过可以在 gradle 文件中指定源文件.

sourceSets {主要的 {clojure {srcDirs = ['src']}}}

另一个问题是缺少依赖项:

存储库 {maven { url "http://clojars.org/repo" }MavenCentral()}依赖{编译org.clojure:clojure:1.6.0"}

gradle build 将生成类文件.

获取jar文件

我们需要为jar文件设置主类.

jar{manifest.attributes("Main-Class": "hello.core")}

我不确定设置是否非常必要;gradle jar 将生成 jar 文件.

执行jar文件

这是运行代码的命令:

java -cp .:/clojure-1.6.0.jar:build/libs/clojure_test.jar hello.core

uberjar

需要三处修改:来自https://的提示/github.com/DevonStrawn/Clojuresque-Boilerplate/blob/master/UberJar/build.gradle.

uberjar{manifest.attributes("Main-Class": "hello.core")}应用插件:'应用程序'uberjar.enabled = true

执行uberjar

现在只需要一个 jar 来执行

clojure_test>java -jar build/libs/clojure_test-standalone.jar你好世界!

新的 build.gradle 文件

buildscript {存储库{maven { url "http://clojars.org/repo" }MavenCentral()}依赖{类路径clojuresque:clojuresque:1.7.0"}}应用插件:'clojure'clojure.aotCompile = true源集{主要的 {clojure {srcDirs = ['src']}}}存储库{maven { url "http://clojars.org/repo" }MavenCentral()}依赖{编译org.clojure:clojure:1.7.0"}罐{manifest.attributes("Main-Class": "hello.core")}超级罐子{manifest.attributes("Main-Class": "hello.core")}应用插件:'应用程序'uberjar.enabled = true

暗影罐

根据 Opal 的回答,我添加了创建 shadowJar 的 gradle 脚本.它包含设置 Main-Class 的 MAINFEST 文件.

buildscript {存储库{maven { url "http://clojars.org/repo" }MavenCentral()jcenter()}依赖{类路径clojuresque:clojuresque:1.7.0"类路径 'com.github.jengelman.gradle.plugins:shadow:1.2.0'}}应用插件:'应用程序'应用插件:'clojure'应用插件:'com.github.johnrengelman.shadow'clojure.aotCompile = truemainClassName = 'hello.core'源集{主要的 {clojure {srcDirs = ['src']}}}存储库{maven { url "http://clojars.org/repo" }MavenCentral()}依赖{编译org.clojure:clojure:1.7.0"}

或者使用这两行代码代替清单更改代码:

应用插件:'应用程序'mainClassName = 'hello.core'

执行shadow jar

获取 ubjer jar

gradle 阴影

和uberjar一样.

clojure_test>java -jar build/libs/clojure_test-all.jar你好世界!

参考资料

I'm trying to use gradle/Clojuresque to build clojure code, run it, and get uberjar.I use hints from http://dev.clojure.org/display/doc/Getting+Started+with+Gradle, https://bitbucket.org/kotarak/clojuresque/wiki/Getting%20Started, and 'Could not find us.bpsm:edn-java:0.4.3' error with Gradle for Clojure (Clojuresque).

This is the grade script.

buildscript {
    repositories {
        maven { url "http://clojars.org/repo" }
        mavenCentral()
    }
    dependencies {
        classpath "clojuresque:clojuresque:1.7.0"
    }
}

apply plugin: 'clojure'

clojure.aotCompile = true

repositories {
    flatDir dirs: project.file("lib/runtime")
    maven { url "http://clojars.org/repo" }
}

With gradle build task, there is no error and I have a jar file, but I don't see any class file generated; I don't think the generated jar contains nothing, especially when I compared the results from manual build (Compile clojure source into class (AOT) from command line (not using lein)).

.
├── build
│   ├── libs
│   │   └── clojure.jar
│   └── tmp
│       └── jar
│           └── MANIFEST.MF
├── build.gradle
└── src
    └── hello
        └── core.clj

This is the core.clj

(ns hello.core
  (:gen-class))
(defn -main
  "This should be pretty simple."
  []
  (println "Hello, World!"))

What might be wrong? Also, how to run the code and get uberjar like lein run and lein uberjar does?

I zipped the directory in https://dl.dropboxusercontent.com/u/10773282/share/2015/clojure_test.zip

解决方案

Create the class files

The source code should be located in ./src/main/clojure as it is the default directory.

One can specify source file in gradle file, though.

sourceSets {
    main {
        clojure {
            srcDirs = ['src']
        }
    }
}

The other issue was with missing dependencies:

repositories {
    maven { url "http://clojars.org/repo" }
    mavenCentral()
}

dependencies {
    compile "org.clojure:clojure:1.6.0"
}

gradle build will generate the class files.

Get the jar file

We need to set the main class for the jar file.

jar
{
    manifest.attributes("Main-Class": "hello.core")
}

I'm not exactly sure if the setup is quite necessary; gradle jar will generate the jar file.

execute the jar file

This is the command to run the code:

java -cp .:<PATH>/clojure-1.6.0.jar:build/libs/clojure_test.jar hello.core

uberjar

There are three modifications needed: hints from https://github.com/DevonStrawn/Clojuresque-Boilerplate/blob/master/UberJar/build.gradle.

uberjar
{
    manifest.attributes("Main-Class": "hello.core")
}

apply plugin: 'application'

uberjar.enabled = true

Execute the uberjar

Now just one jar for the execution

clojure_test> java -jar build/libs/clojure_test-standalone.jar
Hello, World!

The new build.gradle file

buildscript {
    repositories {
        maven { url "http://clojars.org/repo" }
        mavenCentral()
    }
    dependencies {
        classpath "clojuresque:clojuresque:1.7.0"
    }
}

apply plugin: 'clojure'

clojure.aotCompile = true

sourceSets {
    main {
        clojure {
            srcDirs = ['src']
        }
    }
}

repositories {
    maven { url "http://clojars.org/repo" }
    mavenCentral()
}

dependencies {
    compile "org.clojure:clojure:1.7.0"
}

jar
{
    manifest.attributes("Main-Class": "hello.core")
}

uberjar
{
    manifest.attributes("Main-Class": "hello.core")
}

apply plugin: 'application'

uberjar.enabled = true

Shadow jar

From Opal's answer, I add the gradle script that create shadowJar. It contains the MAINFEST file that sets up the Main-Class.

buildscript {
    repositories {
        maven { url "http://clojars.org/repo" }
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath "clojuresque:clojuresque:1.7.0"
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.0'
    }
}

apply plugin: 'application'
apply plugin: 'clojure'
apply plugin: 'com.github.johnrengelman.shadow'

clojure.aotCompile = true
mainClassName = 'hello.core'

sourceSets {
    main {
        clojure {
            srcDirs = ['src']
        }
    }
}

repositories {
    maven { url "http://clojars.org/repo" }
    mavenCentral()
}

dependencies {
    compile "org.clojure:clojure:1.7.0"
}

Or use these two lines of code instead of the manifest change code:

apply plugin: 'application'
mainClassName = 'hello.core'

Execute the shadow jar

Get the ubjer jar

gradle shadow

It's the same as uberjar.

clojure_test> java -jar build/libs/clojure_test-all.jar
Hello, World!

References

这篇关于使用 gradle/clojuresq 构建 clojure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 07:24