如何使用checkstyle停止maven构建

时间:2012-02-02 13:35:56

标签: java maven 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>

我怎样才能达到目标?我们的团队希望拥有严格的编码风格标准,因此我必须使用它。

3 个答案:

答案 0 :(得分:16)

我意识到自问这个问题以来已经有一段时间了,但上述答案都没有为我解决这个问题。

为了使构建在违规时失败,我必须将public class AsyncT extends AsyncTask<String,Void,Void> { String jsonResult; URL url; public AsyncT(String jsonResult, URL url) { this.jsonResult = jsonResult; this.url = url; } @Override protected Void doInBackground(String... params) { try { HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setRequestProperty("Content-Type", "application/json"); httpURLConnection.connect(); DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream()); wr.writeBytes(jsonResult); wr.flush(); wr.close(); } catch (IOException e) { e.printStackTrace(); } return null; } } 值从其默认violationSeverity更改为error块中的warning,类似于:

configuration

另外,请注意我们已经建立了一个略微修改的样式版本(在<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依赖项。

答案 1 :(得分:6)

要实现您的目标,除了报告生命周期之外,您还需要在构建生命周期中使用maven-checkstyle-plugin:

<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>

答案 2 :(得分: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>