Unable to point my test sources to compileTestJava

I am converting an ant project to gradle when the main source is built by ant (through gradle) while the test sources are built directly by gradle.

The main sources are under $[projectRoot}/src. Test sources are under $[projectRoot}/test

Even after explicitly setting sourceSets, compileTesstsJava fails to see the test sources and finishes trivially, indicating that source set is empty:

sourceSets {
    main {
        java {
            srcDir = 'src'
        }
    }
    test {
        java {
            srcDir = 'test'
        }
    }
}

Here is the compileTestJava run:

$ ./gradlew test --debug
//...
^[[1m> Building 42% > :compileTestJava^[[22m^[[33D^[[0K^[[1A^[[93CStarting to execute task ':compileTestJava'
^[[1m> Building 42% > :compileTestJava^[[22m^[[33D^[[0K^[[1A^[[14CINFO
^[[1m> Building 42% > :compileTestJava^[[22m^[[33D^[[0K^[[1A^[[18C]
 ^[[1m> Building 42% > :compileTestJava^[[22m^[[33D^[[0K^[[1A^[[21Corg.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter
^[[1m> Building 42% > :compileTestJava^[[22m^[[33D^[[0K^[[1A^[[93C]
 ^[[1m> Building 42% > :compileTestJava^[[22m^[[33D^[[0K^[[1A^[[95CSkipping task ':compileTestJava' as it has no source files.
^[[1m> Building 42% > :compileTestJava^[[22m^[[33D^[[0K^[[1A^[[154C
^[[1m> Building 42% > :compileTestJava^[[22m^[[33D^[[0K14:34:21.427
^[[1m> Building 42% > :compileTestJava^[[22m^[[33D^[[0K^[[1A^[[12C [
^[[1m> Building 42% > :compileTestJava^[[22m^[[33D^[[0K^[[1A^[[14CDEBUG
^[[1m> Building 42% > :compileTestJava^[[22m^[[33D^[[0K^[[1A^[[19C] [
^[[1m> Building 42% > :compileTestJava^[[22m^[[33D^[[0K^[[1A^[[22Corg.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter
^[[1m> Building 42% > :compileTestJava^[[22m^[[33D^[[0K^[[1A^[[91C]
 ^[[1m> Building 42% > :compileTestJava^[[22m^[[33D^[[0K^[[1A^[[93CFinished executing task ':compileTestJava'
//...
$

Here is my full build.gradle:

defaultTasks ‘test’ buildDir = ‘dist’ apply plugin: ‘java’ apply plugin: ‘eclipse’

ext {

antTarget = “feature” }

sourceSets {

main {

java {

srcDir = ‘src’

}

}

test {

java {

srcDir = ‘test’

}

} }

repositories {

mavenLocal()

mavenCentral()

maven { url “https://repo.spring.io/libs-release” } }

dependencies {

// Add at least version 4.8 of JUnit as dependency.

testCompile ‘junit:junit:[4.8,)’ }

task wrapper(type: Wrapper) {

println '[Running tasks for project ’ + project.getDescription() + ‘]’

gradleVersion = ‘1.11’ }

task antBuild (group: ‘legacyBuild’, description : ‘Invoke Ant build’) {

println ‘[Invoking Ant build]’

ant.importBuild ‘build/build.xml’ }

clean {

project.file(‘dist’).deleteDir() }

Any suggestions greatly appreciated!

I have checked in the gradle script and the full build log into

https://github.com/rk-git/anttogradle

/Kobe

Probably not important, but you might remove the “=”. The examples I’ve seen don’t have that (except for when setting “srcDirs” with a list).

What EXACTLY is in the “test” directory, which is in your project directory?

I modified the sourceSet definition as you suggested:

sourceSets {
    main {
        java {
            //srcDir 'src'
        }
    }
    test {
        java {
            srcDir 'test/java'
        }
    }
}
16:25:29.854 [INFO] [org.gradle.execution.taskgraph.AbstractTaskPlanExecutor] :compileTestJava (Thread[main,5,main]) started.
16:25:29.855 [LIFECYCLE] [class org.gradle.TaskExecutionLogger] :compileTestJava
16:25:29.857 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Starting to execute task ':compileTestJava'
16:25:29.877 [INFO] [org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter] Skipping task ':compileTestJava' as it has no source files.

${project.dir}/test contains the test code:

(test
         (java
               (com (X
(...) ) )
         )
    )

Pl note that I have commented out the main.java.srcDir , since its being built using my antBuild and I do not want Gradle java plugin to start building the main sources. I want gradle to build only the test sources.

Regards,

/Kobe

Ok, so to be exactly clear, in the root of your project directory, you have a “test” folder? Inside the “test” folder there is a “java” folder. Inside the “java” folder is a folder named “com”, and so on, and somewhere in that tree is at least one “.java” source file? From your literal example, I would conclude that you have “$projectdir/test/test/java/com/…”, but that’s not what your build script is expecting. Which is it?

Thanks for your help!

My gradle script says:

sourceSets {
    main {
        java {
            //srcDir 'src'
        }
    }
    test {
        java {
            srcDir 'test/java'
        }
    }
}

Does this mean it will look for my test sources under

${project.dir}/test/test/java/

?

Just to get past this problem, nested the test source in another test directory as in:

${project.dir}/test/test/java/

comoileJavaTest still fails to find the sources.

Finally, I modified my sourceSet to:

sourceSets {
    main {
        java {
            //srcDir 'src'
        }
    }
    test {
        java {
            srcDir 'java'
        }
    }
}

and I kept my original test folder structure of

${project.dir}/test/java/

This fails to find the test src files as well.

Thanks,

/Kobe

What I think is confusing you is that the name of the sourceSet and the “java” block under it has no correlation to folder names. The only thing that specifies a directory/folder is the “srcDir” property. If you set “srcDir” to “java”, then Gradle will expect to find “$projectdir/java/com/…”. If you set “srcDir” to “test/java”, it will expect to find “$projectdir/test/java/com/…”.

Thank you much. That resolved it.

Regards,

/Kobe