<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>dev.ikm.build</groupId>
        <artifactId>parent</artifactId>
        <version>1.58.0</version>
    </parent>
    <artifactId>java-parent</artifactId>
    <name>Java Parent</name>
    <description>
        The Java Parent that helps to define the build processes that should be used across projects. This is versioned
        and if you want to use a different version of the pom, you can simply change the parent version, and it will
        adjust all the properties being used throughout, as the properties and plugins will be inherited by
        subprojects.
    </description>
    <packaging>pom</packaging>

    <properties>
        <!-- make sure to change the hard coded java version in the enforcer as well -->

        <java.version>23</java.version>

        <!-- Default Lifecycle additions -->
        <maven.resources-plugin.version>3.2.0</maven.resources-plugin.version>
        <maven.surefire-plugin.version>3.5.2</maven.surefire-plugin.version>
        <maven.surefire-report-plugin.version>3.5.2</maven.surefire-report-plugin.version>
        <maven.failsafe-plugin.version>3.5.2</maven.failsafe-plugin.version>
        <maven.surefire-junit-platform.version>3.5.2</maven.surefire-junit-platform.version>
        <maven.compiler-plugin.version>3.13.0</maven.compiler-plugin.version>
        <maven.jar-plugin.version>3.4.2</maven.jar-plugin.version>
        <maven.war-plugin.version>3.4.0</maven.war-plugin.version>
        <maven-javadoc-plugin.version>3.11.2</maven-javadoc-plugin.version>
        <maven-source-plugin.version>3.3.0</maven-source-plugin.version>
        <maven.resources-plugin.version>3.3.1</maven.resources-plugin.version>

        <!-- Failsafe / Surefire config -->
        <failsafeArgLine>--enable-preview</failsafeArgLine>
        <surefireArgLine>--enable-preview</surefireArgLine>

        <!-- Compiler Extensions -->
        <record-builder-core.version>34</record-builder-core.version>
        <auto-service.version>1.0.1</auto-service.version>

        <!-- Style and Code Quality Check Plugins -->
        <maven.checkstyle-plugin.version>3.6.0</maven.checkstyle-plugin.version>
        <checkstyle.version>10.21.2</checkstyle.version>
        <maven-pmd-plugin.version>3.26.0</maven-pmd-plugin.version>
        <maven.jacoco-plugin.version>0.8.12</maven.jacoco-plugin.version>
        <maven.spotbugs-plugin.version>4.8.6.6</maven.spotbugs-plugin.version>
        <jlgrock.checkstyle.version>2.1.0</jlgrock.checkstyle.version>
        <maven.pom-check-maven-plugin.version>1.0.0</maven.pom-check-maven-plugin.version>
        <junit.version>5.11.4</junit.version>
        <slf4j.version>2.0.16</slf4j.version>

        <eclipse-plugin.version>1.0.0</eclipse-plugin.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>${junit.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>${slf4j.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <!--======================================================-->
                <!-- Default (JAR/WAR/EAR) Lifecycle                      -->
                <!--======================================================-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>${maven.resources-plugin.version}</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven.surefire-plugin.version}</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>${maven.failsafe-plugin.version}</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven.compiler-plugin.version}</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>${maven.war-plugin.version}</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                    <version>${maven-javadoc-plugin.version}</version>
                </plugin>

                <!--======================================================-->
                <!-- Code Quality Check Plugins                           -->
                <!--======================================================-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>${maven.dependency-plugin.version}</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-pmd-plugin</artifactId>
                    <version>${maven-pmd-plugin.version}</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-checkstyle-plugin</artifactId>
                    <version>${maven.checkstyle-plugin.version}</version>
                    <dependencies>
                        <dependency>
                            <groupId>com.github.jlgrock</groupId>
                            <artifactId>personal-checkstyle</artifactId>
                            <version>${jlgrock.checkstyle.version}</version>
                            <classifier>checkstyle-resources</classifier>
                            <type>zip</type>
                        </dependency>
                        <!-- Required version of checkstyle for newer versions of java. This can be
                         removed if we get to a point that we are using an LTS version of java -->
                        <dependency>
                            <groupId>com.puppycrawl.tools</groupId>
                            <artifactId>checkstyle</artifactId>
                            <version>${checkstyle.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>

                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>${maven.jacoco-plugin.version}</version>
                </plugin>

                <!--Spot bugs -->
                <plugin>
                    <groupId>com.github.spotbugs</groupId>
                    <artifactId>spotbugs-maven-plugin</artifactId>
                    <version>${maven.spotbugs-plugin.version}</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>${maven.site.plugin}</version>
                </plugin>

                <plugin>
                    <groupId>com.github.jlgrock</groupId>
                    <artifactId>pom-check-maven-plugin</artifactId>
                    <version>${maven.pom-check-maven-plugin.version}</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>${maven.jar-plugin.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <release>${java.version}</release>
                    <compilerArgs>
                        <arg>--enable-preview</arg>
                    </compilerArgs>
                    <fork>true</fork>
                    <annotationProcessorPaths>
                        <annotationProcessorPath>
                            <groupId>io.soabase.record-builder</groupId>
                            <artifactId>record-builder-processor</artifactId>
                            <version>${record-builder-core.version}</version>
                        </annotationProcessorPath>
                        <annotationProcessorPath>
                            <groupId>com.google.auto.service</groupId>
                            <artifactId>auto-service</artifactId>
                            <version>${auto-service.version}</version>
                        </annotationProcessorPath>
                    </annotationProcessorPaths>
                    <annotationProcessors>
                        <annotationProcessor>io.soabase.recordbuilder.processor.RecordBuilderProcessor
                        </annotationProcessor>
                        <annotationProcessor>com.google.auto.service.processor.AutoServiceProcessor
                        </annotationProcessor>
                    </annotationProcessors>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useModulePath>true</useModulePath>
                    <argLine>${surefireArgLine}</argLine>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <configuration>
                    <useModulePath>true</useModulePath>
                    <argLine>${failsafeArgLine}</argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <reporting>
        <plugins>
            <!--======================================================-->
            <!-- Java Code Quality Report Plugins                     -->
            <!--======================================================-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>javadoc</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>com.github.spotbugs</groupId>
                <artifactId>spotbugs-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <configuration>
                    <dataFile>${project.build.directory}/jacoco/jacoco-combined.exec</dataFile>
                </configuration>
                <reportSets>
                    <reportSet>
                        <reports>
                            <!-- select non-aggregate reports -->
                            <report>report</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <configuration>
                    <configLocation>checkstyle/production-checkstyle.xml</configLocation>
                    <outputEncoding>${project.reporting.outputEncoding}</outputEncoding>
                </configuration>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>checkstyle</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
    </reporting>

    <profiles>
        <profile>
            <id>java-source-additional-tasks</id>
            <activation>
                <file>
                    <exists>${basedir}/src/main/java</exists>
                </file>
            </activation>
            <build>
                <!-- apply licenses to all java source files -->
                <plugins>
                    <!-- Generate Java Doc jar -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <configuration>
                            <doclint>none</doclint>
                            <source>${java.version}</source>
                            <release>${java.version}</release>
                        </configuration>
                        <executions>
                            <execution>
                                <id>jar</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Source Files -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>java-tests-additional-tasks</id>
            <activation>
                <file>
                    <exists>${basedir}/src/test/java</exists>
                </file>
            </activation>
            <build>
                <plugins>
                    <!-- bundle test classes into a jar -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>jar-tests-classes</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>test-jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Source Files -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>attach-test-sources</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>test-jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <!-- To disable this, use something like "mvn install -P!codeQuality" -->
            <id>codeQuality</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <!--======================================================-->
                    <!-- Code Quality Checks                                  -->
                    <!--======================================================-->

                    <!-- Turn on the surefire plugin as part of the standard test in the build lifecycle -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <useModulePath>true</useModulePath>
                            <argLine>${surefireArgLine}</argLine>
                        </configuration>
                    </plugin>

                    <!-- Turn on the failsafe plugin as part of the integration test in the build lifecycle -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <dependencies>
                            <!-- https://maven.apache.org/surefire/maven-failsafe-plugin/examples/junit-platform.html -->
                            <dependency>
                                <groupId>org.apache.maven.surefire</groupId>
                                <artifactId>surefire-junit-platform</artifactId>
                                <version>${maven.surefire-junit-platform.version}</version>
                            </dependency>
                        </dependencies>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <forkCount>0.5C</forkCount>
                            <reuseForks>false</reuseForks>
                            <useModulePath>true</useModulePath> <!-- default is true, just being explicit -->
                            <!-- Sets the VM argument line used when integration tests are run. -->
                            <argLine>${failsafeArgLine}</argLine>
                        </configuration>
                    </plugin>

                    <!-- Dependency check -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <configuration>
                            <checkModificationExcludes>
                                <checkModificationExclude>pom.xml</checkModificationExclude>
                            </checkModificationExcludes>
                        </configuration>
                        <executions>
                            <execution>
                                <id>analyze</id>
                                <goals>
                                    <goal>analyze-only</goal>
                                </goals>
                                <configuration>
                                    <failOnWarning>false</failOnWarning>
                                    <outputXML>true</outputXML>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- SPOTBUGS CHECK: Fails build if there are issues -->
                    <plugin>
                        <groupId>com.github.spotbugs</groupId>
                        <artifactId>spotbugs-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>check</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <effort>Max</effort>
                            <threshold>Medium</threshold>
                            <xmlOutput>true</xmlOutput>
                            <failOnError>false</failOnError>
                            <!-- Add this configuration to your build if you need to exclude Spotbugs findings -->
                            <!--<excludeFilterFile>${project.rootdir}/spotbugs-exclude.xml</excludeFilterFile>-->
                        </configuration>
                    </plugin>

                    <!-- PMD CHECK: Fails build if there are "High" priority violations -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-pmd-plugin</artifactId>
                        <configuration>
                            <failurePriority>1</failurePriority>
                            <failOnViolation>false</failOnViolation>
                            <aggregate>true</aggregate>
                             <targetJdk>20</targetJdk>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>check</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Code Coverage Check -->
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <executions>
                            <!-- Prepares the property pointing to the JaCoCo runtime agent which
                                is passed as VM argument when Maven the Surefire plugin is executed. -->
                            <execution>
                                <id>pre-unit-test</id>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                                <configuration>
                                    <destFile>${project.build.directory}/jacoco/jacoco-ut.exec</destFile>
                                    <!-- Sets the name of the property containing the settings for JaCoCo
                                        runtime agent. -->
                                    <propertyName>surefireArgLine</propertyName>
                                </configuration>
                            </execution>

                            <!-- Prepares the property pointing to the JaCoCo runtime agent which
                                is passed as VM argument when Maven the Failsafe plugin is executed. -->
                            <execution>
                                <id>pre-integration-test</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                                <configuration>
                                    <!-- Sets the path to the file which contains the execution data. -->
                                    <destFile>${project.build.directory}/jacoco/jacoco-it.exec</destFile>
                                    <!-- Sets the name of the property containing the settings for JaCoCo
                                        runtime agent. -->
                                    <propertyName>failsafeArgLine</propertyName>
                                </configuration>
                            </execution>

                            <!-- Create a unified coverage report of unit and integration testing -->
                            <execution>
                                <id>merge-ut-and-it</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>merge</goal>
                                </goals>
                                <configuration>
                                    <fileSets>
                                        <fileSet>
                                            <directory>${project.build.directory}/jacoco/</directory>
                                            <includes>
                                                <include>*.exec</include>
                                            </includes>
                                        </fileSet>
                                    </fileSets>
                                    <destFile>${project.build.directory}/jacoco/jacoco-combined.exec</destFile>
                                </configuration>
                            </execution>

                            <!-- Ensures that the code coverage report for integration tests after
                                integration tests have been run. -->
                            <execution>
                                <id>post-integration-test</id>
                                <goals>
                                    <goal>report</goal>
                                </goals>
                                <configuration>
                                    <!-- Sets the path to the file which contains the execution data. -->
                                    <dataFile>${project.build.directory}/jacoco/jacoco-combined.exec</dataFile>
                                    <!-- Sets the output directory for the code coverage report. -->
                                    <outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
                                </configuration>
                            </execution>

                            <!-- the check goal by default runs in the verify phase, we want to
                                fail the build if minimum code coverage checks aren't met -->
                            <execution>
                                <id>check</id>
                                <goals>
                                    <goal>check</goal>
                                </goals>
                                <configuration>
                                    <excludes>
                                        <!-- exclude config & POJO classes from coverage requirements -->
                                        <exclude>**/*Application.*</exclude>
                                        <exclude>**/config/*.*</exclude>
                                        <exclude>**/aop/*.*</exclude>
                                        <exclude>**/*Advice.*</exclude>
                                    </excludes>

                                    <dataFile>${project.build.directory}/jacoco/jacoco-combined.exec</dataFile>
                                    <haltOnFailure>false</haltOnFailure>
                                    <rules>
                                        <!-- All classes must have 100% line and 100% branch coverage .
                                            Note we use 2 d.p so that we get can any check failure messages reported
                                            to 2 d.p -->
                                        <rule>
                                            <element>BUNDLE</element>
                                            <limits>
                                                <limit>
                                                    <counter>LINE</counter>
                                                    <value>COVEREDRATIO</value>
                                                    <minimum>1</minimum>
                                                </limit>
                                                <limit>
                                                    <counter>BRANCH</counter>
                                                    <value>COVEREDRATIO</value>
                                                    <minimum>1</minimum>
                                                </limit>
                                            </limits>
                                        </rule>
                                    </rules>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Check Style: Make sure that it matches basic formatting and usage
                            requirements -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-checkstyle-plugin</artifactId>
                        <executions>
                            <execution>
                                <configuration>
                                    <configLocation>checkstyle/production-checkstyle.xml</configLocation>
                                    <encoding>${project.build.sourceEncoding}</encoding>
                                    <consoleOutput>true</consoleOutput>
                                    <violationSeverity>error</violationSeverity>
                                    <failsOnError>false</failsOnError>
                                    <failOnViolation>false</failOnViolation>
                                </configuration>
                                <goals>
                                    <goal>check</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Use of Enforcer plugin to:
                         * Require a specific Java Version in all poms
                     -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-enforcer-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>enforce-java-version</id>
                                <goals>
                                    <goal>enforce</goal>
                                </goals>
                                <phase>validate</phase>
                                <configuration>
                                    <rules>
                                        <requireJavaVersion>
                                            <version>${java.version}</version>
                                        </requireJavaVersion>
                                    </rules>
                                    <fail>true</fail>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>

            <reporting>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-report-plugin</artifactId>
                        <version>${maven.surefire-report-plugin.version}</version>
                        <reportSets>
                            <reportSet>
                                <id>unit-tests</id>
                                <reports>
                                    <report>report-only</report>
                                </reports>
                            </reportSet>
                            <reportSet>
                                <id>integration-tests</id>
                                <reports>
                                    <report>failsafe-report-only</report>
                                </reports>
                            </reportSet>
                        </reportSets>
                        <configuration>
                            <reportsDirectories>
                                <trimStackTrace>false</trimStackTrace>
                            </reportsDirectories>
                        </configuration>
                    </plugin>
                </plugins>
            </reporting>
        </profile>

    </profiles>
</project>
