I'm definitely getting closer, but I still have a strange error.
Here's an excerpt from my gradle.build file:
task generateTestDb {
srcFile = file("src/main/groovy/service/populate_stadium_data.groovy")
destFile = file("$buildDir/baseball.h2.db")
inputs.file srcFile
outputs.file destFile
doLast {
println 'building stadium db'
def config = new CompilerConfiguration(
classpath:sourceSets.main.runtimeClasspath.asPath)
new GroovyShell(new Binding(),config).evaluate(srcFile)
}
}
The "populate_stadium_data.groovy" script reads data from the Google
geocoder web service and stores data in a local H2 database. It uses a
couple of local classes I wrote (beans.Stadium, service.Geocoder) to do so.
I'm trying to arrange to only execute this task if the database doesn't
already exist.
The problem is that when I run the populateStadiumDb task, I now get:
* What went wrong:
Execution failed for task ':generateTestDb'.
Cause: java.lang.ClassNotFoundException: org.h2.Driver
Cause: org.h2.Driver
That's strange, because the gradle dependencies include
'com.h2database:h2:1.2.140' and the script runs just fine without gradle (I
run it from Eclipse, but the Eclipse project was created using the gradle
eclipse plugin). Using sourceSets.main.runtimeClasspath.asPath resolved the
issue of finding the Stadium and Geocoder classes, and if I print the
sourceSets.main.runtimeClasspath elements the H2 jar is in there, so why
isn't the driver being found?
As an aside (and probably an easier question), is there a simpler way to
refer to the populate_stadium_data groovy script than giving it an explicit
path from the src directory?
Again, thanks for the help,
Ken
On Thu, Aug 4, 2011 at 10:44 PM, Adam Murdoch
<[email protected]>wrote:
>
> On 05/08/2011, at 3:18 AM, Kenneth Kousen wrote:
>
> Hi Peter,
>
> Thanks for the help, but I still don't quite have it. Here's the idea: I
> have a web application whose data resides in a database. I have a script
> that generates that database, which uses some of the classes in my domain.
>
> What I want to do is to execute that script as part of the build process,
> but only if the database doesn't already exist (for testing I'm using H2 and
> storing the database in the build directory). I think I can do that using
> the inputs/outputs properties of a task. The only problem I'm having is
> executing the script itself.
>
> I tried adding configurations.runtime.asPath, but that included only the
> dependent jars and not my compiled classes.
>
>
> You want sourceSets.main.runtimeClasspath.asPath, instead.
>
>
> More below:
>
> On Thu, Aug 4, 2011 at 10:51 AM, Peter Niederwieser <[email protected]>wrote:
>
>>
>> Kenneth Kousen wrote:
>> >
>> > I tried using a CompilerConfiguration:
>> >
>> > def config = new
>> >
>> org.codehaus.groovy.control.CompilerConfiguration(classpath:sourceSets.main.runtimeClasspath)
>> > new GroovyShell(new Binding(), config).evaluate(script)
>> >
>> > but again the classes in the script aren't found.
>> >
>>
>> sourceSets.main.runtimeClasspath is a FileCollection, but
>> CompilerConfiguration.setClasspath() expects a String.
>>
>> Try this: new CompilerConfiguration(classpath:
>> configurations.runtime.asPath)
>>
>
> As I say, that includes the runtime jars, but not my compiled classes.
>
>>
>> Even better, introduce your own configuration (e.g. 'script') and use
>> that.
>>
>
> Um, how do I do that? Can you point me to an example?
>
>>
>>
>> Kenneth Kousen wrote:
>> >
>> > I know there's a JavaExec task, but I don't see a GroovyExec task and it
>> > seems a
>> > bit strange to artificially wrap my script in a Java program just to run
>> > this.
>> >
>>
>> Compiled Groovy scripts can be run with JavaExec as-is. Might make sense
>> to
>> have something for source scripts, although inlining and "apply from:" are
>> also options.
>>
>
> If I understand the JavaExec tasks, I need to know the compiled class name.
> What's that for a script? (I might be able to figure that one out. :)
>
> Again, thanks for your help,
>
> Ken
>
>>
>> --
>> Peter Niederwieser
>> Principal Engineer, Gradleware
>> http://gradleware.com
>> Creator, Spock Framework
>> http://spockframework.org
>> Blog: http://pniederw.wordpress.com
>> Twitter: @pniederw
>>
>>
>> --
>> View this message in context:
>> http://gradle.1045684.n5.nabble.com/Run-groovy-script-from-gradle-with-classpath-tp4664822p4666604.html
>> Sent from the gradle-user mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>> http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>
> --
> Kenneth A. Kousen
> President
> Kousen IT, Inc.
>
>
>
> --
> Adam Murdoch
> Gradle Co-founder
> http://www.gradle.org
> VP of Engineering, Gradleware Inc. - Gradle Training, Support, Consulting
> http://www.gradleware.com
>
>
--
Kenneth A. Kousen
President
Kousen IT, Inc.