Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minazou67/e42cf903f5038a860d8ff2dd15f7fbdc to your computer and use it in GitHub Desktop.
Save minazou67/e42cf903f5038a860d8ff2dd15f7fbdc to your computer and use it in GitHub Desktop.
How to share the static analysis tool configuration with Maven

How to share the static analysis tool configuration with Maven

静的解析ツールの設定ファイルなどを Maven で共有する方法です。

Environment

  • Maven 3.3+

Introduction

Eclipse などの IDE 上では相対パスなどを使用して共有が可能ですが、 その方法だと CI サーバー上でビルドした際に設定ファイルが参照できません。 その為、Maven 上での設定が必要になります。

Preparation

静的解析ツールの設定ファイルのみを保持する Maven プロジェクトを作成します。

my-java-config
 ┣ src/main/resources
 ┃ ┣ my_checks.xml
 ┃ ┣ my-spotbugs-exclude-filter.xml
 ┃ ┗ my-pmd-ruleset.xml
 ┗ pom.xml

作成したプロジェクトをビルドし、インハウス・リポジトリにデプロイします。

How to Use

利用したい Maven プロジェクト側に下記の設定を追加します。

  <properties>
    <checkstyle.version>3.0.0</checkstyle.version>
    <spotbugs.version>3.1.6</spotbugs.version>
    <pmd.version>3.10.0</pmd.version>
    <my-java-config.version>1.0.0</my-java-config.version>
    <checkstyle.config.location>my_checks.xml</checkstyle.config.location>
    <spotbugs.excludeFilterFile>my-spotbugs-exclude-filter.xml</spotbugs.excludeFilterFile>
    <pmd.rulesetFile>my-pmd-ruleset.xml</pmd.rulesetFile>
  </properties>

  <build>
    <pluginManagement>
      <plugins>
        <!-- Checkstyle -->
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-checkstyle-plugin</artifactId>
          <version>${checkstyle.version}</version>
          <dependencies>
            <dependency>
              <groupId>com.example</groupId>
              <artifactId>my-java-config</artifactId>
              <version>${my-java-config.version}</version>
            </dependency>
          </dependencies>
        </plugin>
        <!-- SpotBugs -->
        <plugin>
          <groupId>com.github.spotbugs</groupId>
          <artifactId>spotbugs-maven-plugin</artifactId>
          <version>${spotbugs.version}</version>
          <dependencies>
            <dependency>
              <groupId>com.example</groupId>
              <artifactId>my-java-config</artifactId>
              <version>${my-java-config.version}</version>
            </dependency>
          </dependencies>
        </plugin>
        <!-- PMD -->
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-pmd-plugin</artifactId>
          <version>${pmd.version}</version>
          <dependencies>
            <dependency>
              <groupId>com.example</groupId>
              <artifactId>my-java-config</artifactId>
              <version>${my-java-config.version}</version>
            </dependency>
          </dependencies>
        </plugin>
      </plugins>
    </pluginManagement>
  <build>

  <reporting>
    <plugins>
      <!-- Checkstyle -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>${checkstyle.version}</version>
      </plugin>
      <!-- SpotBugs -->
      <plugin>
        <groupId>com.github.spotbugs</groupId>
        <artifactId>spotbugs-maven-plugin</artifactId>
        <version>${spotbugs.version}</version>
      </plugin>
      <!-- PMD -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>${pmd.version}</version>
        <configuration>
          <rulesets>
            <ruleset>${pmd.rulesetFile}</ruleset>
          </rulesets>
        </configuration>
      </plugin>
    </plugins>
  </reporting>

  <pluginRepositories>
    <pluginRepository>
      <id>inhouse-repo</id>
      <name>Inhouse Repository</name>
      <url>http://192.168.XXX.XXX/nexus/content/groups/public/</url>
    </pluginRepository>
  </pluginRepositories>

Tips

Inheritance of pom file

親プロジェクトの pom.xml に上記の設定がされている場合は、子プロジェクトにも適用されます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment