<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (C) 2015-2020 HandcraftedBits

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <artifactId>handcraftedbits-parent-java</artifactId>
  <packaging>pom</packaging>
  <name>HandcraftedBits Parent POM for Java Projects</name>
  <description>A parent POM used by HandcraftedBits Java projects</description>
  <url>https://github.com/handcraftedbits/handcraftedbits-parent</url>
  <inceptionYear>2015</inceptionYear>

  <parent>
    <groupId>com.handcraftedbits</groupId>
    <artifactId>handcraftedbits-parent</artifactId>
    <version>1.4.2</version>
  </parent>

  <properties>
    <checkstyle.suppressions.location>checkstyle-handcraftedbits-suppressions.xml</checkstyle.suppressions.location>
    <coverage.branch.minimum>0.00</coverage.branch.minimum>
    <coverage.minimum>0.00</coverage.minimum>
    <failsafe.args.extra></failsafe.args.extra>
    <javadoc.excludedPackages>*.internal.*</javadoc.excludedPackages>
    <javadoc.skip>false</javadoc.skip>
    <surefire.args.extra></surefire.args.extra>
    <version.java>11</version.java>

    <version.dep.codequality>1.1.0</version.dep.codequality>
    <version.dep.junit>5.7.0</version.dep.junit>
    <version.dep.logback>1.3.0-alpha5</version.dep.logback>
    <version.dep.lombok>edge-SNAPSHOT</version.dep.lombok>
    <version.dep.mockito>3.5.10</version.dep.mockito>
    <version.dep.slf4j>2.0.0-alpha1</version.dep.slf4j>
    <version.dep.spotbugs>4.1.2</version.dep.spotbugs>
    
    <version.plugin.build-helper>3.2.0</version.plugin.build-helper>
    <version.plugin.checkstyle>3.1.1</version.plugin.checkstyle>
    <version.plugin.compiler>3.8.1</version.plugin.compiler>
    <version.plugin.coveralls>4.3.0</version.plugin.coveralls>
    <version.plugin.failsafe>2.22.2</version.plugin.failsafe>
    <version.plugin.jacoco>0.8.6</version.plugin.jacoco>
    <version.plugin.javadoc>3.2.0</version.plugin.javadoc>
    <version.plugin.source>3.2.1</version.plugin.source>
    <version.plugin.spotbugs>4.0.4</version.plugin.spotbugs>
    <version.plugin.surefire>2.22.2</version.plugin.surefire>
  </properties>
  
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${version.dep.logback}</version>
        <scope>runtime</scope>
      </dependency>
      <dependency>
        <groupId>com.github.spotbugs</groupId>
        <artifactId>spotbugs-annotations</artifactId>
        <version>${version.dep.spotbugs}</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${version.dep.junit}</version>
      </dependency>
      <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${version.dep.junit}</version>
      </dependency>
      <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>${version.dep.mockito}</version>
      </dependency>
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${version.dep.lombok}</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${version.dep.slf4j}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  
  <profiles>
    <!-- Code coverage reporting and enforcement -->
    <profile>
      <id>code-coverage</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <file>
          <exists>${basedir}/src/main/java</exists>
        </file>
      </activation>
      
      <build>
        <plugins>
          <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${version.plugin.jacoco}</version>
            <executions>
              <execution>
                <id>jacoco-merge-results</id>
                <phase>verify</phase>
                <goals>
                  <goal>merge</goal>
                </goals>
                <configuration>
                  <destFile>${project.build.directory}/coverage/jacoco-final.exec</destFile>
                  <fileSets>
                    <fileSet>
                      <directory>${project.build.directory}/coverage</directory>
                      <includes>
                        <include>*.exec</include>
                      </includes>
                    </fileSet>
                  </fileSets>
                </configuration>
              </execution>
              <execution>
                <id>jacoco-test-report</id>
                <goals>
                  <goal>report</goal>
                </goals>
                <configuration>
                  <dataFile>${project.build.directory}/coverage/jacoco-final.exec</dataFile>
                  <outputDirectory>${project.build.directory}/coverage/report</outputDirectory>
                </configuration>
              </execution>
              <execution>
                <id>jacoco-test-check</id>
                <goals>
                  <goal>check</goal>
                </goals>
                <configuration>
                  <dataFile>${project.build.directory}/coverage/jacoco-final.exec</dataFile>
                  <rules>
                    <rule>
                      <element>CLASS</element>
                      <excludes>
                        <exclude>*IT</exclude>
                        <exclude>*IT*</exclude>
                        <exclude>*Test</exclude>
                        <exclude>*Test*</exclude>
                      </excludes>
                      <limits>
                        <limit>
                          <counter>BRANCH</counter>
                          <value>COVEREDRATIO</value>
                          <minimum>${coverage.branch.minimum}</minimum>
                        </limit>
                        <limit>
                          <counter>INSTRUCTION</counter>
                          <value>COVEREDRATIO</value>
                          <minimum>${coverage.minimum}</minimum>
                        </limit>
                      </limits>
                    </rule>
                  </rules>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    
    <!-- coveralls.io support -->
    <profile>
      <id>coveralls</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        
        <property>
          <name>env.COVERALLS_REPO_TOKEN</name>
        </property>
        <file>
          <exists>${basedir}/src/main/java</exists>
        </file>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.eluder.coveralls</groupId>
            <artifactId>coveralls-maven-plugin</artifactId>
            <version>${version.plugin.coveralls}</version>
            <executions>
              <execution>
                <id>update-coveralls</id>
                <phase>verify</phase>
                <goals>
                  <goal>report</goal>
                </goals>
                <configuration>
                  <jacocoReports>${project.build.directory}/coverage/report/jacoco.xml</jacocoReports>
                  <repoToken>${env.COVERALLS_REPO_TOKEN}</repoToken>
                  <skip>${skipTests}</skip>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    
    <!-- Integration test support -->
    <profile>
      <id>integration-test</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <file>
          <exists>${basedir}/src/it</exists>
        </file>
      </activation>
      
      <dependencies>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-api</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-engine</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.mockito</groupId>
          <artifactId>mockito-core</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>
      
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>${version.plugin.failsafe}</version>
            <configuration>
              <argLine>${coverage.failsafe.args} ${failsafe.args.extra}</argLine>
              <parallel>classes</parallel>
              <threadCount>4</threadCount>
            </configuration>
            <executions>
              <execution>
                <id>integration-tests</id>
                <goals>
                  <goal>integration-test</goal>
                  <goal>verify</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>${version.plugin.build-helper}</version>
            <executions>
              <execution>
                <id>add-integration-test-sources</id>
                <goals>
                  <goal>add-test-source</goal>
                </goals>
                <configuration>
                  <sources>
                    <source>src/it/java</source>
                  </sources>
                </configuration>
              </execution>
              <execution>
                <id>add-integration-test-resources</id>
                <goals>
                  <goal>add-test-resource</goal>
                </goals>
                <configuration>
                  <resources>
                    <resource>
                      <directory>src/it/resources</directory>
                    </resource>
                  </resources>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${version.plugin.jacoco}</version>
            <executions>
              <execution>
                <id>jacoco-integration-test-prepare-agent</id>
                <goals>
                  <goal>prepare-agent-integration</goal>
                </goals>
                <configuration>
                  <destFile>${project.build.directory}/coverage/jacoco-integration.exec</destFile>
                  <propertyName>coverage.failsafe.args</propertyName>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    
    <!-- Basic settings for Java projects -->
    <profile>
      <id>java-project</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <file>
          <exists>${basedir}/src/main/java</exists>
        </file>
      </activation>
      
      <dependencies>
        <dependency>
          <groupId>com.github.spotbugs</groupId>
          <artifactId>spotbugs-annotations</artifactId>
        </dependency>
        <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
        </dependency>
      </dependencies>
      
      <build>
        <plugins>
          <plugin>
            <groupId>com.github.spotbugs</groupId>
            <artifactId>spotbugs-maven-plugin</artifactId>
            <version>${version.plugin.spotbugs}</version>
            <executions>
              <execution>
                <id>check-spotbugs</id>
                <phase>verify</phase>
                <goals>
                  <goal>check</goal>
                </goals>
                <configuration>
                  <maxRank>20</maxRank>
                  <threshold>Low</threshold>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>${version.plugin.checkstyle}</version>
            <dependencies>
              <dependency>
                <groupId>com.handcraftedbits</groupId>
                <artifactId>handcraftedbits-codequality</artifactId>
                <version>${version.dep.codequality}</version>
              </dependency>
            </dependencies>
            <executions>
              <execution>
                <id>check-style</id>
                <phase>process-classes</phase>
                <goals>
                  <goal>check</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <configLocation>checkstyle-handcraftedbits.xml</configLocation>
              <includeTestSourceDirectory>true</includeTestSourceDirectory>
              <logViolationsToConsole>true</logViolationsToConsole>
              <sourceDirectories>
                <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
              </sourceDirectories>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>${version.plugin.enforcer}</version>
            <executions>
              <execution>
                <id>enforce-java-version</id>
                <goals>
                  <goal>enforce</goal>
                </goals>
                <configuration>
                  <rules>
                    <requireJavaVersion>
                      <version>${version.java}</version>
                    </requireJavaVersion>
                  </rules>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    
    <!-- Additional plugins executed on release -->
    <profile>
      <id>release</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>

      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>${version.plugin.javadoc}</version>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <phase>prepare-package</phase>
                <goals>
                  <goal>jar</goal>
                </goals>
                <configuration>
                  <skip>${javadoc.skip}</skip>
                  <additionalparam>-Xdoclint:none</additionalparam>
                  <excludePackageNames>${javadoc.excludedPackages}</excludePackageNames>
                  <links combine.children="append" />
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>${version.plugin.source}</version>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar-no-fork</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    
    <!-- Unit test support -->
    <profile>
      <id>unit-test</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <file>
          <exists>${basedir}/src/test/java</exists>
        </file>
      </activation>
      
      <dependencies>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-api</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-engine</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.mockito</groupId>
          <artifactId>mockito-core</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>
      
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${version.plugin.surefire}</version>
            <configuration>
              <argLine>${coverage.surefire.args} ${surefire.args.extra}</argLine>
              <parallel>classes</parallel>
              <threadCount>4</threadCount>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${version.plugin.jacoco}</version>
            <executions>
              <execution>
                <id>jacoco-unit-test-prepare-agent</id>
                <goals>
                  <goal>prepare-agent</goal>
                </goals>
                <configuration>
                  <destFile>${project.build.directory}/coverage/jacoco-unit.exec</destFile>
                  <propertyName>coverage.surefire.args</propertyName>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  
  <!-- Always set the correct Java version, even for projects that don't contain any sources. -->
  
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>${version.plugin.compiler}</version>
          <configuration>
            <annotationProcessorPaths>
              <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${version.dep.lombok}</version>
              </path>
            </annotationProcessorPaths>
            <source>${version.java}</source>
            <target>${version.java}</target>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <!-- Only needed until https://github.com/rzwitserloot/lombok/issues/2125 is fixed. -->
  <repositories>
    <repository>
      <id>projectlombok.org</id>
      <url>https://projectlombok.org/edge-releases</url>
    </repository>
  </repositories>
</project>
