Java 更改gradle中生成代码的输出目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37512772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change output directory of generated code in gradle
提问by Araz Abishov
Project contains annotation processor which generates java code during compilation. By default, gradle outputs generated source files into build/classes
directory. That causes some problems with discovery of newly generated source files by IntelliJ.
项目包含在编译期间生成 java 代码的注释处理器。默认情况下,gradle 将生成的源文件输出到build/classes
目录中。这会导致 IntelliJ 发现新生成的源文件时出现一些问题。
Is there any straightforward way of configuring gradle to output source files into another directory? For example $buildDir/gen/main/java
or $buildDir/build/generated/main/java
?
是否有任何直接的方法可以配置 gradle 将源文件输出到另一个目录?例如$buildDir/gen/main/java
或$buildDir/build/generated/main/java
?
采纳答案by Araz Abishov
There is an option for java compiler which allows to customize output directory for generated java sources (documentation).
java 编译器有一个选项,它允许为生成的 java 源(文档)自定义输出目录。
-s dir
Specify the directory where to place generated source files. The directory must already exist; javac will not create it. If a class is part of a package, the compiler puts the source file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -s C:\mysrc and the class is called com.mypackage.MyClass, then the source file will be placed in C:\mysrc\com\mypackage\MyClass.java.
-s 目录
指定放置生成的源文件的目录。该目录必须已经存在;javac 不会创建它。如果类是包的一部分,编译器会将源文件放在反映包名称的子目录中,并根据需要创建目录。例如,如果您指定 -s C:\mysrc 并且类名为 com.mypackage.MyClass,则源文件将放置在 C:\mysrc\com\mypackage\MyClass.java 中。
Example of build.gradle
示例 build.gradle
compileJava {
options.compilerArgs << "-s"
options.compilerArgs << "$projectDir/generated/java"
doFirst {
// make sure that directory exists
file(new File(projectDir, "/generated/java")).mkdirs()
}
}
clean.doLast {
// clean-up directory when necessary
file(new File(projectDir, "/generated")).deleteDir()
}
sourceSets {
generated {
java {
srcDir "$projectDir/generated/java"
}
}
}
This code snippet does next:
此代码片段接下来会执行以下操作:
- creates and specifies directory as output for generated code
- deletes generated sources if clean task is invoked
- adds new source set
- 创建并指定目录作为生成代码的输出
- 如果调用了清理任务,则删除生成的源
- 添加新的源集
Update
更新
Use gradle apt plugininstead.
改用 gradle apt 插件。
回答by Ivan Pronin
Simply specify value for project.buildDirproperty in your build.gradle file:
只需在 build.gradle 文件中为project.buildDir属性指定值:
project.buildDir = '/gen/main/java'
This will put all generated build files to the <project_root>/gen/main/java
folder.
这会将所有生成的构建文件放到该<project_root>/gen/main/java
文件夹中。
回答by superbeyone
By default generated Java files are under $generatedFilesBaseDir/$sourceSet/$builtinPluginName
, where $generatedFilesBaseDir
is $buildDir/generated/source/proto
by default, and is configurable. E.g.,
默认情况下,生成的 Java 文件位于 下$generatedFilesBaseDir/$sourceSet/$builtinPluginName
,默认情况下,其中$generatedFilesBaseDir
是$buildDir/generated/source/proto
可配置的。例如,
protobuf {
...
generatedFilesBaseDir = "$projectDir/src/generated"
}
The subdirectory name, which is by default $builtinPluginName
, can also be changed by setting the outputSubDir
property in the builtins or plugins block of a task configuration within generateProtoTasks
block (see previous section). E.g.,
默认情况下$builtinPluginName
,子目录名称也可以通过设置outputSubDir
块内任务配置的 builtins 或 plugins 块中的属性来更改generateProtoTasks
(请参阅上一节)。例如,
{
task ->
task.plugins {
grpc {
// Write the generated files under
// "$generatedFilesBaseDir/$sourceSet/grpcjava"
outputSubDir = 'grpcjava'
}
}
}