<?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.2</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.9</unmeshed-api-common.version><!-- S3 precompiled JAR version -->
    </properties>

    <build>
        <plugins>

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

            <!-- AntRun plugin: create dummy source and fetch precompiled JAR -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>create-dummy-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <mkdir dir="${project.build.directory}/generated-sources/dummy/io/unmeshed"/>
                                <echo file="${project.build.directory}/generated-sources/dummy/io/unmeshed/UnmeshedAPICommon.java">
                                    package io.unmeshed;

                                    /**
                                    * Unmeshed API Common - Precompiled JAR from S3
                                    * Placeholder class to satisfy Maven Central requirements.
                                    * Actual classes are contained in the precompiled JAR.
                                    */
                                    public class UnmeshedAPICommon {

                                    /**
                                    * Library Version
                                    */
                                    public static final String VERSION = "${project.version}";

                                    /**
                                    * Default constructor
                                    */
                                    public UnmeshedAPICommon() {
                                    // Placeholder constructor
                                    }
                                    }
                                </echo>
                            </target>
                        </configuration>
                    </execution>
                    <execution>
                        <id>download-and-replace-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 generated 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-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.build.directory}/generated-sources/dummy</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 (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>
                    </execution>
                </executions>
            </plugin>

            <!-- Create Javadoc JAR (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>
                            <release>8</release>
                            <quiet>true</quiet>
                        </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>
