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

ngynwnxp  于 9个月前  发布在  Android
关注(0)|答案(7)|浏览(96)

我使用注解处理。所以我用apt plugin。它在build/source/apt中生成新的java源代码。
下面是我的版本。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可以找到生成文件的源代码,但build不能。

1zmg4dgp

1zmg4dgp1#

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

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

关键是在这里使用+=。基本上和这样说是一样的:

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

4urapxun2#

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

qoefvg9y

qoefvg9y3#

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

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

希望会有帮助

smdnsysy

smdnsysy4#

对于匕首2与jar插件,你可以把

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

in the building.gradle

8gsdolmq

8gsdolmq5#

考虑这样的代码结构

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

src/main/gen -生成代码的文件夹
src/main/java -是手动代码的文件夹
要包含这两个(生成的和手动的java代码),你必须指定这两个作为java编译的输入目录(在build.gradle文件中)-我已经将其添加到文件的末尾:

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

您可以根据需要指定任意多个源。希望这有助于并解释了一点

z5btuh9x

z5btuh9x6#

尝试为输出类定义一个自定义源集。例如:

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

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

vyswwuz2

vyswwuz27#

使用Kotlingradle插件时:

sourceSets {
    main {
        java {
            srcDir("$apiSpecOutputDir/src/main/java")
        }
    }   
}

相关问题