Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

gradle

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.

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

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

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?

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
like image 237
Jazzepi Avatar asked Apr 12 '15 19:04

Jazzepi


People also ask

What is compileOnly in Gradle?

compileOnly. Gradle adds the dependency to the compile classpath only (that is, it is not added to the build output). This is useful when you're creating an Android module and you need the dependency during compilation, but it's optional to have it present at runtime.

How do I specify Java 11 in Gradle?

Right click on the deploy or any other task and select "Open Gradle Run Configuration..." Then navigate to "Java Home" and paste your desired java path.

What is source compatibility in Gradle?

According to Gradle documentation: sourceCompatibility is "Java version compatibility to use when compiling Java source." targetCompatibility is "Java version to generate classes for."


2 Answers

Please try:

apply plugin: 'java'

compileJava {
    options.compilerArgs << '-parameters' 
}
like image 75
Opal Avatar answered Oct 15 '22 18:10

Opal


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

like image 33
Jared Burrows Avatar answered Oct 15 '22 20:10

Jared Burrows