# Eclipse classpath error when java test code lives with main code

**URL:** <https://discuss.gradle.org/t/eclipse-classpath-error-when-java-test-code-lives-with-main-code/6547>\
**Category:** Old Forum Archive\
**Created:** [March 6, 2013, 5:10pm UTC](https://discuss.gradle.org/t/eclipse-classpath-error-when-java-test-code-lives-with-main-code/6547 "2013-03-06T17:10:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Bradley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mike_bradley/32/734_2.png) [@Mike\_Bradley](https://discuss.gradle.org/u/Mike_Bradley)\
**Post date:** [March 6, 2013, 5:10pm UTC](https://discuss.gradle.org/t/eclipse-classpath-error-when-java-test-code-lives-with-main-code/6547/1 "2013-03-06T17:10:00Z")

</div>

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:

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

```

build.gradle:

```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

---

<div class="post-metadata">

**Author:** ![Mike\_Bradley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mike_bradley/32/734_2.png) [@Mike\_Bradley](https://discuss.gradle.org/u/Mike_Bradley)\
**Post date:** [March 6, 2013, 6:19pm UTC](https://discuss.gradle.org/t/eclipse-classpath-error-when-java-test-code-lives-with-main-code/6547/2 "2013-03-06T18:19:00Z")

</div>

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

```gradle
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 )
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Mike\_Bradley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mike_bradley/32/734_2.png) [@Mike\_Bradley](https://discuss.gradle.org/u/Mike_Bradley)\
**Post date:** [March 8, 2013, 8:05pm UTC](https://discuss.gradle.org/t/eclipse-classpath-error-when-java-test-code-lives-with-main-code/6547/3 "2013-03-08T20:05:00Z")

</div>

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](http://stackoverflow.com/questions/15298886/how-to-keep-java-code-and-junit-tests-together-building-with-gradle)

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [March 9, 2013, 10:39am UTC](https://discuss.gradle.org/t/eclipse-classpath-error-when-java-test-code-lives-with-main-code/6547/4 "2013-03-09T10:39:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mike\_Bradley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mike_bradley/32/734_2.png) [@Mike\_Bradley](https://discuss.gradle.org/u/Mike_Bradley)\
**Post date:** [March 11, 2013, 1:50pm UTC](https://discuss.gradle.org/t/eclipse-classpath-error-when-java-test-code-lives-with-main-code/6547/5 "2013-03-11T13:50:00Z")

</div>

Yes, it causes a error that reads:

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

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [March 11, 2013, 2:31pm UTC](https://discuss.gradle.org/t/eclipse-classpath-error-when-java-test-code-lives-with-main-code/6547/6 "2013-03-11T14:31:00Z")

</div>

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.
