<?xml version="1.0" encoding="UTF-8"?>
<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>io.kalix</groupId>
        <artifactId>kalix-maven-java</artifactId>
        <version>1.5.2</version>
    </parent>
    
    <!-- it has spring-boot-starter as parent, but groupId remains io.kalix -->
    <groupId>io.kalix</groupId>
    <artifactId>kalix-spring-boot-parent</artifactId>
    <version>1.5.2</version>
    <packaging>pom</packaging>


    <name>Kalix Spring Boot Parent pom</name>
    <description>
        This is the parent pom for Kalix applications based on Spring.
        It contains all dependencies and plugins to build a Kalix service using the Kalix Java SDK.
    </description>

    <properties>
        <!-- archetype need to override kalixContainerRegistry and kalixOrganization -->
        <kalixContainerRegistry>kcr.us-east-1.kalix.io</kalixContainerRegistry>
        <kalixOrganization>acme</kalixOrganization>

        <dockerImage>${kalixContainerRegistry}/${kalixOrganization}/${project.artifactId}</dockerImage>
        <dockerTag>${project.version}-${build.timestamp}</dockerTag>

        <docker.base.image>docker.io/library/eclipse-temurin:21.0.2_13-jre-jammy</docker.base.image>
        <docker.platform>linux/amd64</docker.platform>

        <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
        <java.version>21</java.version>
        <maven.compiler.release>${java.version}</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kalix-sdk.version>1.5.2</kalix-sdk.version>
        <skip.docker>false</skip.docker>


        <!-- plugin versions -->
        <spring-boot.version>3.2.5</spring-boot.version>
        <maven-shade-plugin.version>3.5.3</maven-shade-plugin.version>
        <maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
        <protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version>
        <docker-maven-plugin.version>0.44.0</docker-maven-plugin.version>
        <build-helper-maven-plugin.version>3.2.0</build-helper-maven-plugin.version>
        <maven-failsafe-plugin.version>3.1.2</maven-failsafe-plugin.version>
        <maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
        <maven-deploy-plugin.version>3.1.1</maven-deploy-plugin.version>

        <maven-failsafe-plugin.version>3.1.2</maven-failsafe-plugin.version>
    </properties>


    <pluginRepositories>
        <pluginRepository>
            <id>akka-repository</id>
            <url>https://repo.akka.io/maven</url>
        </pluginRepository>
    </pluginRepositories>

    <repositories>
        <repository>
            <id>akka-repository</id>
            <name>Akka library repository</name>
            <url>https://repo.akka.io/maven</url>
        </repository>
    </repositories>


    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
            </resource>
        </resources>

        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.7.1</version>
            </extension>
        </extensions>

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>${maven-shade-plugin.version}</version>
                    <configuration>
                        <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
                        <createDependencyReducedPom>true</createDependencyReducedPom>
                        <filters>
                          <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                              <exclude>META-INF/*.SF</exclude>
                              <exclude>META-INF/*.DSA</exclude>
                              <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                          </filter>
                        </filters>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-maven-plugin</artifactId>
                            <version>${spring-boot.version}</version>
                        </dependency>
                    </dependencies>
                    <executions>
                        <execution>
                            <phase>repackage</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                        <resource>META-INF/spring.handlers</resource>
                                        <resource>reference.conf</resource>
                                    </transformer>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                        <resource>META-INF/spring.schemas</resource>
                                    </transformer>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                        <resource>META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports</resource>
                                    </transformer>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                        <resource>META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports</resource>
                                    </transformer>
                                    <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                                        <resource>META-INF/spring.factories</resource>
                                    </transformer>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>${mainClass}</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven-compiler-plugin.version}</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <compilerArgs>
                            <arg>-Xlint:deprecation</arg>
                            <arg>-parameters</arg>
                        </compilerArgs>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>io.fabric8</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>${docker-maven-plugin.version}</version>
                    <configuration>
                        <images>
                            <image>
                                <name>${dockerImage}:%l</name>
                                <build>
                                    <!-- Base Docker image which contains jre-->
                                    <from>${docker.base.image}</from>
                                    <createImageOptions>
                                        <platform>${docker.platform}</platform>
                                    </createImageOptions>
                                    <tags>
                                        <!-- tag for generated image -->
                                        <tag>${dockerTag}</tag>
                                    </tags>
                                    <ports>
                                        <!-- expose port in Docker container -->
                                        <port>8080</port>
                                    </ports>
                                    <assembly>
                                        <!-- NOTE: (optional) switch to "artifact-with-dependencies" to show dependencies library-->
                                        <descriptorRef>artifact</descriptorRef>
                                    </assembly>
                                    <entryPoint>
                                        <arg>java</arg>
                                        <arg>-jar</arg>
                                        <arg>/maven/${project.build.finalName}.jar</arg>
                                    </entryPoint>
                                </build>
                            </image>
                        </images>
                    </configuration>
                    <executions>
                        <execution>
                            <id>build-docker-image</id>
                            <phase>install</phase>
                            <goals>
                                <goal>build</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>push-docker-image</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>push</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <!-- configure src/it/java and src/it/resources -->
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>${build-helper-maven-plugin.version}</version>
                    <executions>
                        <execution>
                            <id>add-integration-test-source</id>
                            <phase>generate-test-sources</phase>
                            <goals>
                                <goal>add-test-source</goal>
                            </goals>
                            <configuration>
                                <sources>
                                    <source>src/it/java</source>
                                </sources>
                            </configuration>
                        </execution>
                        <execution>
                            <id>add-integration-test-resource</id>
                            <phase>generate-test-resources</phase>
                            <goals>
                                <goal>add-test-resource</goal>
                            </goals>
                            <configuration>
                                <resources>
                                    <resource>
                                        <directory>${basedir}/src/it/resources</directory>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven-surefire-plugin.version}</version>
                    <configuration>
                        <excludes>
                            <!-- ignore integration test classes -->
                            <exclude>**/*IntegrationTest</exclude>
                        </excludes>
                    </configuration>
                </plugin>

                
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring-boot.version}</version>
                    <executions>
                        <execution>
                            <id>repackage</id>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <mainClass>${mainClass}</mainClass>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>io.kalix</groupId>
                    <artifactId>kalix-maven-plugin</artifactId>
                    <version>${kalix-sdk.version}</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>deploy</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <dockerImage>${dockerImage}:${dockerTag}</dockerImage>
                        <mainClass>${mainClass}</mainClass>
                    </configuration>
                </plugin>
                
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>${maven-deploy-plugin.version}</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

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

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

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

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

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <configuration>
                    <skip>${skip.docker}</skip>
                </configuration>    
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>io.kalix</groupId>
                <artifactId>kalix-maven-plugin</artifactId>
            </plugin>
            
        </plugins>
    </build>

    <profiles>
        <profile>
            <!-- run Integration Tests in src/it/java with `mvn verify -Pit`-->
            <id>it</id>
            <build>
                <plugins>
                    <plugin>
                        <!-- run *IntegrationTest with failsafe -->
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>${maven-failsafe-plugin.version}</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                                <configuration>
                                    <includes>
                                        <include>**/*IntegrationTest</include>
                                    </includes>
                                </configuration>
                            </execution>
                        </executions>
                        <configuration>
                            <classesDirectory>${project.build.outputDirectory}</classesDirectory>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.kalix</groupId>
                <artifactId>kalix-spring-boot-starter</artifactId>
                <version>${kalix-sdk.version}</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>io.kalix</groupId>
                <artifactId>kalix-spring-boot-starter-test</artifactId>
                <version>${kalix-sdk.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>io.kalix</groupId>
            <artifactId>kalix-spring-boot-starter</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.kalix</groupId>
            <artifactId>kalix-spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
