12 Aralık 2021 Pazar

checkstyle plugin

Giriş

checkstyle Supression

Örnek - Metoda
Java kodunda checkstyle hatalarını dikkate almamak için metodun başına şöyle yaparız
@SuppressWarnings({"checkstyle:methodcount"})
Örnek - Sınıfa
Şöyle yaparız
@SuppressWarnings({
        "checkstyle:CyclomaticComplexity",
        "checkstyle:MethodLength",
        "checkstyle:NPathComplexity",
        "checkstyle:TrailingComment"
})
public final class SqlConsole {..}

linkXRef Tag
Örnek
Açıklaması şöyle
the linkXRef is here to disable links to source code in report which requires more steps to configure. To keep this tutorial as simple as possible I decided to disable it.
Şöyle yaparız
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>3.1.2</version>
  <configuration>
    <linkXRef>false</linkXRef> 
  </configuration>
</plugin>
configLocation Tag
Örnek
Açıklaması şöyle
Checkstyle is a static code analysis tool used in software development for checking if Java source code is compliant with specified coding rules. To apply checkstyle to our code, create checkstyle.xml at project root, then add the following plugin in our root pom. In the example below, the config file is checkstyle.xml. The goal check mentioned in the execution section asks the plugin to run in the verify phase of the build and forces a build failure when a violation of coding standards occurs. If we run the mvn clean install command, it will scan the files for violations and the build will fail if any violations are found.
Şöyle yaparız. validate aşamasında check goal çalıştırılır. checkstyle.xml dosyası konfigürasyon olarak belirtilir.
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>3.1.2</version>
  <configuration>
    <configLocation>checkstyle.xml</configLocation>
    <encoding>UTF-8</encoding>
    <consoleOutput>true</consoleOutput>
    <failsOnError>true</failsOnError>
    <linkXRef>false</linkXRef>
  </configuration>
  <executions>
    <execution>
      <id>validate</id>
      <phase>validate</phase>
      <goals>
        <goal>check</goal>
      </goals>
    </execution>
  </executions>
</plugin>
Örnek
Şöyle yaparız
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>3.1.2</version>
  <configuration>
    <configLocation>checkstyle.xml</configLocation>
  </configuration>
</plugin>
Goals
3 tane goal var
1. check
2. checkstyle
3. checkstyle-aggregate
4. help

1. checkstyle goal
targer/site/checkstyle.html isimli bir rapor oluşturur

Örnek
Şöyle yaparız
mvn checkstyle:checkstyle
...
[INFO] ----------< codes.hubertwo.maven.checkstyle:maven-checkstyle >----------
[INFO] Building maven-checkstyle 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] --- maven-checkstyle-plugin:3.1.2:checkstyle (default-cli) @ maven-checkstyle ---
[INFO] There are 8 errors reported by Checkstyle 8.29 with sun_checks.xml ruleset.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
2. check goal - Build pipeline tarafından kullanılır
Açıklaması şöyle
Sometimes it’s better to show validation errors in build log than downloading and opening the report. I prefer this way when I’m working on changes locally and want to fix code before submitting changes. It’s also handy when you want to include code style check in you continuous integration pipeline. To do so simply use checkstyle:check goal. In this goal the build will fail if any error found by default.
checkstyle.xml
Milyon tane module var

VisibilityModifier Module
Örnek
Şöyle yaparız
<module name="VisibilityModifier">
    <property name="scope" value="PUBLIC"/>
</module>
UnusedImports Module
Açıklaması şöyle
By default, checkstyle does not check for unused imports in the code. To my surprise, none of Sun or Google checkstyle definition does not contain this rule. Moreover, checkstyle does not support rule definition inheritance, so if you use default style in your project you have to copy the whole file and add extra module for unused imports. If you use custom style, the change will require only one line.
Örnek
Şöyle yaparız
<module name="UnusedImports"/>



Hiç yorum yok:

Yorum Gönder

Local Snapshot Kullanmak

Örnek Şöyle yaparız <repository> <id>snapshot-repository</id> <name>Maven2 Snapshot Repository</name> &l...