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

我使用thrift并在构建目录下生成一些源java文件(接口) (build/generated-sources/thrift//)但在我的src/main/java我有我的类具有相同的包定义在生成的java文件中,我的类也实现了thrift生成的接口,所以如何在build.gradle中配置它,以便它可以在i ntelliJ和build上工作

 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' 

gradle构建应该自动运行。 要使其在Intellij上运行,请尝试将以下内容添加到build.gradle中。

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

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