On 10/12/2010, at 5:19 AM, Robert Fischer wrote:

> I have a jar that I use to mock some code for compiling, but then do
> not want that mock jar on the classpath for runtime: the functionality
> is provided by a javaagent.  What's the recommended way to handle
> this?  I'm not sure how to redefine the "runtime" configuration to
> exclude the jar, so I tried to implement it by mangling the classpath
> of compile tasks:
> 
>  configurations {
>    java7Mock
>  }
> 
>  dependencies {
>    java7Mock jsr292Mock
>  }
> 
>  tasks.withType(AbstractCompile).allTasks { Task t ->
>    t.classpath = t.classpath + t.project.configurations.java7Mock
>  }


This approach is a bit too dependent on order, and so you're running into 
problems with things being null or empty.

I think the best option is to mess with the compile and runtime classpaths of 
the appropriate source sets. Here's an example:

configurations {
    extraStuff
    compileWithExtraStuff { extendsFrom compile, extraStuff }
}

dependencies {
    extraStuff '....'
}

sourceSets.main {
    compileClasspath = configurations.compileWithExtraStuff
}

This adds the dependency to the compile classpath only, ie it won't be added to 
runtime, testCompile, testRuntime, etc

You could add something like this to include the dependency in the testCompile 
and testRuntime classpaths:

configurations {
    testCompile { extendsFrom extraStuff }
}


--
Adam Murdoch
Gradle Developer
http://www.gradle.org
CTO, Gradle Inc. - Gradle Training, Support, Consulting
http://www.gradle.biz

Reply via email to