Using gradle/clojuresq to build clojure

十年热恋 提交于 2019-12-01 06:34:00
prosseek

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

You're missing maven central repository:

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" }
    mavenCentral()
}

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

And you both modify gradle source sets or move hello/core.clj to src/main/clojure where gradle by default looks for sources. With these changes jar file is generated and can be run when valid cp provided - I've no clojure installed so can't check it easily.

EDIT

Here a sample project can be found that has all the changes I introduced. It also produces uber jar with shadowJar task that has clojure dependency built-in and can enables hello.core to be run with the following command:

java -cp build/libs/29015575-all.jar hello.core 

EDIT2

Now the github example produces runnable jar.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!