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

con*_*ile 24 java eclipse android gradle annotation-processing

我使用注释处理.因此我使用apt插件.它生成新的Java源代码build/source/apt.

这是我的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'
}
Run Code Online (Sandbox Code Playgroud)

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

编辑:使用

sourceSets {
    apt{
        java{
            srcDir 'build/source/apt'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

运行时导致以下错误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
Run Code Online (Sandbox Code Playgroud)

使用以前的Eclipse查找生成的文件的源,但生成不会.

Pra*_*ath 17

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

  • 该解决方案无法扩展。如果您有多个源目录,则需要将它们全部添加到这一行中。milosmns 的解决方案解决了这个缺点。 (4认同)

mil*_*mns 8

其他人回答的内容将覆盖我的原始目录,因此我找到了一种解决方法-如果您不想覆盖原始目录列表,则可以这样做:

sourceSets.main.java.srcDirs += myDir
sourceSets.main.kotlin.srcDirs += myDir
Run Code Online (Sandbox Code Playgroud)

关键是在+=这里使用。基本上与这样声明相同:

sourceSets {
  main {
    java.srcDirs += myDir
    kotlin.srcDirs += myDir
  }
}
Run Code Online (Sandbox Code Playgroud)


Jai*_*ide 6

尝试为输出类定义自定义源集。就像是:

sourceSets {
    apt{
        java{
            srcDir 'build/source/apt'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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


SHo*_*oko 6

对于带有jar插件的匕首2,你可以放

sourceSets.main.java.srcDirs = ['build/generated/source/apt/main','src/main/java']
Run Code Online (Sandbox Code Playgroud)

在build.gradle中

  • 解决方案无法扩展。如果您有多个源目录,则都需要在这一行中将它们全部添加到此处。milosmns的解决方案解决了这一缺点。 (2认同)

小智 6

考虑这样的代码结构

\n
src\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80main\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80gen\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80com\n\xe2\x94\x82   \xe2\x94\x82       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80java\n\xe2\x94\x82   \xe2\x94\x82           \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80generated\n\xe2\x94\x82   \xe2\x94\x82               \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80code\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80java\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80com\n\xe2\x94\x82   \xe2\x94\x82       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80java\n\xe2\x94\x82   \xe2\x94\x82           \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80test\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80resources\n\xe2\x94\x82       \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80icons\n\xe2\x94\x82       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80META-INF\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80test\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80java\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80resources\n
Run Code Online (Sandbox Code Playgroud)\n
\n

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

\n
\n
\n

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

\n
\n

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

\n
sourceSets {\n    main {\n        java {\n            srcDirs = ['src/main/java', 'src/main/gen']\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

您可以根据需要指定任意多个源。\n希望这会有所帮助并解释一下

\n

  • 我有点疯狂地试图弄清楚这一点,因为我在 gradle.build 中已经有 java { srcDirs = ["src/main/java", "src/main/ generated"]} 但 intellij 无法识别其中的来源src/main/生成的。但是,如果我像您的帖子中那样将其更改为“src/ generated”,它就会突然起作用。Java 目录仍然需要路径中的 main ,否则它不会工作 (2认同)

Nol*_*uen 5

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

sourceSets["main"].java.srcDir(file("path/to/generated/source/directory"))
Run Code Online (Sandbox Code Playgroud)

希望,它会有所帮助。