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

    <parent>
        <groupId>io.last9</groupId>
        <artifactId>vertx-otel-autoconfigure</artifactId>
    <version>2.4.0</version>
    </parent>

    <artifactId>vertx4-otel-agent</artifactId>
    <packaging>jar</packaging>

    <name>Vert.x 4 OpenTelemetry Agent</name>
    <description>
        Standalone Java agent for zero-code OpenTelemetry tracing in Vert.x 4 applications.
        Uses classloader isolation to prevent classpath conflicts.
        Usage: java -javaagent:vertx4-otel-agent.jar -jar app.jar
    </description>

    <dependencies>
        <!-- Library module — provided scope: ensures build order without bundling.
             The shaded fat JAR is embedded in inst/ via antrun below. -->
        <dependency>
            <groupId>io.last9</groupId>
            <artifactId>vertx4-rxjava3-otel-autoconfigure</artifactId>
            <version>${project.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- OTel API + Context — provided so maven-dependency-plugin:copy can resolve them.
             These are NOT bundled into the agent JAR (they're embedded as bootstrap JARs in inst/).
             Bootstrap injection ensures our 1.38.0 shadows any older version the app carries
             (e.g. opentelemetry-api:1.18.0 from vertx-opentelemetry:4.5.x). -->
        <dependency>
            <groupId>io.opentelemetry</groupId>
            <artifactId>opentelemetry-context</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.opentelemetry</groupId>
            <artifactId>opentelemetry-api</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Compile agent bootstrap with Java 8 target so the JVM can load
                 AgentBootstrap.class on Java 8 and gracefully bail out before
                 touching any Java 11-compiled classes in agent-impl.jar. -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <release>8</release>
                </configuration>
            </plugin>

            <!-- Copy OTel API + Context JARs for bootstrap classloader injection.
                 These are embedded in inst/ and injected via appendToBootstrapClassLoaderSearch
                 at premain time, ensuring our 1.38.0 shadows any older version the app carries. -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.6.1</version>
                <executions>
                    <execution>
                        <id>copy-bootstrap-jars</id>
                        <phase>prepare-package</phase>
                        <goals><goal>copy</goal></goals>
                        <configuration>
                            <artifactItems>
                                <!-- Context MUST be listed before API — API classes import
                                     io.opentelemetry.context.* so Context must be on the
                                     bootstrap classpath first. -->
                                <artifactItem>
                                    <groupId>io.opentelemetry</groupId>
                                    <artifactId>opentelemetry-context</artifactId>
                                    <version>${opentelemetry.version}</version>
                                    <outputDirectory>${project.build.outputDirectory}/inst</outputDirectory>
                                    <destFileName>bootstrap-opentelemetry-context.jar</destFileName>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>io.opentelemetry</groupId>
                                    <artifactId>opentelemetry-api</artifactId>
                                    <version>${opentelemetry.version}</version>
                                    <outputDirectory>${project.build.outputDirectory}/inst</outputDirectory>
                                    <destFileName>bootstrap-opentelemetry-api.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Embed the library fat JAR inside inst/ directory.
                 This JAR is loaded by AgentClassLoader in an isolated classloader,
                 so its classes never appear on the system classloader. -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>embed-library-jar</id>
                        <phase>prepare-package</phase>
                        <goals><goal>run</goal></goals>
                        <configuration>
                            <target>
                                <mkdir dir="${project.build.outputDirectory}/inst"/>
                                <copy file="${project.basedir}/../vertx4-rxjava3-otel-autoconfigure/target/vertx4-rxjava3-otel-autoconfigure-${project.version}.jar"
                                      tofile="${project.build.outputDirectory}/inst/agent-impl.jar"/>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Package with agent manifest entries.
                 Only AgentBootstrap + AgentClassLoader are at standard class locations.
                 Everything else is inside inst/agent-impl.jar. -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Premain-Class>io.last9.tracing.otel.agent.AgentBootstrap</Premain-Class>
                            <Agent-Class>io.last9.tracing.otel.agent.AgentBootstrap</Agent-Class>
                            <Can-Redefine-Classes>true</Can-Redefine-Classes>
                            <Can-Retransform-Classes>true</Can-Retransform-Classes>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

            <!-- Source JAR for releases -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
            </plugin>

            <!-- Skip surefire — no tests in agent module -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
