<?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
                             https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>io.unmeshed</groupId>
    <artifactId>unmeshed-api-common</artifactId>
    <version>${revision}</version>
    <packaging>jar</packaging>

    <name>unmeshed-api-common</name>
    <description>Common Java Library for Unmeshed Workflow Engine.</description>
    <url>https://github.com/unmeshed/unmeshed-java-sdk</url>

    <licenses>
        <license>
            <name>MIT License</name>
            <url>http://www.opensource.org/licenses/mit-license.php</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>unmeshed-devs</id>
            <name>Unmeshed Devs</name>
            <email>unmeshed-devs@unmeshed.com</email>
        </developer>
    </developers>

    <scm>
        <connection>scm:git:https://github.com/unmeshed/unmeshed-java-sdk.git</connection>
        <developerConnection>scm:git:ssh://github.com/unmeshed/unmeshed-java-sdk.git</developerConnection>
        <url>https://github.com/unmeshed/unmeshed-java-sdk</url>
    </scm>

    <properties>
        <!-- Version of this Maven module -->
        <revision>2.0.6</revision>
        <!-- Java and encoding -->
        <java.version>22.0.1</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
        <!-- External S3 JAR version, can remain older if needed -->
        <unmeshed-api-common.version>1.9.58</unmeshed-api-common.version><!-- S3 precompiled JAR version -->
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.38</version> <!-- use latest stable -->
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <release>16</release>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.38</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>

            <!-- Compiler plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <release>21</release>
                </configuration>
            </plugin>

            <!-- AntRun plugin: download and unpack real sources JAR, download compiled JAR -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.1.0</version>
                <executions>

                    <!-- Download and unpack -sources.jar from S3 into generated-sources -->
                    <execution>
                        <id>download-and-unpack-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <mkdir dir="${project.build.directory}/generated-sources/unmeshed"/>
                                <exec executable="aws" failonerror="true">
                                    <arg value="s3"/>
                                    <arg value="cp"/>
                                    <arg value="s3://unmeshed-sdk/maven-repo/io/unmeshed/unmeshed-api-common/${unmeshed-api-common.version}/unmeshed-api-common-${unmeshed-api-common.version}-sources.jar"/>
                                    <arg value="${project.build.directory}/unmeshed-api-common-sources.jar"/>
                                    <env key="AWS_ACCESS_KEY_ID" value="${env.AWS_ACCESS_KEY_ID}"/>
                                    <env key="AWS_SECRET_ACCESS_KEY" value="${env.AWS_SECRET_ACCESS_KEY}"/>
                                    <env key="AWS_REGION" value="${env.AWS_REGION}"/>
                                </exec>

                                <!-- unzip the sources into generated-sources -->
                                <unzip src="${project.build.directory}/unmeshed-api-common-sources.jar"
                                       dest="${project.build.directory}/generated-sources/unmeshed"/>
                            </target>
                        </configuration>
                    </execution>

                    <!-- Download compiled JAR from S3 to replace built artifact before packaging -->
                    <execution>
                        <id>download-compiled-jar</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <mkdir dir="${project.build.directory}"/>
                                <exec executable="aws">
                                    <arg value="s3"/>
                                    <arg value="cp"/>
                                    <arg value="s3://unmeshed-sdk/maven-repo/io/unmeshed/unmeshed-api-common/${unmeshed-api-common.version}/unmeshed-api-common-${unmeshed-api-common.version}.jar"/>
                                    <arg value="${project.build.directory}/${project.build.finalName}.jar"/>
                                    <env key="AWS_ACCESS_KEY_ID" value="${env.AWS_ACCESS_KEY_ID}"/>
                                    <env key="AWS_SECRET_ACCESS_KEY" value="${env.AWS_SECRET_ACCESS_KEY}"/>
                                    <env key="AWS_REGION" value="${env.AWS_REGION}"/>
                                </exec>
                            </target>
                        </configuration>
                    </execution>
                    <execution>
                        <id>replace-with-s3-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <exec executable="aws">
                                    <arg value="s3"/>
                                    <arg value="cp"/>
                                    <arg value="s3://unmeshed-sdk/maven-repo/io/unmeshed/unmeshed-api-common/${unmeshed-api-common.version}/unmeshed-api-common-${unmeshed-api-common.version}.jar"/>
                                    <arg value="${project.build.directory}/${project.build.finalName}.jar"/>
                                    <env key="AWS_ACCESS_KEY_ID" value="${env.AWS_ACCESS_KEY_ID}"/>
                                    <env key="AWS_SECRET_ACCESS_KEY" value="${env.AWS_SECRET_ACCESS_KEY}"/>
                                    <env key="AWS_REGION" value="${env.AWS_REGION}"/>
                                </exec>
                            </target>
                        </configuration>
                    </execution>

                </executions>
            </plugin>

            <!-- Add extracted sources to source path -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <id>add-extracted-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.build.directory}/generated-sources/unmeshed</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- JAR plugin will run normally and create artifact -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <forceCreation>true</forceCreation>
                </configuration>
            </plugin>

            <!-- Create sources JAR from extracted sources (required by Maven Central) -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                        <configuration>
                            <!-- Ensure the plugin picks up the generated sources -->
                            <includes>
                                <include>**/*.java</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Create Javadoc JAR from extracted sources (required by Maven Central) -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <sourcepath>${project.build.directory}/generated-sources/unmeshed</sourcepath>
                            <release>21</release>
                            <quiet>true</quiet>
                            <doclint>none</doclint>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- GPG Signing -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>${maven-gpg-plugin.version}</version>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                        <configuration>
                            <useAgent>true</useAgent>
                            <gpgArguments>
                                <arg>--batch</arg>
                                <arg>--pinentry-mode</arg>
                                <arg>loopback</arg>
                            </gpgArguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Nexus Staging / Central -->
            <plugin>
                <groupId>org.sonatype.central</groupId>
                <artifactId>central-publishing-maven-plugin</artifactId>
                <version>0.8.0</version>
                <extensions>true</extensions>
                <configuration>
                    <publishingServerId>ossrh</publishingServerId>
                    <autoPublish>true</autoPublish>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <distributionManagement>
        <snapshotRepository>
            <id>ossrh</id>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
            <id>ossrh</id>
            <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>

</project>
