45

I use annotation processing. Therefore I use the apt plugin. It generates new java sources in build/source/apt.

Here is my build.gradle:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'apt'
apply plugin: 'war'
apply plugin: 'gwt'
apply plugin: 'jetty'

sourceCompatibility = 1.7
version = '1.0'

eclipse {
    classpath {
       downloadSources=true
       downloadJavadoc=true
    }
}

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots/"
        }
    }
    dependencies {
        classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.6'      
        classpath 'com.jimdo.gradle:gradle-apt-plugin:0.5-SNAPSHOT'
    }
}

repositories {
    mavenCentral()
    maven {
        name = "sonatype"
        url = "https://oss.sonatype.org/content/repositories/snapshots/"
    }
}

dependencies {
    apt 'com.google.dagger:dagger-compiler:2.0-SNAPSHOT:jar-with-dependencies'

    compile 'com.google.guava:guava:18.0'
    compile 'com.google.guava:guava-gwt:18.0'
    compile 'javax.inject:javax.inject:1'   
    compile 'com.google.dagger:dagger:2.0-SNAPSHOT'
}

gwt {
    gwtVersion='2.7.0'
    logLevel = 'INFO'
    minHeapSize = "512M";
    maxHeapSize = "1024M";


    compiler {
        strict = true;
    }
    modules 'test.GWTT'     
}

tasks.withType(de.richsource.gradle.plugins.gwt.AbstractGwtActionTask) {
    args '-XjsInteropMode', 'JS'
}

I need this sources to be available in my project such that eclipse can find them and such that they are included while compiling the project how can I do that?

Edit: Using

sourceSets {
    apt{
        java{
            srcDir 'build/source/apt'
        }
    }
}

Leads to the following errors when running gradle build:

Compiling module test.GWTT
   Tracing compile failure path for type 'test.client.GWTT'
      [ERROR] Errors in 'file:/Users/mg/Documents/Grails/GGTS3.6.2/TestGradle2/src/main/java/test/client/GWTT.java'
         [ERROR] Line 17: No source code is available for type test.client.test2.Dagger_MyWidgetGinjector; did you forget to inherit a required module?
   Finding entry point classes
      Tracing compile failure path for type 'test.client.GWTT'
         [ERROR] Errors in 'file:/Users/mg/Documents/Grails/GGTS3.6.2/TestGradle2/src/main/java/test/client/GWTT.java'
            [ERROR] Line 17: No source code is available for type test.client.test2.Dagger_MyWidgetGinjector; did you forget to inherit a required module?
      [ERROR] Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly
:compileGwt FAILED

Using the former Eclipse finds the sources of the generated files but build does not.

Michael
  • 32,527
  • 49
  • 210
  • 370

7 Answers7

37

What others answered overwrites my original directories, so I found a workaround - if you don't want to overwrite the original directory list, you can do it like this:

sourceSets.main.java.srcDirs += myDir
sourceSets.main.kotlin.srcDirs += myDir

The key is to use += here. It's essentially the same as stating it like this:

sourceSets {
  main {
    java.srcDirs += myDir
    kotlin.srcDirs += myDir
  }
}
milosmns
  • 3,595
  • 4
  • 36
  • 48
29

sourceSets.main.java.srcDirs = ['build/generated-sources/xjc','src/main/java'] worked for me.

user247702
  • 23,641
  • 15
  • 110
  • 157
Prabhath
  • 629
  • 6
  • 13
  • 5
    The solution does not scale. If you have multiple source directories they all need to be added here in this one line. The solution from milosmns addresses this shortcoming. – Leo Mekenkamp Nov 06 '19 at 08:56
15

Just in case somebody is searching for the solution using kotlin script (.kts):

sourceSets["main"].java.srcDir(file("path/to/generated/source/directory"))

Hope, it will help.

