<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright 2023 Amazon.com, Inc. or its affiliates.
  ~ 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://maven.apache.org/POM/4.0.0"
    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>software.amazon.lambda</groupId>
        <artifactId>powertools-idempotency</artifactId>
        <version>2.9.0</version>
    </parent>

    <artifactId>powertools-idempotency-dynamodb</artifactId>
    <packaging>jar</packaging>

    <name>Powertools for AWS Lambda (Java) library Idempotency - DynamoDB</name>
    <description>
        DynamoDB implementation for the idempotency module
    </description>

    <dependencies>
        <dependency>
            <groupId>software.amazon.lambda</groupId>
            <artifactId>powertools-idempotency-core</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>url-connection-client</artifactId>
            <version>${aws.sdk.version}</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>dynamodb</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>software.amazon.awssdk</groupId>
                    <artifactId>netty-nio-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>software.amazon.awssdk</groupId>
                    <artifactId>apache-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>sdk-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.crac</groupId>
            <artifactId>crac</artifactId>
        </dependency>

        <!-- Test dependencies -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>software.amazon.lambda</groupId>
            <artifactId>powertools-common</artifactId>
            <version>${project.version}</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>
        <!-- DynamoDB Local for testing -->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>DynamoDBLocal</artifactId>
            <!-- >2.2.0 is not compatible with Java 11 anymore -->
            <!-- https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocalHistory.html -->
            <version>2.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>generate-graalvm-files</id>
            <!-- The AWS DynamoDBClient uses com.amazonaws.xray.interceptors.TracingInterceptor if available on the
            classpath. If the DynamoDB persistence store is used together with tracing, this would fail in GraalVM 
            otherwise since it tries to load the tracing interceptor at runtime. This makes sure it is included in
            reflect-config.json -->
            <dependencies>
                <dependency>
                    <groupId>com.amazonaws</groupId>
                    <artifactId>aws-xray-recorder-sdk-aws-sdk-v2-instrumentor</artifactId>
                    <scope>test</scope>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <argLine>
                                -Dorg.graalvm.nativeimage.imagecode=agent
                                -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image/software.amazon.lambda/powertools-idempotency-dynamodb,experimental-class-define-support
                                --add-opens java.base/java.util=ALL-UNNAMED
                                --add-opens java.base/java.lang=ALL-UNNAMED
                            </argLine>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>graalvm-native</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.graalvm.buildtools</groupId>
                        <artifactId>native-maven-plugin</artifactId>
                        <version>0.11.2</version>
                        <extensions>true</extensions>
                        <executions>
                            <execution>
                                <id>test-native</id>
                                <goals>
                                    <goal>test</goal>
                                </goals>
                                <phase>test</phase>
                            </execution>
                        </executions>
                        <configuration>
                            <imageName>powertools-idempotency-dynamodb</imageName>
                            <exclusions>
                                <exclusion>
                                    <groupId>com.amazonaws</groupId>
                                    <artifactId>DynamoDBLocal</artifactId>
                                </exclusion>
                            </exclusions>
                            <buildArgs>
                                <buildArg>--add-opens java.base/java.util=ALL-UNNAMED</buildArg>
                                <buildArg>--add-opens java.base/java.lang=ALL-UNNAMED</buildArg>
                                <buildArg>--enable-url-protocols=http</buildArg>
                                <buildArg>--no-fallback</buildArg>
                                <buildArg>--verbose</buildArg>
                                <buildArg>--native-image-info</buildArg>
                                <buildArg>-H:+UnlockExperimentalVMOptions</buildArg>
                                <buildArg>-H:+ReportExceptionStackTraces</buildArg>
                            </buildArgs>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <build>
        <plugins>
            <!-- Copy DynamoDB Local JAR -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dynamodb-local</id>
                        <phase>generate-test-resources</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeScope>test</includeScope>
                            <excludeTransitive>false</excludeTransitive>
                            <!-- MDEP-187: Avoids Maven phase circular dependency by not trying to copy unpacked reactor
                            artifacts (powertools-idempotency-core)  -->
                            <excludeGroupIds>software.amazon.lambda</excludeGroupIds>
                            <outputDirectory>${project.build.directory}/dynamodb-local</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- Start DynamoDB Local before tests -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>start-dynamodb-local</id>
                        <phase>process-test-classes</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>java</executable>
                            <workingDirectory>${project.build.directory}/dynamodb-local</workingDirectory>
                            <arguments>
                                <argument>-Djava.library.path=${project.build.directory}/dynamodb-local</argument>
                                <argument>-cp</argument>
                                <argument>${project.build.directory}/dynamodb-local/*</argument>
                                <argument>com.amazonaws.services.dynamodbv2.local.main.ServerRunner</argument>
                                <argument>-inMemory</argument>
                                <argument>-port</argument>
                                <argument>8000</argument>
                            </arguments>
                            <async>true</async>
                            <asyncDestroyOnShutdown>true</asyncDestroyOnShutdown>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.4.2</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Automatic-Module-Name>software.amazon.awssdk.enhanced.dynamodb</Automatic-Module-Name>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <!-- Configure Surefire to expose external DynamoDB Local address -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <systemPropertyVariables>
                        <dynamodb.endpoint>http://localhost:8000</dynamodb.endpoint>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <plugin>
                <groupId>dev.aspectj</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>${aspectj-maven-plugin.version}</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <complianceLevel>${maven.compiler.target}</complianceLevel>
                    <Xlint>ignore</Xlint>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>software.amazon.lambda</groupId>
                            <artifactId>powertools-idempotency-core</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>
