<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (C) 2016/2022 Litote
  ~
  ~ 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>org.litote.kmongo</groupId>
        <artifactId>kmongo-root</artifactId>
        <version>4.5.1-SNAPSHOT</version>
    </parent>

    <artifactId>kmongo-benchmark</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>common</module>
        <module>jackson</module>
        <module>native</module>
        <module>serialization</module>
    </modules>

    <name>KMongo JMH benchmark</name>

    <dependencies>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-core</artifactId>
            <version>${jmh.version}</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.litote.kmongo</groupId>
                <artifactId>kmongo-benchmark-common</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <properties>
        <!--
            Select a JMH benchmark generator to use. Available options:
               default:    whatever JMH chooses by default;
               asm:        parse bytecode with ASM;
               reflection: load classes and use Reflection over them;
          -->
        <jmh.generator>default</jmh.generator>

        <!--
            Name of the benchmark Uber-JAR to generate.
          -->
        <uberjar.name>benchmarks</uberjar.name>
    </properties>

    <build>
        <plugins>
            <!--
                1. Compile Kotlin sources first.
            -->

            <plugin>
                <artifactId>kotlin-maven-plugin</artifactId>
                <groupId>org.jetbrains.kotlin</groupId>
                <version>${kotlin.version}</version>

                <executions>
                    <execution>
                        <id>process-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                            </sourceDirs>
                        </configuration>
                    </execution>

                    <execution>
                        <id>process-test-sources</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!--
                2. Invoke JMH generators to produce benchmark code
            -->

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>${plugin.exec.version}</version>
                <executions>
                    <execution>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <includePluginDependencies>true</includePluginDependencies>
                            <mainClass>org.openjdk.jmh.generators.bytecode.JmhBytecodeGenerator</mainClass>
                            <arguments>
                                <argument>${project.basedir}/target/classes/</argument>
                                <argument>${project.basedir}/target/generated-sources/jmh/</argument>
                                <argument>${project.basedir}/target/classes/</argument>
                                <argument>${jmh.generator}</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.openjdk.jmh</groupId>
                        <artifactId>jmh-generator-bytecode</artifactId>
                        <version>${jmh.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

            <!--
                3. Add JMH generated code to the compile session.
            -->

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>${plugin.build-helper.version}</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.basedir}/target/generated-sources/jmh</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!--
                4. Compile JMH generated code.
             -->

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${plugin.compiler.version}</version>
                <configuration>
                    <compilerVersion>${kotlin.compiler.jvmTarget}</compilerVersion>
                    <source>${kotlin.compiler.jvmTarget}</source>
                    <target>${kotlin.compiler.jvmTarget}</target>
                    <compilerArgument>-proc:none</compilerArgument>
                </configuration>
            </plugin>

            <!--
                5. Package all the dependencies into the JAR
             -->

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>${plugin.shade.version}</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <finalName>${uberjar.name}</finalName>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>org.openjdk.jmh.Main</mainClass>
                                </transformer>
                            </transformers>
                            <filters>
                                <filter>
                                    <!--
                                        Shading signed JARs will fail without this.
                                        http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
                                    -->
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>
