Eclipse classpath error when java test code lives with main code


(Mike Bradley) #1

I’m using the gradle file below to try and work with a Java directory structure in which the test classes are in the same packages/directories as the code they are testing. However, I need to split the test class files into a different directory when building. This .gradle seems to work if I’m building from the command line. However creating an Eclipse project from it will generate duplicate entries in the classpath. What am I doing wrong?

classpath problem:

<classpathentry excluding="**/*Test.java" kind="src" path="src"/>
<classpathentry including="**/*Test.java" kind="src" path="src"/>

build.gradle:

apply plugin: 'java'
apply plugin: 'eclipse'
  sourceCompatibility = 1.6
  repositories {
    mavenCentral()
}
  dependencies {
    compile group: 'log4j', name: 'log4j', version: '1.2.17'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}
  sourceSets {
    main {
        java {
            srcDir 'src'
            exclude '**/*Test.java'
        }
        output.classesDir = file("$buildDir/classes")
    }
    test {
        java {
            srcDir 'src'
            include '**/*Test.java'
        }
        output.classesDir = file("$buildDir/test-classes")
    }
}

Gradle Version: 1.4 Gradle IDE version (for eclipse): 3.2.0.201301240803-M2


(Mike Bradley) #2

In case this helps anyone else: I know neither groovy nor gradle but I worked around my problem with the following hack.

eclipse {
    classpath {
        file {
            //closure executed after .classpath content is loaded from existing file
            //and after gradle build information is merged
            whenMerged { classpath ->
                classpath.entries.removeAll { entry -> entry.kind == 'src' && entry.dir == file("$projectDir/src")}
                def srcEntry = new org.gradle.plugins.ide.eclipse.model.SourceFolder('src', null)
                srcEntry.dir = file("$projectDir/src")
                classpath.entries.add( srcEntry )
            }
        }
    }
}

(Mike Bradley) #3

I asked this same question on stackoverflow. Link: http://stackoverflow.com/questions/15298886/how-to-keep-java-code-and-junit-tests-together-building-with-gradle


(Peter Niederwieser) #4

Does the “duplicate” class path entry cause problems? Logically, it looks correct to me.


(Mike Bradley) #5

Yes, it causes a error that reads:

“Build path contains duplicate entry: ‘src’ for the project ‘’”


(Peter Niederwieser) #6

I can’t think of a good way to solve this problem automatically. Ideally, source dirs could be overridden directly in ‘eclipse.classpath’. But given that the latter only accepts source sets, I think that your solution is the best one.