2012-02-02 46 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依赖项。

相关问题