Including secondary class folders besides 'build/classes/main'

(Jesús Zazueta) #1

Hi. Continuing with my Gradle learning process, I have this situation:

For a Java Project, my main build output classes folder, as configured by default with Gradle is:


Suppose now that I have a task which generates classes that I’m storing in a secondary classes folder, like so:


Two questions:

  1. How can I, specifically, tell Eclipse (via the Eclipse Plugin) that its class path should include this location, like so:
<classpathentry kind="lib" path="/home/code/my-project/build/classes/generated"/>

This instructs Eclipse to “Add a class folder” on its Java Build Path. I tried doing the same with the Gradle build file:

eclipse {
 classpath {
  file {
   withXml {
    def node = it.asNode()
    node.appendNode('classpathentry', [kind: 'lib', path: project.ext.generatedClassesDir])

Note that ‘project.ext.generatedClassesDir’ is a ‘file()’ object.

Upon initial import into Eclipse of the project, the class folder was not added to the build path.

However, if I run the “eclipseClassPath” task once the project has been imported, the folder gets added but the Gradle dependencies declared in the project are added a second time to the build path as direct project dependencies (i.e. One set defined as “Gradle Dependencies” and again as a full list of direct jar dependencies).

So that’s one thing. Now:

  1. During my main build process (‘jar’ task for this project), how can I instruct the Java plugin to also copy the classes from this secondary generated classes folder into my resulting jar file? Is that possible? I mean, I could do a simple copy but I’m hoping there’s a declarative way to do this instead.

Thanks again for your time and help!

(Jesús Zazueta) #2

Sigh… nevermind.

What I did in the end was to switch from XMLBeans to JAXB2 to generate my schema bindings.

Fortunately, JAXB2 generates source only, and so I don’t have the problem of including additional class folders inside my project. If anyone’s interested, here’s how I call XJCTask to generate Java bindings from an XML Schema.

project.ext.generatedSrcDir = file("$buildDir/generated-src")
  dependencies {
 provided 'com.sun.xml.bind:jaxb-impl:2.2.6'
 provided 'com.sun.xml.bind:jaxb-xjc:2.2.6'
  task jaxb {
   ext {
  packageName = 'my.schema.package'
  packagePath = 'my/schema/package'
  srcFile = file('src/main/resources/my/schema/package/my-schema.xsd')
  destDir = new File(project.ext.generatedSrcDir, packagePath)
    inputs.file srcFile
 outputs.dir destDir
    ant.taskdef(name: 'xjc',
   classname: '',
   classpath: configurations.provided.asPath)
     doLast {
  ant.xjc(schema: srcFile, package: packageName,
    destdir: project.ext.generatedSrcDir)
  task generateSources() {} += project.ext.generatedSrcDir
  generateSources.dependsOn jaxb
 compileJava.dependsOn generateSources
eclipseClasspath.dependsOn generateSources

That will keep Eclipse and command-line Gradle happy.