How to disable compile warnings for generated classes?


(uyentvu) #1

Hi all,

Does anyone know how to disable warnings when compiling generated classes?

I would like to see compile warnings for my source code only. I do not wish to see compile warnings for generated classes.

Here is my code and it’s not working. I am seeing all compile warnings:

File generatedDir = new File(buildDir.toString() + ‘/generated’)

//Include generated classes in the sourceSets for compilation

sourceSets.main.java

{

srcDir generatedDir

}

Any help would be greatly appreciated.

Thanks!


(René Groeschke) #2

Hello Uyen, With the snippet above, you defined two directories for the same source set (here: main.java). Since a sourceset is compiled at once, you have to split the compilation of the generated java code and the other java code. See http://gradle.org/docs/current/userguide/java_plugin.html#N118FA for details.

regards, René


(uyentvu) #3

Hi Rene’,

Thanks for your reply.

When you said I have to split the compilation of the generated java code and other java code, you meant to do as below:

sourceSets{

main {

java {

srcDir ‘src/java’

}

java {

srcDir 'buildDir.toString() + ‘/generated’

}

} }

I read the link you specified but I am still unsure about what to do. If you can give me some code samples, that would be very helpful.

Thanks.


(Peter Niederwieser) #4

A solution based on source sets could look like this:

apply plugin: "java"
  sourceSets {
    generated {
        java.srcDirs = ["$buildDir/generated"]
    }
    main {
        java.srcDirs = ["src/java"]
        compileClasspath += sourceSets.generated.output
    }
}
  compileGeneratedJava {
    dependsOn(mySourceGeneratingTask)
    options.warnings = false
}

(uyentvu) #5

Peter,

Thanks so much for your response.

I tried your code above and got the following error: Could not find property ‘output’ on source set generated.

I am using Gradle 1.0-milestone-3.

Thanks.


(uyentvu) #6

Peter,

Also, does “compileGeneratedJava” task get called automatically? or I have to do as follows:

compileJava.dependsOn compileGeneratedJava

Thanks in advance.


(Peter Niederwieser) #7

You shouldn’t need to add a task dependency manually. Gradle should be able to figure it out based on the compile class path.


(Peter Niederwieser) #8

In 1.0-m3 it has to be ‘compileClasspath += sourceSets.generated.classes’.


(uyentvu) #9

Peter,

I would like to personally thank you for your help. It works now. This forum is great.

Regards,


(uyentvu) #10

I am running my junit tests and it could not find the path to “generated” sourceSets.

My junit tests depend on sourceSets.generated in the path. Do you know how to specify sourceSets.generated in the runtime path when I run JUnit tests?

Thanks.


(uyentvu) #11

I figured it out…

test {

runtimeClasspath += sourceSets.generated.classes

}

Thanks anyway…