Nolequen
  • 3,032
  • 6
  • 36
  • 55
  • 1
    If you are already in the `val set by getting {}` block then you can just do `kotlin.srcDir("$buildDir/generated/source/path/to/my/dir")` – charlag Aug 06 '20 at 20:13
  • If you are not in the right place, you can get it off the project: `project.extensions.findByType(AppExtension::class.java)!!.sourceSets["main"].java.srcDir(file("path/to/generated/source/directory"))` :-) – Blundell Jan 30 '22 at 22:17
7

For dagger 2 with jar plugin you can put

sourceSets.main.java.srcDirs = ['build/generated/source/apt/main','src/main/java']

in the build.gradle

JuanMoreno
  • 2,498
  • 1
  • 25
  • 34
  • 2
    The solution does not scale. If you have multiple source directories they all need to be added here in this one line. The solution from milosmns addresses this shortcoming. – Leo Mekenkamp Nov 06 '19 at 08:56
6

Consider code structure like this

src
├───main
│   ├───gen
│   │   └───com
│   │       └───java
│   │           └───generated
│   │               └───code
│   ├───java
│   │   └───com
│   │       └───java
│   │           └───test
│   └───resources
│       ├───icons
│       └───META-INF
└───test
    ├───java
    └───resources

src/main/gen - is folder for generated code

src/main/java - is folder for manuall code

To include both (generated and manuall java code) you have to specify both as input dirs for java compilation (in build.gradle file) - I've add it to the end of file:

sourceSets {
    main {
        java {
            srcDirs = ['src/main/java', 'src/main/gen']
        }
    }
}

You can specify as many sources as you want. Hope this helps and explains a bit

Vins
  • 1,931
  • 16
  • 14
EagleEye1984
  • 463
  • 5
  • 6
  • 1
    I was going a bit crazy trying to figure this out because I had in my gradle.build already java { srcDirs = ["src/main/java", "src/main/generated"]} but intellij would not recognize the sources in src/main/generated. However if I change it to 'src/generated' like in your post, it suddenly works. Java dir still needs the main in the path or it wont work though – Wulf Jan 29 '21 at 09:57
5

Try defining a custom source set for the output classes. Something like:

sourceSets {
    apt{
        java{
            srcDir 'build/source/apt'
        }
    }
}

should get you close. For more detail check the source sets section (23.7) of the java gradle plugin docs for more detail.

Jaimie Whiteside
  • 1,180
  • 9
  • 21
  • Please see my edit I tried your idea. Eclipse found the sources but ``gradle build``leads to the error above and does not find the source. – Michael Feb 05 '15 at 14:37
  • Notbeing familiar with your code, is the error because the tests can't find the apt output? If so then you probably need to hook that into your dependency graph. Section 23.7.3 of the docs I posted have samples showing how to do that. Good Luck. – Jaimie Whiteside Feb 05 '15 at 15:41
  • I do not use any tests. the GWT plugin was not able to find the sources generated by the APT plugin. Do you know a way to tell the GWT plugin where the sources are? – Michael Feb 05 '15 at 15:43
  • I'm not familiar with that plugin. Does it hook directly into the java plugin? if so you might need to explicitly provide those in the custom source set too. – Jaimie Whiteside Feb 05 '15 at 15:47
  • Well I don't know here is the plugin: https://github.com/steffenschaefer/gwt-gradle-plugin. If you could post what you wrote in your last comment, then I can try it. – Michael Feb 05 '15 at 15:54
  • As I say, I'm not familiar with the gwt plugin. Did you try adding the output of the apt source set as a dependency? – Jaimie Whiteside Feb 05 '15 at 16:04
  • No I did not how can I add the output as a dependency? – Michael Feb 05 '15 at 16:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70318/discussion-between-jaimie-whiteside-and-confile). – Jaimie Whiteside Feb 05 '15 at 16:06
1

When using kotlin gradle dsl:

sourceSets {
main {
    java {
        srcDir("$apiSpecOutputDir/src/main/java")
    }
}}
xilef
  • 2,199
  • 22
  • 16