<?xml version="1.0" encoding="UTF-8"?>
<!--

    Copyright © 2025 DataSQRL (contact@datasqrl.com)

    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.datasqrl.flinkrunner</groupId>
    <artifactId>flink-sql-runner-parent</artifactId>
    <version>0.9.2</version>
  </parent>

  <artifactId>flink-sql-runner</artifactId>
  <name>Flink SQL Runner</name>

  <dependencies>
    <!-- Provided -->
    <dependency>
      <groupId>com.datasqrl.flinkrunner</groupId>
      <artifactId>stdlib-utils</artifactId>
      <version>${project.version}</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-core</artifactId>
      <version>${flink.version}</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-table-api-java-bridge</artifactId>
      <version>${flink.version}</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-table-planner-loader</artifactId>
      <version>${flink.version}</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-s3-fs-hadoop</artifactId>
      <version>${flink.version}</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.iceberg</groupId>
      <artifactId>iceberg-flink-runtime-2.0</artifactId>
      <version>${iceberg.version}</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.iceberg</groupId>
      <artifactId>iceberg-aws-bundle</artifactId>
      <version>${iceberg.version}</version>
      <scope>provided</scope>
    </dependency>

    <!-- Required for Iceberg to work -->
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-hdfs-client</artifactId>
      <version>${hadoop.version}</version>
      <scope>provided</scope>
      <exclusions>
        <exclusion>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
        </exclusion>
        <exclusion>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-mapreduce-client-core</artifactId>
      <scope>provided</scope>
    </dependency>

    <!-- Add logging framework, to produce console output when running in the IDE. -->
    <!-- These dependencies are excluded from the application JAR by default. -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <scope>provided</scope>
    </dependency>

    <!-- Compile -->
    <!-- Custom Connectors -->
    <dependency>
      <groupId>com.datasqrl.flinkrunner</groupId>
      <artifactId>kafka-safe-connector</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>com.datasqrl.flinkrunner</groupId>
      <artifactId>postgresql-connector</artifactId>
      <version>${project.version}</version>
    </dependency>

    <!-- Custom Formats -->
    <dependency>
      <groupId>com.datasqrl.flinkrunner</groupId>
      <artifactId>flexible-csv-format</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>com.datasqrl.flinkrunner</groupId>
      <artifactId>flexible-json-format</artifactId>
      <version>${project.version}</version>
    </dependency>

    <!-- StdLib -->
    <dependency>
      <groupId>com.datasqrl.flinkrunner</groupId>
      <artifactId>stdlib-commons</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>com.datasqrl.flinkrunner</groupId>
      <artifactId>stdlib-json</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>com.datasqrl.flinkrunner</groupId>
      <artifactId>stdlib-vector</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>com.datasqrl.flinkrunner</groupId>
      <artifactId>stdlib-text</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>com.datasqrl.flinkrunner</groupId>
      <artifactId>stdlib-math</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>com.datasqrl.flinkrunner</groupId>
      <artifactId>stdlib-openai</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>com.datasqrl.flinkrunner</groupId>
      <artifactId>stdlib-iceberg</artifactId>
      <version>${project.version}</version>
    </dependency>

    <!-- Flink Connectors -->
    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-connector-kafka</artifactId>
      <version>${kafka.conn.version}</version>
    </dependency>

    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-connector-jdbc-postgres</artifactId>
      <version>${jdbc.conn.version}</version>
    </dependency>

    <dependency>
      <groupId>com.getindata</groupId>
      <artifactId>flink-http-connector</artifactId>
      <version>${http.conn.version}</version>
    </dependency>

    <!-- Flink Formats -->
    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-avro-confluent-registry</artifactId>
      <version>${flink.version}</version>
    </dependency>

    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-parquet</artifactId>
      <version>${flink.version}</version>
    </dependency>

    <dependency>
      <groupId>info.picocli</groupId>
      <artifactId>picocli</artifactId>
      <version>${picocli.version}</version>
    </dependency>

    <dependency>
      <groupId>software.amazon.msk</groupId>
      <artifactId>aws-msk-iam-auth</artifactId>
      <version>${aws-msk-iam-auth.version}</version>
    </dependency>

    <!-- Runtime -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-slf4j-impl</artifactId>
      <scope>runtime</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <scope>runtime</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <scope>runtime</scope>
    </dependency>

    <!-- Test -->
    <dependency>
      <groupId>com.datasqrl.flinkrunner</groupId>
      <artifactId>udf-sample</artifactId>
      <version>${project.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.datasqrl.flinkrunner</groupId>
      <artifactId>system-functions-sample</artifactId>
      <version>${project.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-avro</artifactId>
      <version>${flink.version}</version>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-runtime-web</artifactId>
      <version>${flink.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.flink</groupId>
      <artifactId>flink-test-utils</artifactId>
      <version>${flink.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.nextbreakpoint</groupId>
      <artifactId>com.nextbreakpoint.flink.client</artifactId>
      <version>${nextbreakpoint.flink.client.version}</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-slf4j2-impl</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>org.awaitility</groupId>
      <artifactId>awaitility</artifactId>
      <version>${awaitility.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-exec</artifactId>
      <version>${commons-exec.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.testcontainers</groupId>
      <artifactId>testcontainers-junit-jupiter</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.testcontainers</groupId>
      <artifactId>testcontainers-postgresql</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.jdbi</groupId>
      <artifactId>jdbi3-core</artifactId>
      <version>${jdbi3.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.jdbi</groupId>
      <artifactId>jdbi3-sqlobject</artifactId>
      <version>${jdbi3.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.jacoco</groupId>
      <artifactId>org.jacoco.core</artifactId>
      <version>${jacoco.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.jacoco</groupId>
      <artifactId>org.jacoco.agent</artifactId>
      <version>${jacoco.version}</version>
      <classifier>runtime</classifier>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <resources>
      <resource>
        <targetPath>${project.basedir}/target</targetPath>
        <filtering>true</filtering>
        <directory>src/main/docker</directory>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.6.1</version>
        <executions>
          <!-- Run shade goal on package phase -->
          <execution>
            <goals>
              <goal>shade</goal>
            </goals>
            <phase>package</phase>
            <configuration>
              <finalName>flink-sql-runner.uber</finalName>
              <shadedArtifactAttached>true</shadedArtifactAttached>
              <shadedClassifierName>${flink.version.label}</shadedClassifierName>
              <createDependencyReducedPom>false</createDependencyReducedPom>
              <artifactSet>
                <excludes>
                  <!--
                  We have to exclude AutoRegisterSystemFunction from the shaded JAR because this class
                  is loaded via ServiceLoader from the Flink classpath (/opt/flink/lib), not the job JAR.
                  Including it here would result in a NoClassDefFoundError due to Flink's classloader isolation.

                  To overcome thus, we put `stdlib-commons` under /opt/flink/lib, so it will be available in the
                  proper classloader.
                  -->
                  <exclude>com.datasqrl.flinkrunner:stdlib-utils</exclude>
                  <exclude>org.apache.flink:flink-shaded-force-shading</exclude>
                  <exclude>com.google.code.findbugs:jsr305</exclude>
                  <exclude>org.slf4j:*</exclude>
                  <exclude>org.apache.logging.log4j:*</exclude>
                </excludes>
              </artifactSet>
              <filters>
                <!-- Exclude original AvroDeserializationSchema, because for now we overwrite that class downstream. -->
                <filter>
                  <artifact>org.apache.flink:flink-avro</artifact>
                  <excludes>
                    <exclude>org/apache/flink/formats/avro/AvroDeserializationSchema**</exclude>
                  </excludes>
                </filter>
                <filter>
                  <artifact>*:*</artifact>
                  <excludes>
                    <!--
                    Do not copy the signatures in the META-INF folder.
                    Otherwise, this might cause SecurityExceptions when using the JAR.
                    -->
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                  </excludes>
                </filter>
              </filters>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>com.datasqrl.flinkrunner.CliRunner</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>com.marvinformatics.jacoco</groupId>
        <artifactId>easy-jacoco-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>instrument-uber-jar</id>
            <goals>
              <goal>instrument-jar</goal>
            </goals>
            <configuration>
              <source>${project.build.directory}/flink-sql-runner.uber.jar</source>
              <destination>${project.build.directory}/flink-sql-runner.jacoco.jar</destination>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>${exec-maven-plugin.version}</version>
        <executions>
          <execution>
            <id>build-docker-image</id>
            <goals>
              <goal>exec</goal>
            </goals>
            <phase>package</phase>
            <configuration>
              <workingDirectory>${project.basedir}/target/</workingDirectory>
              <executable>docker</executable>
              <skip>${fast}</skip>
              <arguments>
                <argument>build</argument>
                <argument>--tag</argument>
                <argument>flink-sql-runner</argument>
                <argument>.</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- Copying S3, Iceberg, UDF JARs -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>unpack-iceberg-aws-bundle-without-logging</id>
            <goals>
              <goal>unpack</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>org.apache.iceberg</groupId>
                  <artifactId>iceberg-aws-bundle</artifactId>
                  <version>${iceberg.version}</version>
                  <type>jar</type>
                  <overWrite>true</overWrite>
                  <outputDirectory>${project.build.directory}/iceberg-aws-bundle-temp</outputDirectory>
                  <excludes>org/apache/logging/**,org/slf4j/**</excludes>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
          <execution>
            <id>copy-s3-fs-hadoop</id>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
              <includeGroupIds>org.apache.flink</includeGroupIds>
              <includeArtifactIds>flink-s3-fs-hadoop</includeArtifactIds>
              <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
          </execution>
          <execution>
            <id>copy-iceberg-runtime</id>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
              <includeGroupIds>org.apache.iceberg</includeGroupIds>
              <includeArtifactIds>iceberg-flink-runtime-2.0</includeArtifactIds>
              <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
          </execution>
          <execution>
            <id>copy-hdfs-client-for-iceberg</id>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
              <includeGroupIds>org.apache.hadoop</includeGroupIds>
              <includeArtifactIds>hadoop-hdfs-client</includeArtifactIds>
              <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
          </execution>
          <execution>
            <id>copy-hadoop-common-for-iceberg</id>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
              <includeGroupIds>org.apache.hadoop</includeGroupIds>
              <includeArtifactIds>hadoop-common</includeArtifactIds>
              <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
          </execution>
          <execution>
            <id>copy-hadoop-mapreduce-client-core-for-iceberg</id>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
              <includeGroupIds>org.apache.hadoop</includeGroupIds>
              <includeArtifactIds>hadoop-mapreduce-client-core</includeArtifactIds>
              <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
          </execution>
          <execution>
            <id>copy-stdlib-utils</id>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
              <includeGroupIds>com.datasqrl.flinkrunner</includeGroupIds>
              <includeArtifactIds>stdlib-utils</includeArtifactIds>
              <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
          </execution>
          <execution>
            <id>copy-udf-sample</id>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
              <includeGroupIds>com.datasqrl.flinkrunner</includeGroupIds>
              <includeArtifactIds>udf-sample</includeArtifactIds>
              <outputDirectory>${project.build.testOutputDirectory}/udfs</outputDirectory>
            </configuration>
          </execution>
          <execution>
            <id>copy-system-functions-sample</id>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
              <includeGroupIds>com.datasqrl.flinkrunner</includeGroupIds>
              <includeArtifactIds>system-functions-sample</includeArtifactIds>
              <outputDirectory>${project.build.testOutputDirectory}/systemfunction</outputDirectory>
            </configuration>
          </execution>
          <execution>
            <id>copy-jacoco-cli</id>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
              <includeArtifactIds>org.jacoco.agent</includeArtifactIds>
              <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- Repackage iceberg-aws-bundle without SLF4J using antrun plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <id>repack-iceberg-aws-bundle</id>
            <goals>
              <goal>run</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
              <target>
                <jar basedir="${project.build.directory}/iceberg-aws-bundle-temp" destfile="${project.build.directory}/iceberg-aws-bundle-${iceberg.version}.jar"/>
              </target>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>m2e</id>
      <activation>
        <property>
          <name>m2e.version</name>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <configuration>
              <skip>true</skip>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
