How to fail gradle build on Javadoc warnings
There is a non-standard hidden javadoc option -Xwerror
available on all supported Java releases. Thus you could simply do something like this:
if (JavaVersion.current().isJava8Compatible()) {
tasks.withType(Javadoc) {
// The '-quiet' as second argument is actually a hack,
// since the one paramater addStringOption doesn't seem to
// work, we extra add '-quiet', which is added anyway by
// gradle. See https://github.com/gradle/gradle/issues/2354
// See JDK-8200363 (https://bugs.openjdk.java.net/browse/JDK-8200363)
// for information about the -Xwerror option.
options.addStringOption('Xwerror', '-quiet')
}
}
A feature request for an official '-Werror' for javadoc is tracked as JDK-8200363. This feature is now available in JDK 15+ as -Werror
, -Xwerror
also works as an alias.
note: i've totally replaced my original answer, because i've found a better one - which is not that ugly:
import org.gradle.logging.internal.OutputEvent
import org.gradle.logging.internal.OutputEventListener
task("javadocCheck",type:Javadoc){
// regular javadoc task configuration
def outputEvents = []
def listener=new OutputEventListener(){
void onOutput(OutputEvent event){
outputEvents << event
}
};
doFirst {
getLogging().addOutputEventListener(listener)
}
doLast {
getLogging().removeOutputEventListener(listener)
outputEvents.each { e ->
if(e.toString() =~ " warning: "){
throw new GradleException("You have some javadoc warnings, please fix them!");
}
}
}
}
task.getLogging() is now deprecated and LoggingManagerInternal#addOutputEventListener() got removed.
Here is a solution that should work with Gradle >2.14.
import org.gradle.api.logging.StandardOutputListener
task("javadocCheck",type: Javadoc) {
// regular javadoc task configuration
def capturedOutput = []
def listener = { capturedOutput << it } as StandardOutputListener
doFirst {
logging.addStandardErrorListener(listener)
logging.addStandardOutputListener(listener)
}
doLast {
logging.removeStandardOutputListener(listener)
logging.removeStandardErrorListener(listener)
capturedOutput.each { e ->
if(e.toString() =~ " warning: ") {
throw new GradleException("You have some javadoc warnings, please fix them!");
}
}
}
}