Add to dependencies classpath


(kshaw) #1

I am trying to evaluate gradle as a build tool. For my own education, I am trying to translate an ant script that compiles clojure source, using a flat directory for dependencies, explicitly not using an external repo. I will look into clojuresque plugin later.

What is a gradle-like way to compile java then compile clojure in the same build script. My ant script looks like:

<java classname=“clojure.lang.Compile”

classpathref=“cp”

failonerror=“true”

fork=“true”>

I’ve seen adding to a buildscript classpath like:

buildscript {

repositories { … }

dependencies {

classpath ‘a:b:0.1’ }

and I’ve seen:

sourceSets.something.runtimeClasspath += files(“src/blah”)

But, I would like to know if I can do this in a way that takes advantage of gradle doing things automatically.


(kshaw) #2

Urggh. It stripped out the ant script.

The ant script had an ant classpath element which consisted of:

build/classes src/main/clojure lib/*.jar

A javac task using srcdir src/main/java

And a java task executing clojure.lang.Compile with system property

clojure.compile.path = ‘build/classes’

and command line argument:

com.example.core


(Peter Niederwieser) #3

In Gradle, you’d use the Java plugin along with either a third-party Clojure plugin, or a Clojure compiler Ant task, or a ‘JavaExec’ task that executes the Clojure compiler very similar to your Ant script. You’d typically use the regular ‘dependencies {}’ block to declare dependencies and would pass them on to the Clojure compiler (a Clojure plugin would typically do this for you). The buildscript class path is only needed for bringing in third-party plugins or classes that are used by the build itself.


(kshaw) #4

So, how do I add the clojure source directory to the classpath for JavaExec task type?

Compiling clojure isn’t my goal here. I will look at clojuresque later, after understanding more about gradle. I am thinking of things that I do with a different build tool and using them as examples to see how I would put together general solutions to general problems, using gradle.


(Peter Niederwieser) #5

So, how do I add the clojure source directory to the classpath for JavaExec task type?

Not sure what you mean by that. You’d simply pass the source directory as a command line argument, in whatever form the Clojure compiler expects it. Something like:

task compileClojure(type: JavaExec) {
    executable "org.MainClass"
    args "foo", "bar", "baz"
}

See JavaExec in the DSL reference.


(kshaw) #6

I mean the equivelent of java -cp src/main/clojure some.Class. Clojure looks on the classpath for the clojure files to compile.


(Peter Niederwieser) #7

args “-cp”, “src/main/clojure”, “some.Class”


(kshaw) #8

Err. Okay. I can use the classpath property similar to the buildscript example.

So, is it the gradle-like way to have repository + dependencies for compile, but an explicit classpath for executing.

I’m imagining that there could be a way to fit the clojure source directory into repositories + dependencies.


(Peter Niederwieser) #9

Err. Okay. I can use the classpath property similar to the buildscript example.

Make sure not to confuse the class path to be passed to the Clojure compiler with the class path for executing the Clojure compiler.


(Peter Niederwieser) #10

I’m imagining that there could be a way to fit the clojure source directory into repositories + dependencies.

Sure. You can do something like:

dependencies {
    compile files("src/main/clojure")
}
  task compileClojure(...) {
    ...
    args "-cp", configurations.compile.asPath
}

When not using a plugin (or writing your own), some lower-level configuration like the above will always be necessary.