Using gradle/clojuresq to build clojure

后端 未结 2 1166
醉酒成梦
醉酒成梦 2021-01-14 01:37

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

相关标签:
2条回答
  • 2021-01-14 01:43

    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.

    0 讨论(0)
  • 2021-01-14 01:52

    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

    • https://github.com/johnrengelman/shadow
    • Boiler plate - https://github.com/DevonStrawn/Clojuresque-Boilerplate
    • Building a uberjar with Gradle
    0 讨论(0)
提交回复
热议问题