使用gradle / clojuresq来构建clojure [英] Using gradle/clojuresq to build clojure

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

问题描述

我想使用gradle / Clojuresque来构建clojure代码,运行它,并得到uberjar。
我使用 http://dev.clojure.org的提示/ display / doc / Getting + Started + with + Gradle https:// bitbucket.org/kotarak/clojuresque/wiki/Getting%20Started '找不到us.bpsm:edn-java:0.4.3'错误与Gradle的Clojure(Clojuresque)

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

这是等级脚本。

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

使用 gradle build 任务,没有错误,我有一个jar文件,但我没有看到任何类文件生成;我不认为生成的jar包含任何东西,特别是当我比较手动构建的结果(编译clojure源到类(AOT)从命令行(不使用lein))。

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

这是core.clj

This is the core.clj

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

此外,如何运行代码和获得uberjar像 lein run lein uberjar 有吗?

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

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

推荐答案

创建类文件



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

可以在gradle文件中指定源文件。

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 将生成类文件。

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

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

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

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

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

这是运行代码的命令:

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



uberjar



需要三个修改: href =https://github.com/DevonStrawn/Clojuresque-Boilerplate/blob/master/UberJar/build.gradle =nofollow> https://github.com/DevonStrawn/Clojuresque-Boilerplate/blob/master /UberJar/build.gradle 。

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

apply plugin: 'application'

uberjar.enabled = true



执行uberjar



现在只有一个jar执行

Execute the uberjar

Now just one jar for the execution

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



新的build.gradle文件



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



阴影jar



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

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

或者使用这两行代码而不是清单更改代码: / p>

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

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



执行影子jar



获取ubjer jar

Execute the shadow jar

Get the ubjer jar

gradle shadow

作为uberjar。

It's the same as uberjar.

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



参考



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