Search code examples
javagradlecompilationwarningssuppress-warnings

Is it possible to tell -Werror to ignore a certain class or a certain warning during Java compilation?


I use Gradle to build my Java project with the following settings to treat warnings as errors:

tasks.withType(JavaCompile) {
  options.compilerArgs << "-Xlint:unchecked" << "-Werror"
}

Now we would like to work on some experimental research stuff in a certain class (let's call it Experimental.java) which throws some warnings (warning: XYZ is internal proprietary API and may be removed in a future release). This leads to the problem that the whole project does not build.

Can we exclude a certain class from being checked based on -Werror (i.e., "don't check Experimental.java")? Or can we tell -Werror to ignore a certain warning (i.e., "don't treat internal proprietary API warning as error")? We don't want to turn off -Werror completely, other classes should still be treated that way.


Solution

  • Thanks to this answer I came up with a solution.

    First off, one has to add the compiler flag -XDenableSunApiLintControl and can then turn off "Sun internal warnings" using -Xlint:-sunapi.

    My adjusted Gradle build file looks like the following:

    tasks.withType(JavaCompile) {
      options.compilerArgs << "-XDenableSunApiLintControl" << "-Xlint:all" << "-Werror" << "-Xlint:-sunapi"
    }