How can I add a generated Source Folder to my Source Path in Gradle?

≡放荡痞女 提交于 2020-07-04 07:27:23

问题


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.


回答1:


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




回答2:


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
  }
}



回答3:


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




回答4:


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.




回答5:


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.




回答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/java', 'src/gen']
        }
    }
}

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



来源:https://stackoverflow.com/questions/28345705/how-can-i-add-a-generated-source-folder-to-my-source-path-in-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!