Java 如何将生成的源文件夹添加到 Gradle 中的源路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28345705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:04:02  来源:igfitidea点击:

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

javaandroideclipsegradleannotation-processing

提问by confile

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

我使用注释处理。因此我使用apt 插件。它在build/source/apt.

Here is my build.gradle:

这是我的 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?

我需要在我的项目中提供这些资源,以便 eclipse 可以找到它们,并且在编译项目时将它们包含在内,我该怎么做?

Edit: Using

编辑:使用

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

Leads to the following errors when running gradle build:

运行时导致以下错误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.

使用以前的 Eclipse 可以找到生成文件的来源,但 build 没有。

回答by Jaimie Whiteside

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 docsfor more detail.

应该让你接近。有关更多详细信息,请查看java gradle 插件文档的源集部分 (23.7) 以获取更多详细信息。

回答by Prabhath

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

sourceSets.main.java.srcDirs = ['build/generated-sources/xjc','src/main/java']为我工作。

回答by SHoko

For dagger 2 with jar plugin you can put

对于带有 jar 插件的 Dagger 2,您可以放置

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

in the build.gradle

在 build.gradle 中

回答by milosmns

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

回答by Nolequen

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

以防万一有人使用 kotlin 脚本 (.kts) 搜索解决方案:

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

Hope, it will help.

希望,它会有所帮助。

回答by EagleEye1984

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

src/main/gen - 是生成代码的文件夹

src/main/java - 是手动代码的文件夹

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:

要同时包含(生成的和手动的 java 代码),您必须将两者都指定为 java 编译的输入目录(在 build.gradle 文件中)-我已将其添加到文件末尾:

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

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

您可以根据需要指定任意数量的源。希望这会有所帮助并解释一下