On 3/08/10 6:08 PM, [email protected] wrote:
Hi,

I have a simple groovy project with sources in src/main/groovy. Lets say they are all in a package called 'foo'.

If I run compileGroovy without any additional configuration the classes end up in

build/classes/main/foo/

why is the main in there ?

Because they're the classes for the 'main' source set. The classes directory for the 'test' source set is build/classes/test, and so on.

You can change these default locations if you like. For example:

sourceSets.main.classesDir = file('build/main-classes')
sourceSets.test.classesDir = file('build/test-classes')

Probably best not to point them at the same directory.



Also - how can I run a class from a build.gradle ?

this is my build.gradle:

apply plugin:'groovy'

buildscript{
  apply {
apply from: 'http://myserver:port/artifactory/repo/de/configuration/1.0/configuration-1.0.gradle'
  }
}

dependencies {
    groovy group:'org.codehaus.groovy', name:'groovy-all', version:'1.7.4'
}

task 'run-project' << {
    new foo.AGroovy()
}

I assumed that per default if the compiled class is in build/classes it will run. But also if the compiled class is under build/classes/foo/AGroovy.class running 'gradle run-proejct' fails with 'unable to resolve....'

What do I have to configure to run from a build script ?

There's a few options.

If the class is only used at build time, you can move it to the buildSrc/src/main/groovy directory, and Gradle will automatically compile it and make it available in the build script, like you're trying to do above. Nothing more to configure there.

If it's a production class, you might use the JavaExec task, but this requires a class with a main() method:

task 'run-project'(type: JavaExec) {
    main = 'my.mainClass'
    classpath = sourceSets.main.runtimeClasspath
    args = ['some', 'args']
}

Another option is to make use of Groovy's dynamic nature and load the class up after it has been compiled:

task 'run-project' {
    dependsOn sourceSets.main.runtimeClasspath
    doLast {
def cl = new URLClassLoader(sourceSets.main.runtimeClasspath.collect {it.toURI().toURL() } as URL[])
        def myClass = cl.loadClass('my.mainClass').newInstance()
        // do some stuff with myClass
    }
}


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

Reply via email to