2012-02-02 41 views
5

我是新來的maven和checkstyle所以請禮貌:)。如何停止使用checkstyle maven構建使用checkstyle

我設法使用maven checkstyle插件,我可以創建我的代碼報告。但是我真正想要的是,如果樣式檢查有任何錯誤,我可以停止maven的構建過程。

到目前爲止,我的pom.xml看起來象下面這樣:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.company.app</groupId> 
    <artifactId>my-app</artifactId> 
    <packaging>jar</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>my-app</name> 
    <url>http://maven.apache.org</url> 
    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>1.2.12</version> 
     <scope>compile</scope> 
    </dependency> 
    </dependencies> 
    <reporting> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-checkstyle-plugin</artifactId> 
     <version>2.8</version> 
     </plugin> 
    </plugins> 
    </reporting> 
</project> 

我怎麼能在這裏實現我的目標是什麼?我們的團隊希望有嚴格的編碼風格標準,所以我必須使用它。

回答

5

達到你想要什麼,你需要使用maven-的CheckStyle-插件在構建生命週期中除了報告週期:

<project> 
... 
<build> 
... 
<plugins> 
    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-checkstyle-plugin</artifactId> 
    <version>2.8</version> 
    <executions> 
     <execution> 
     <phase>process-sources</phase> 
     <goals> 
      <goal>check</goal> 
     </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <failsOnError>true</failsOnError> 
    </configuration> 
    </plugin> 
</plugins> 
</build> 
</project> 
+0

thnx man,有沒有辦法將結果打印到命令行,而不是寫入xml文件? – LostMohican 2012-02-02 14:17:46

+1

@LostMohican:是的,您可以將' true'添加到配置部分以啓用控制檯輸出。要獲得更多選項,你可以看看那裏:http://maven.apache.org/plugins/maven-checkstyle-plugin/check-mojo.html – 2012-02-02 15:10:37

3

您可以嘗試設置failsOnError property

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-checkstyle-plugin</artifactId> 
    <version>2.8</version> 
    <configuration> 
    <failsOnError>true</failsOnError> 
    </configuration> 
    </plugin> 
5

我意識到,一直存在,因爲一些時間這個問題被問到,但以上答案都沒有解決這個問題。

爲了構建失敗的違法行爲,我不得不從它的默認errorviolationSeverity值更改爲warningconfiguration塊,類似於:

<plugin> 
    <artifactId>maven-checkstyle-plugin</artifactId> 
    <version>2.17</version> 
    <dependencies> 
     <dependency> 
      <groupId>com.puppycrawl.tools</groupId> 
      <artifactId>checkstyle</artifactId> 
      <version>7.5.1</version> 
     </dependency> 
    </dependencies> 
    <executions> 
     <execution> 
      <id>validate</id> 
      <phase>validate</phase> 
      <configuration> 
       <configLocation>checkstyle.xml</configLocation> 
       <encoding>UTF-8</encoding> 
       <consoleOutput>true</consoleOutput> 
       <failsOnError>false</failsOnError> 
       <failOnViolation>true</failOnViolation> 
       <violationSeverity>warning</violationSeverity> 
       <linkXRef>false</linkXRef> 
      </configuration> 
      <goals> 
       <goal>check</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

而且,請注意,我們已經建立一個稍微修改過的版本(在checkstyle.xml中定義),主要基於最新的google_checks.xml。但是,爲了使其發揮作用,還必須更新com.puppycrawl.tools.checkstyle依賴項。

相關問題