Gradle task not able to resolve class


(Ulisses) #1

Hi all. I have a gradle (1.8) project with 2 classes A and B where B import A. Both files are under /foo/bar/ package (myProjectFolder/test/foo/bar folder).

A.groovy class

package foo.bar

import org.junit.Test;

public class ATest{ 
    @Test
    public void run() {
        System.out.println("CLASS A");
    }
}

B.groovy class

package foo.bar

import org.junit.Test;

public class BTest{
    @Test
    public void run() {
        ATest a = new ATest();
        a.run();
        System.out.println("CLASS B");
    }
}

My build.gradle file

apply plugin: "groovy"
apply plugin: "java"

repositories {
        mavenCentral()
}

dependencies {
        groovy group: "org.codehaus.groovy", name:"groovy-all", version: "1.8.6"
        testCompile "junit:junit:4.10"
}

test {
        testLogging.showStandardStreams = true
}

sourceSets {
        test { groovy {
                srcDir 'test/foo/bar'
        } }
}

buildscript {
        repositories { mavenCentral() }
}

configurations{
    addToClassLoader
}

dependencies {
    addToClassLoader "junit:junit:4.10"
}

URLClassLoader loader = GroovyObject.class.classLoader
configurations.addToClassLoader.each {File file ->

loader.addURL(file.toURL())
}

task runA << {
    new GroovyShell().run(file('test/foo/bar/ATest.groovy'));
}

task runB << {
    new GroovyShell().run(file('test/foo/bar/BTest.groovy'));
}

Output console for gradle clean -Dtest.single=A test

[root@vm1]# gradle clean -Dtest.single=A test
The groovy configuration has been deprecated and is scheduled to be removed in Gradle 2.0. Typically, usages of 'groovy' can simply be replaced with 'compile'. In some cases, it may be necessary to additionally configure the 'groovyClasspath' property of GroovyCompile and Groovydoc tasks.
:clean
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources
:classes
:compileTestJava UP-TO-DATE
:compileTestGroovy
:processTestResources UP-TO-DATE
:testClasses
:test
foo.bar.ATest > run STANDARD_OUT
    CLASS A
BUILD SUCCESSFUL
Total time: 13.205 secs

Output console for gradle clean -Dtest.single=B test

[root@vm1]# gradle clean -Dtest.single=B test
The groovy configuration has been deprecated and is scheduled to be removed in Gradle 2.0. Typically, usages of 'groovy' can simply be replaced with 'compile'. In some cases, it may be necessary to additionally configure the 'groovyClasspath' property of GroovyCompile and Groovydoc tasks.
:clean
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources
:classes
:compileTestJava UP-TO-DATE
:compileTestGroovy
:processTestResources UP-TO-DATE
:testClasses
:test
foo.bar.BTest > run STANDARD_OUT
    CLASS A
    CLASS B
BUILD SUCCESSFUL
Total time: 12.218 secs

Output console for gradle -q runA -i

[root@vm1]# gradle -q runA -i
Starting Build
Settings evaluated using empty settings script.
Projects loaded. Root project using build file '/opt/myProject/build.gradle'.
Included projects: [root project 'myProject']
Evaluating root project 'myProject' using build file '/opt/myProject/build.gradle'.
Starting file lock listener thread.
The groovy configuration has been deprecated and is scheduled to be removed in Gradle 2.0. Typically, usages of 'groovy' can simply be replaced with 'compile'. In some cases, it may be necessary to additionally configure the 'groovyClasspath' property of GroovyCompile and Groovydoc tasks.
All projects evaluated.
Selected primary task 'runA'
Tasks to be executed: [task ':runA']
:runA (Thread[main,5,main]) started.
:runA
Executing task ':runA' (up-to-date check took 0.001 secs) due to:
  Task has not declared any outputs.
CLASS A
JUnit 4 Runner, Tests: 1, Failures: 0, Time: 63
:runA (Thread[main,5,main]) completed. Took 0.739 secs.
BUILD SUCCESSFUL
Total time: 7.826 secs

Output console for gradle -q runB -i <-- THIS EXECUTION FAILS and I don’t know why.

[root@vm1]# gradle -q runB -i
Starting Build
Settings evaluated using empty settings script.
Projects loaded. Root project using build file '/opt/myProject/build.gradle'.
Included projects: [root project 'myProject']
Evaluating root project 'myProject' using build file '/opt/myProject/build.gradle'.
Starting file lock listener thread.
The groovy configuration has been deprecated and is scheduled to be removed in Gradle 2.0. Typically, usages of 'groovy' can simply be replaced with 'compile'. In some cases, it may be necessary to additionally configure the 'groovyClasspath' property of GroovyCompile and Groovydoc tasks.
All projects evaluated.
Selected primary task 'runB'
Tasks to be executed: [task ':runB']
:runB (Thread[main,5,main]) started.
:runB
Executing task ':runB' (up-to-date check took 0.001 secs) due to:
  Task has not declared any outputs.
:runB FAILED
:runB (Thread[main,5,main]) completed. Took 0.237 secs.

FAILURE: Build failed with an exception.
* Where:
Build file '/opt/myProject/build.gradle' line: 46
* What went wrong:
Execution failed for task ':runB'.
> startup failed:
  /opt/myProject/test/foo/bar/BTest.groovy: 8: unable to resolve class ATest
   @ line 8, column 9.
            ATest a = new ATest();
         ^
  /opt/myProject/test/foo/bar/BTest.groovy: 8: unable to resolve class ATest
   @ line 8, column 13.
            ATest a = new ATest();
             ^
  2 errors
* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.
BUILD FAILED
Total time: 7.872 secs

So, what am I missing to run the task runB correctly?


(Rodrigo B. de Oliveira) #2

Hi Ulisses,

I cannot help you understanding why GroovyShell does not find the other class but if what you’re trying to accomplish with runB is to execute just a subset of the tests may I suggest you to define a Test task instead?

task runB(type: Test) {
  classpath = sourceSets.test.runtimeClasspath
  testLogging.showStandardStreams = true
  include '**/B*'
}

This definition comes with the usual benefits of up-to-date checking, reporting, etc.

Cheers!
Rodrigo


(Ulisses) #3

It solves my problem, but not the GroovyShell issue.
Still don’t know why GroovyShell can’t find class A.

I will use your suggestion while I can’t fix GroovyShell.
Obrigado jovem.