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

6

我使用Thrift,它会在build目录下生成一些源Java文件(接口)(build/generated-sources/thrift/<package name>/<class>),但是在我的src/main/java中,我有与生成的Java文件相同包定义的类,我的类也实现了Thrift生成的接口。那么我该如何在我的build.gradle中配置,以便在IntelliJ和构建过程中正常工作?

plugins {
  id "org.jruyi.thrift" version "0.3.1"
}
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: "org.jruyi.thrift"

group 'com.hello'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.5

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.apache.thrift', name: 'libthrift', version:'0.9.3'
    compile 'com.datastax.cassandra:cassandra-driver-core:3.0.0'
    compile 'com.datastax.cassandra:cassandra-driver-mapping:3.0.0'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

compileThrift {
    thriftExecutable "/usr/local/hello/bin/thrift"
    sourceDir "src/main/thrift"
    createGenFolder false
}

task thrift(type: Exec) {
    commandLine '/usr/local/hello/bin/thrift'
}


compileJava {
    dependsOn 'compileThrift'

Thrift 通过 Gradle 生成这些源码吗? - acdcjunior
是的。我附上了我的Gradle文件。 - user1870400
1
怎么样:sourceSets.main.java { srcDir "$buildDir/generated-sources/thrift" } - RaGe
这是谁的“分身?!?” 真的,这两个问题在同一天被提出的几率有多大... - smeeb
1个回答

4

Gradle构建应该自动工作。 要在Intellij上使其正常工作,请尝试将以下内容添加到您的build.gradle文件中。

idea.module.sourceDirs += file("$buildDir/generated-sources/thrift")

不要忘记刷新您的Gradle项目。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接