17

I've created custom source set in Gradle project to keep all generated code:

sourceSets {
  generated {
    java {
      srcDir 'src/generated/java'
    }
    resources {
      srcDir 'src/generated/resources'
    }
  }
}

I want to make the result of this source set's code compilation available at compile and run time for main and test source sets.

What's the right semantic way to do it in Gradle?

UPDATE:

As suggested here: How do I add a new sourceset to Gradle? doesn't work for me, I still get java.lang.ClassNotFoundException when I launch my app (though compilation and unit tests run fine). Here is what I tried:

sourceSets {
  main {
    compileClasspath += sourceSets.generated.output
    runtimeClasspath += sourceSets.generated.output
  }

  test {
    compileClasspath += sourceSets.generated.output
    runtimeClasspath += sourceSets.generated.output
  }
}
Community
  • 1
  • 1
Anton Moiseev
  • 2,766
  • 4
  • 21
  • 30
  • there is an answer here http://stackoverflow.com/questions/11581419/how-do-i-add-a-new-sourceset-to-gradle – lakshman Jan 16 '14 at 12:34
  • That question is a little bit different and doesn't solve my problem, I still get `ClassNotFoundException` when I launch my app. Please, see updated question. – Anton Moiseev Jan 16 '14 at 12:42
  • You'd have to provide more information, e.g. how exactly you launch the app, what class is not found, etc. – Peter Niederwieser Jan 16 '14 at 12:47
  • can you add the stacktrace to the question? – lakshman Jan 16 '14 at 12:52
  • It's a Spring 4 project with Spring Boot enabled. As build result I get single self-executable JAR with all dependencies packaged in it. As I can see now, missing generated class doesn't present in the resulting JAR. Investigating... – Anton Moiseev Jan 16 '14 at 12:53
  • @PeterNiederwieser is there a way to explicitly say gradle that I want the `*.class` files from `generated` source set to be included into resulting JAR? Or I should create a custom task that produces JAR for `generated` source set (as [here](http://www.gradle.org/docs/current/userguide/java_plugin.html#N125AE)) and then add this JAR as dependency to the main `compile` configuration. – Anton Moiseev Jan 16 '14 at 13:05
  • 1
    `jar { from sourceSets.generated.output }`. – Peter Niederwieser Jan 16 '14 at 13:07

1 Answers1

19
sourceSets {
    main {
        compileClasspath += generated.output
        runtimeClasspath += generated.output
    }
}

Same for the test source set.

Peter Niederwieser
  • 111,903
  • 17
  • 295
  • 240
  • 2
    Thanks, Peter! For everyone who face similar issue: as turned out the issue was with packaging JAR, not with specifying dependencies, please see Peter's answer [here](http://stackoverflow.com/questions/21161845/gradle-custom-source-set-as-dependency-for-the-main-and-test-ones#comment31855557_21161845). – Anton Moiseev Jan 16 '14 at 13:13