Make Gradle NOT use the source set directory as the source Eclipse folder

I have an additional java source in my assembly called gen- as in generated sources with a lot of compiler warnings in them. The task gradle eclipseconfigures the source directory of this set as the source eclipse folder, which leads to a lot of warnings that I don't want to see. Another reason is that the generated source code should not change - I do not want anyone to edit it randomly, thinking that this is regular code.

The following steps are performed, but only when overwriting an existing configuration with gradle eclipse.

eclipse.classpath.file {
    whenMerged { classpath ->
        classpath.entries.removeAll { 
            entry -> entry.kind == 'src' && entry.path == 'src/gen/java'
        }
    }
}

However, this does not work if clearing the configuration is gradle cleanEclipse eclipsewhat happens when you first import the project into eclipse.

Reading the documentation for the EclipseClasspath object , I believe that the only way is to use it eclipse.classpath.file.withXml, but it's too dirty to edit raw xml.

Is there an even more elegant solution?

+3
source share
3 answers

I solved a similar scenario by adding the 'gen' directory to the java sourceSet core set.

Well

configurations {
 jaxb
}

dependencies {
  jaxb 'com.sun.xml.bind:jaxb-xjc:'
}

sourceSets.main.java.srcDirs 'gen'

task createGenDirs() {
  file("gen").mkdirs()
}

task jaxb << {
  dependsOn createGenDirs

  //generate src into gen directory

}

task cleanGeneratedCode(type: Delete) {
  delete file("gen")

clean.dependsOn cleanGeneratedCode

hope this helps

0
source

Here's how I did it (in my case, the source folders were merged and only the packages were different):

apply plugin: 'java'
sourceSets {
    main {
        java {
            srcDir 'src/main/java'
            exclude 'com/foo/generated/**'
        }
    }
    // This sourceset is ignored by Eclipse
    gen {
        java {
            srcDir 'src/main/java'
            include 'com/foo/generated/**'
        }
    }
}
// Include in a real build from Gradle rather than Eclipse
compileJava {
    source sourceSets.gen.java
}
0
source

Eclipse , , Eclipse.

But maybe if there was a source directory that gradle used to create a second can or something like that. If you have this material as a separate source, you can exclude it from eclipse:

sourceSets {
    main {
        java { srcDir 'src' }
    }
    gen {
        java { srcDir 'src-gen' }
    }
}
eclipse {
    classpath {
        sourceSets -= [sourceSets.gen]
    }
}

I have a slightly different situation where the project file refers to another project source, so I don’t want any code to be displayed in Eclipse:

sourceSets.main.java { srcDir '../other_project/src' }
eclipse {
    classpath {
        // remove the source set from the Eclipse classpath
        sourceSets -= [sourceSets.main]
    }
    // Don't let the src dir show up as a linked folder either
    project {
        file {
            withXml {
                def res = it.asNode().get('linkedResources')[0]
                res.link.findAll{ it.name[0].text() == 'src' }.each{     
                    res.remove(it) }
            }
        }
    }
}
0
source

All Articles