Compile clojure file independetly

62 views
Skip to first unread message

Sébastien Lambour

unread,
Oct 9, 2014, 9:05:49 AM10/9/14
to cloju...@googlegroups.com
I'm building a mixing  project with java and clojure

The project woks well with leiningen but I not able to port it with gradle

I have to respect this build order

1- compile in.clj
2- compile  java sourset
3- compile out.clj

I would like compile one file likes in java :
task compileOne (type: JavaCompile) {
    source = sourceSets.main.java.srcDirs
    include 'some/pkg/ClassTwo.java'
    classpath = sourceSets.main.compileClasspath
    destinationDir = sourceSets.main.output.classesDir
}
But it seems not working with ClojureCompile
task compileOne (type: ClosureCompile) {
    source = sourceSets.main.clojure.srcDirs
    include 'some/pkg/ClassTwo.clj'
    classpath = sourceSets.main.compileClasspath
    destinationDir = sourceSets.main.output.classesDir
}

Thank for help
Regards, 
Sebastien


Meikel Brandmeyer

unread,
Oct 9, 2014, 10:41:21 AM10/9/14
to cloju...@googlegroups.com
Hi,

there are two approaches, I see. The first one is rather clumsy.

    compileClojure {
        dependsOn project.compileJava
        excludeNamespace 'test.me-before'
    }

    task compileEarlyClojure(type: clojuresque.tasks.ClojureCompile) {
        from project.sourceSets.main.clojure
        aotCompile = true
        includeNamespace 'test.me-before'
        destinationDir = project.compileClojure.destinationDir
        classpath      = project.compileClojure.classpath
    }

    compileJava {
        dependsOn project.compileEarlyClojure
        classpath = project.files(project.compileEarlyClojure.destinationDir, classpath)
    }

This basically configures a second compile task and limits the scope of the two tasks. Finally the the dependencies between the tasks are resolved via dependsOn.

The other possibility is to introduce a second source set for the early needed parts. Put them under src/early/clojure and add to the build.gradle:

    sourceSets {
        // Note the order here!
        early {
            compileClasspath = project.sourceSets.main.compileClasspath
        }

        main {
            compileClasspath = project.files(project.sourceSets.early.output, compileClasspath)
        }
    }

    compileClojure {
        dependsOn project.compileJava
    }

    compileEarlyClojure {
        aotCompile = true
    }

    jar {
        from project.sourceSets.early.output
    }

This again configures the dependencies and adds any output of the early source set to the jar. This works for me also. I find the second solution to be cleaner and less hacky. YMMV.

Note also, that you have to turn on AOT compilation explicitly!

Hope this helps.

Meikel

Reply all
Reply to author
Forward
0 new messages