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

问题描述 投票:18回答:4

我使用注释处理。因此我使用apt plugin。它在build/source/apt中生成新的java源代码。

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

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

编辑:使用

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

运行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

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

java android eclipse gradle annotation-processing
4个回答
18
投票

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


7
投票

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

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

在build.gradle中


4
投票

其他人回答的内容会覆盖我原来的目录,所以我找到了一个解决方法 - 如果你不想覆盖原始目录列表,你可以这样做:

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

关键是在这里使用+=。它基本上与说明如下:

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

2
投票

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

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

应该让你亲近有关更多详细信息,请查看java gradle plugin docs的源集部分(23.7)。

© www.soinside.com 2019 - 2024. All rights reserved.