<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright 2011-2024 Lime Mojito Pty Ltd
  ~
  ~    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="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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.limemojito.oss.standards</groupId>
        <artifactId>oss-maven-standards</artifactId>
        <version>14.0.19</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>java-development</artifactId>
    <packaging>pom</packaging>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.limemojito.oss.standards</groupId>
                <artifactId>library</artifactId>
                <version>14.0.19</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <properties>
        <coverage.line.ratio>0.95</coverage.line.ratio>
        <coverage.exclude.pattern.1 />
        <coverage.exclude.pattern.2 />
        <coverage.exclude.pattern.3 />
        <coverage.exclude.pattern.4 />
        <coverage.exclude.pattern.5 />

        <!-- Docker port to wait for -->
        <docker.compose.port>4566</docker.compose.port>

        <!-- Compiler configuration assuming JDK 11 or higher -->
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <maven.compiler.release.version>${java.version}</maven.compiler.release.version>

        <!--suppress HttpUrlsUsage -->
        <maven-checkstyle-plugin.configLocation>http://standards.limemojito.com/oss-checkstyle.xml
        </maven-checkstyle-plugin.configLocation>

        <!-- For internal build use only -->
        <coverage.exclude.build.pattern.1 />
        <coverage.exclude.build.pattern.2 />
        <coverage.exclude.build.pattern.3 />
        <coverage.exclude.build.pattern.4 />
        <coverage.exclude.build.pattern.5 />

        <test.compose.location>${project.basedir}/src/test/docker/docker-compose.yml</test.compose.location>
        <lombok.outputDirectory>${project.build.directory}/generated-sources/delombok</lombok.outputDirectory>

        <!-- This can be disabled by profile - suggest fast-build -->
        <skipTests>false</skipTests>
        <skipITs>false</skipITs>
        <lime.fast-build>false</lime.fast-build>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>

        <!-- Test Dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <!-- We define common CONFIGURATIONS here.  The poms representing a deliverable type have this one as a parent, and their build
        invokes the plugins -->
        <pluginManagement>
            <plugins>
                <plugin>
                    <!-- we use antrun to manage localstack compose -->
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <dependencies>
                        <dependency>
                            <groupId>ant-contrib</groupId>
                            <artifactId>ant-contrib</artifactId>
                            <version>1.0b3</version>
                            <exclusions>
                                <exclusion>
                                    <groupId>ant</groupId>
                                    <artifactId>ant</artifactId>
                                </exclusion>
                            </exclusions>
                        </dependency>
                    </dependencies>
                    <executions>
                        <execution>
                            <id>identify-docker-compose</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <exportAntProperties>true</exportAntProperties>
                                <target>
                                    <condition property="docker.compose.skip" else="false">
                                        <or>
                                            <equals arg1="${lime.fast-build}" arg2="true" />
                                            <not>
                                                <available file="${test.compose.location}" />
                                            </not>
                                        </or>
                                    </condition>
                                    <!--suppress UnresolvedMavenProperty -->
                                    <echo level="info" message="docker.compose.skip is ${docker.compose.skip}" />
                                </target>
                            </configuration>
                        </execution>
                        <execution>
                            <id>wait-for-docker</id>
                            <phase>integration-test</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target xmlns:ac="antlib:net.sf.antcontrib">
                                    <ac:if>
                                        <!--suppress UnresolvedMavenProperty -->
                                        <equals arg1="${docker.compose.skip}" arg2="false" />
                                        <then>
                                            <echo level="info" message="Waiting for Docker on port ${docker.compose.port}" />
                                            <waitfor maxWait="2" maxWaitUnit="minute">
                                                <http url="http://localhost:${docker.compose.port}" />
                                            </waitfor>
                                        </then>
                                    </ac:if>
                                </target>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>com.dkanejs.maven.plugins</groupId>
                    <artifactId>docker-compose-maven-plugin</artifactId>
                    <configuration>
                        <composeFile>${project.basedir}/src/test/docker/docker-compose.yml</composeFile>
                        <!-- Set by antrun task execution -->
                        <!--suppress UnresolvedMavenProperty, MavenModelInspection -->
                        <skip>${docker.compose.skip}</skip>
                    </configuration>
                    <executions>
                        <execution>
                            <id>docker-up</id>
                            <goals>
                                <goal>up</goal>
                            </goals>
                            <phase>pre-integration-test</phase>
                            <configuration>
                                <detachedMode>true</detachedMode>
                            </configuration>
                        </execution>
                        <execution>
                            <id>docker-down</id>
                            <goals>
                                <goal>down</goal>
                            </goals>
                            <phase>post-integration-test</phase>
                            <configuration>
                                <removeOrphans>true</removeOrphans>
                                <removeVolumes>true</removeVolumes>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-checkstyle-plugin</artifactId>
                    <dependencies>
                        <dependency>
                            <groupId>com.puppycrawl.tools</groupId>
                            <artifactId>checkstyle</artifactId>
                            <version>${checkstyle.version}</version>
                            <exclusions>
                                <!--  MCHECKSTYLE-156  -->
                                <exclusion>
                                    <groupId>com.sun</groupId>
                                    <artifactId>tools</artifactId>
                                </exclusion>
                            </exclusions>
                        </dependency>
                    </dependencies>
                    <executions>
                        <execution>
                            <id>validate</id>
                            <phase>process-classes</phase>
                            <goals>
                                <goal>check</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <configLocation>${maven-checkstyle-plugin.configLocation}</configLocation>
                        <consoleOutput>true</consoleOutput>
                        <failsOnError>true</failsOnError>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <!-- note using release option on JDK 9+ -->
                        <release>${java.version}</release>
                        <compilerArgs>
                            <compilerArg>-Xlint:all</compilerArg>
                            <!-- Due to spurious warnings with Lombok -->
                            <compilerArg>-Xlint:-processing</compilerArg>
                            <!-- Serialization is rare now.  IDE assistance covers most situations -->
                            <compilerArg>-Xlint:-serial</compilerArg>
                            <compilerArg>-Werror</compilerArg>
                        </compilerArgs>
                        <showWarnings>true</showWarnings>
                        <showDeprecation>true</showDeprecation>
                        <failOnError>true</failOnError>
                        <annotationProcessorPaths>
                            <annotationProcessorPath>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                                <version>${lombok.version}</version>
                            </annotationProcessorPath>
                        </annotationProcessorPaths>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-enforcer-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>enforce-versions</id>
                            <goals>
                                <goal>enforce</goal>
                            </goals>
                            <configuration>
                                <rules>
                                    <requireMavenVersion>
                                        <version>[3.8.1,)</version>
                                    </requireMavenVersion>
                                    <requireJavaVersion>
                                        <version>[17.0.3,)</version>
                                    </requireJavaVersion>
                                    <DependencyConvergence />
                                    <bannedDependencies>
                                        <excludes>
                                            <exclude>org.hamcrest:hamcrest-all</exclude>
                                            <!-- Old javax apis require EE9+ from now on -->
                                            <exclue>javax.servlet</exclue>
                                            <exclue>javax.xml.bind</exclue>
                                            <exclue>javax.jms</exclue>
                                            <exclue>javax.jms</exclue>
                                            <exclue>javax.validation</exclue>
                                        </excludes>
                                        <searchTransitive>true</searchTransitive>
                                    </bannedDependencies>
                                </rules>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <configuration>
                        <!-- as Lombok generated classes are NOT seen by boot -->
                        <classesDirectory>${project.build.outputDirectory}</classesDirectory>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <addDefaultEntries>true</addDefaultEntries>
                                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                                <addBuildEnvironmentEntries>true</addBuildEnvironmentEntries>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <configuration>
                        <propertiesEncoding>ISO-8859-1</propertiesEncoding>
                        <nonFilteredFileExtensions>
                            <nonFilteredFileExtension>bi5</nonFilteredFileExtension>
                            <nonFilteredFileExtension>dll</nonFilteredFileExtension>
                            <nonFilteredFileExtension>doc</nonFilteredFileExtension>
                            <nonFilteredFileExtension>exe</nonFilteredFileExtension>
                            <nonFilteredFileExtension>so</nonFilteredFileExtension>
                            <nonFilteredFileExtension>xls</nonFilteredFileExtension>
                            <nonFilteredFileExtension>zip</nonFilteredFileExtension>
                        </nonFilteredFileExtensions>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <!-- Include set arglines such as jacoco -->
                        <!--suppress MavenModelInspection -->
                        <argLine>${argLine}</argLine>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>default-prepare-agent</id>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>default-report</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>report</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>default-check</id>
                            <goals>
                                <goal>check</goal>
                            </goals>
                            <!-- We don't care about integration tests -->
                            <phase>package</phase>
                            <!-- Configuration is here to make it easier to override though use the properties first. -->
                            <configuration>
                                <rules>
                                    <rule implementation="org.jacoco.maven.RuleConfiguration">
                                        <element>CLASS</element>
                                        <excludes>
                                            <!-- Exclude for Spring configuration package as these are too hard to unit test -->
                                            <exclude>*.config.*</exclude>
                                            <exclude>*.Config</exclude>
                                            <exclude>*.Configuration</exclude>
                                            <!-- Exclude for *.1 classes produced by the compiler for things like switch statements -->
                                            <exclude>*.?</exclude>
                                            <!-- General exclude patterns from other projects -->
                                            <exclude>${coverage.exclude.pattern.1}</exclude>
                                            <exclude>${coverage.exclude.pattern.2}</exclude>
                                            <exclude>${coverage.exclude.pattern.3}</exclude>
                                            <exclude>${coverage.exclude.pattern.4}</exclude>
                                            <exclude>${coverage.exclude.pattern.5}</exclude>

                                            <!-- Build exclude patterns -->
                                            <exclude>${coverage.exclude.build.pattern.1}</exclude>
                                            <exclude>${coverage.exclude.build.pattern.2}</exclude>
                                            <exclude>${coverage.exclude.build.pattern.3}</exclude>
                                            <exclude>${coverage.exclude.build.pattern.4}</exclude>
                                            <exclude>${coverage.exclude.build.pattern.5}</exclude>
                                        </excludes>
                                        <limits>
                                            <limit implementation="org.jacoco.report.check.Limit">
                                                <counter>LINE</counter>
                                                <value>COVEREDRATIO</value>
                                                <minimum>${coverage.line.ratio}</minimum>
                                            </limit>
                                        </limits>
                                    </rule>
                                </rules>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok-maven-plugin</artifactId>
                    <dependencies>
                        <dependency>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </dependency>
                    </dependencies>
                    <executions>
                        <execution>
                            <phase>verify</phase>
                            <goals>
                                <goal>delombok</goal>
                            </goals>
                            <configuration>
                                <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
                                <outputDirectory>${lombok.outputDirectory}</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

    <profiles>
        <profile>
            <id>fast-build</id>
            <properties>
                <checkstyle.skip>true</checkstyle.skip>
                <codenarc.skip>true</codenarc.skip>
                <enforcer.skip>true</enforcer.skip>
                <jacoco.skip>true</jacoco.skip>
                <skipTests>true</skipTests>
                <skipITs>true</skipITs>
                <!-- preferred way to skip for fast build -->
                <lime.fast-build>true</lime.fast-build>
            </properties>
        </profile>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <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-javadoc-plugin</artifactId>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                                <phase>verify</phase>
                                <configuration>
                                    <sourcepath>${lombok.outputDirectory}:${project.build.sourceDirectory}</sourcepath>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
