build.gradle for eclipse project with multiple source folders

gladiator picture gladiator · Dec 29, 2014 · Viewed 9.3k times · Source

I have a eclipse project with source folders like:

/src/main/java/module1
/src/main/java/module2
/src/main/java/module3

inside these src folders i have packages like:

com.example.module1.xxx
com.example.module2.yyy
com.example.module3.zzz

I have made a gradle build using java and eclipse plugin with sourcesets like:

apply plugin: 'java'
apply plugin: 'eclipse'
sourceSets {
    main {
        java {
            srcDirs = ['src/main/java/module1']
            srcDir 'src/main/resources'
            srcDir 'src/main/java/module2'
            srcDir 'src/main/java/module3'
        }
    }
}
jar{
    manifest {
        attributes 'Implementation-Title': 'Example Project',
                   'Implementation-Version': '1.0.0'    
    }
}

repositories {
    mavenCentral()
}
dependencies{
    compile group: 'com.zaxxer', name: 'HikariCP', version: '2.2.5'
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: log4j2Version
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: log4j2Version
    compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: log4j2Version
    compile group: 'org.apache.logging.log4j', name: 'log4j-jul', version: log4j2Version
    compile group: 'org.apache.logging.log4j', name: 'log4j-jcl', version: log4j2Version
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.9'
}

The problem is when i generate eclipse project using gradle eclipse it correctly generates the 3 module source folders, a resources source folder but it also generates the super source folder src/main/java having all the nested source folders with packages like:

module1.com.exaple.module1.xxx
module2.com.exaple.module2.yyy
module3.com.exaple.module3.zzz

Thus i get error in eclipse "Cannot nest '${projectdir}/src/main/java/module1' inside '${projectdir}/src/main/java'. To enable the nesting exclude 'module1/' from '${projectdir}/src/main/java'"

How do i modify my tasks/sourcesets to get the right project structure?

Answer

gladiator picture gladiator · Dec 29, 2014

Well finally figured out that the build.gradle is fine... what i was doing wrong is i was executing command gradle eclipse which was just appending entries into the existing .classpath file, To generate a fresh copy i had to execute gradle cleanEclipseClasspath eclipse which produced the correct .classpath file.