2015-04-08 59 views
5

我正在使用Java 7(儘管使用1.6編譯)來編譯類和javadocs。我已經消除了所有出現的javadoc警告,但是如果有任何javadoc警告,構思會失敗。如何在Javadoc警告上失敗gradle構建

使用Java 8,這是默認行爲。但是,當涉及到警告時,它也更加嚴格(如果方法沒有列出所有的@params或@returns,我們不需要警告)。另外,我不認爲公司很快就會到8,所以這是一個有爭議的問題。

我希望有一些簡單的標誌設置如果有警告(只有failonError)有gradle失敗。我在想的是抓取javadoc進程的控制檯輸出。如果該輸出包含警告,那麼我知道有警告,並且構建應該失敗。

這裏是我的javadoc塊在我的build.gradle:

task gendocs(type: Javadoc) { 
options.stylesheetFile = new File("./assets/doc_style.css") 
options.windowTitle = "OurTitle" 
options.memberLevel = JavadocMemberLevel.PROTECTED 
options.author = true 
options.linksOffline('http://d.android.com/reference', System.getenv("ANDROID_HOME") + '/docs/reference') 
String v = "${SEMVER}" 
version = v.replace("_", '.') 
destinationDir = new File("${BUNDLE_FOLDER}/docs/api") 
source = sourceSets.main.allJava 
classpath += configurations.compile 
} 

所以,如果沒有做到這一點更簡單的方法,我怎麼檢查的javadoc的控制檯輸出湊呢?

+0

也許問題是,你正在配置'gendocs'任務,而使用'時,默認情況下生成它們的任務java'插件名爲'javadoc'? – madhead

+0

沒有什麼區別。我決定實施checkstyle。 –

+0

隨時upvote一個真正的解決這個https://discuss.gradle.org/t/javadoc-fail-on-warning/18141 – tir38

回答

3

注:我已經完全取代了我原來的答覆,因爲我已經找到了一個更好的 - 這是不是難看:

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!"); 
        } 
       } 
      } 
     } 
+0

感謝這一點,它正在爲我現在需要的。 –

+0

我真的很想這樣做,但我在構建過程中從其他「警告:」字符串中得到了太多的誤報。 – tir38

2

task.getLogging()現在已經過時和LoggingManagerInternal#addOutputEventListener( )被刪除。

這是一個解決方案,應該與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!"); 
       } 
      } 
     } 
    }