如何为我的 Gradle Java 插件设置 compileOptions?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29593500/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:33:57  来源:igfitidea点击:

How can I set the compileOptions for my Gradle Java plugin?

javagradle

提问by Jazzepi

I want to set the -parameters command on my gradle build so that I can use reflection to access the name of the parameters. It seems like I should be doing this with the following closure.

我想在我的 gradle 构建中设置 -parameters 命令,以便我可以使用反射来访问参数的名称。似乎我应该使用以下闭包来做到这一点。

compileJava {
    compileOptions {
        compilerArgs << '-parameters'
    }
}

But compileOptions is listed as read-only, and when I look at the source code there's no setter.

但是 compileOptions 被列为只读,当我查看源代码时,没有设置器。

https://gradle.org/docs/current/dsl/org.gradle.api.tasks.compile.JavaCompile.html#org.gradle.api.tasks.compile.JavaCompile:options

https://gradle.org/docs/current/dsl/org.gradle.api.tasks.compile.JavaCompile.html#org.gradle.api.tasks.compile.JavaCompile:options

How am I suppose to be able to tell the javac compiler what args to use in Gradle?

我怎么能告诉 javac 编译器在 Gradle 中使用什么参数?

Groovy:       2.3.6
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM:          1.8.0_40 (Oracle Corporation 25.40-b25)
OS:           Windows 7 6.1 amd64

回答by Opal

Please try:

请试试:

apply plugin: 'java'

compileJava {
    options.compilerArgs << '-parameters' 
}

回答by Jared Burrows

tasks.withType(JavaCompile) {
    configure(options) {
        options.compilerArgs << '-Xlint:deprecation' << '-Xlint:unchecked' // examples
    }
}

Source:http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html

来源:http : //www.gradle.org/docs/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html

回答by Rafal G.

You cannot overwrite all of the options (since 'options' property is read-only), but you can set them one by one. For example:

您不能覆盖所有选项(因为 'options' 属性是只读的),但您可以一一设置它们。例如:

compileJava {
    //enable compilation in a separate daemon process
    options.fork = true

    //enable incremental compilation
    options.incremental = true
}

Check out the docs: https://gradle.org/docs/current/dsl/org.gradle.api.tasks.compile.JavaCompile.htmland https://gradle.org/docs/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html

查看文档:https: //gradle.org/docs/current/dsl/org.gradle.api.tasks.compile.JavaCompile.htmlhttps://gradle.org/docs/current/dsl/org.gradle .api.tasks.compile.CompileOptions.html

回答by Ali Azaz Alam

You can work with Compile Options in App.gradlefile like this:

您可以在App.gradle文件中使用编译选项,如下所示:

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.2"
    defaultConfig {
        applicationId "com.example.aliazaz.menuapp"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    /*Add Compile options in following block*/
    compileOptions {

        //Like these 
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}