<?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>

    <groupId>ai.philterd</groupId>
    <artifactId>phisql</artifactId>
    <!--
        This version is the implementation version and is independent of the schema version
        (redaction.policy.schema.version below). Increment this freely for code-only changes.
        See the compatibility matrix in README.md for the schema <-> jar version mapping.
    -->
    <version>1.2.0</version>
    <packaging>jar</packaging>

    <name>PhiSQL Reference Implementation</name>
    <description>
        Reference parser and AST library for the PhiSQL specification.
        The parser is generated from spec/v1.0/grammar/PhiSQL.g4 at build time.
        The grammar file is the source of truth; any drift causes the build to fail.
    </description>
    <url>https://github.com/philterd/phisql</url>
    <inceptionYear>2022</inceptionYear>

    <organization>
        <name>Philterd, LLC</name>
        <url>https://www.philterd.ai</url>
    </organization>

    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <developers>
        <developer>
            <name>Philterd, LLC</name>
            <email>support@philterd.ai</email>
            <organization>Philterd</organization>
            <organizationUrl>https://www.philterd.ai</organizationUrl>
        </developer>
    </developers>

    <scm>
        <connection>scm:git@github.com:philterd/phisql.git</connection>
        <developerConnection>scm:git@github.com:philterd/phisql.git</developerConnection>
        <url>https://github.com/philterd/phisql/</url>
    </scm>

    <distributionManagement>
        <snapshotRepository>
            <id>central</id>
            <url>https://central.sonatype.com/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

    <properties>
        <maven.compiler.release>17</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <antlr.version>4.13.2</antlr.version>
        <jackson.version>2.17.2</jackson.version>
        <junit.version>5.10.2</junit.version>
        <!--
            Version of the canonical redaction policy schema bundled into this
            JAR. The schema is authored at ../../schema/<version>/schema.json; this
            property selects which version is packaged and exposed at runtime via
            ai.philterd.phisql.PolicySchema.
        -->
        <redaction.policy.schema.version>1.1.0</redaction.policy.schema.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4-runtime</artifactId>
            <version>${antlr.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <!--
            Filter src/main/resources so policy-schema.properties picks up the
            redaction.policy.schema.version property at build time. That is how
            PolicySchema learns which schema version was bundled without the
            version being hardcoded in Java.
        -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>
            <!--
                Maven Central publishing. Releases are pushed through the Sonatype
                Central portal; the GPG signing required by Central lives in the
                `sign` profile below so local/CI builds don't need a key.
            -->
            <plugin>
                <groupId>org.sonatype.central</groupId>
                <artifactId>central-publishing-maven-plugin</artifactId>
                <version>0.9.0</version>
                <extensions>true</extensions>
                <configuration>
                    <publishingServerId>central</publishingServerId>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.4.0</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.12.0</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <additionalOptions>
                                <additionalOption>-Xdoclint:none</additionalOption>
                            </additionalOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.antlr</groupId>
                <artifactId>antlr4-maven-plugin</artifactId>
                <version>${antlr.version}</version>
                <executions>
                    <execution>
                        <id>antlr</id>
                        <goals>
                            <goal>antlr4</goal>
                        </goals>
                        <configuration>
                            <!--
                                Read the grammar directly from the spec. This is the
                                load-bearing assertion that the reference parser
                                cannot drift from the spec: there is only one
                                grammar file in the repo, and it lives in spec/.
                            -->
                            <sourceDirectory>${project.basedir}/../../spec/v1.0/grammar</sourceDirectory>
                            <outputDirectory>${project.build.directory}/generated-sources/antlr4/ai/philterd/phisql/grammar</outputDirectory>
                            <visitor>true</visitor>
                            <listener>true</listener>
                            <arguments>
                                <argument>-package</argument>
                                <argument>ai.philterd.phisql.grammar</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.2.5</version>
            </plugin>

            <plugin>
                <!--
                    Bundle the spec catalog YAML files as JAR resources so the
                    compiler can load them via the classloader at runtime. This
                    keeps the catalog as the single source of truth: there is
                    no copy of the catalog inside reference/, and the JAR carries
                    the same files the spec validator and conformance suite use.
                -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.3.1</version>
                <executions>
                    <execution>
                        <id>copy-spec-catalog</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.outputDirectory}/spec/v1.0/catalog</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.basedir}/../../spec/v1.0/catalog</directory>
                                    <includes>
                                        <include>*.yaml</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                    <!--
                        Bundle the canonical redaction policy schema as a JAR
                        resource so projects depending on phisql can read it via
                        ai.philterd.phisql.PolicySchema without checking out this
                        repo. The version is selected by the
                        redaction.policy.schema.version property and the resource
                        path mirrors the repo and published-URL layout
                        (schema/<version>/schema.json). The JSON is copied
                        verbatim (not filtered) to keep it byte-identical to the
                        source of truth.
                    -->
                    <execution>
                        <id>copy-policy-schema</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.outputDirectory}/schema/${redaction.policy.schema.version}</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.basedir}/../../schema/${redaction.policy.schema.version}</directory>
                                    <includes>
                                        <include>schema.json</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <!--
            Activated with -Pcli to build a self-contained, runnable jar for the
            command-line front end (ai.philterd.phisql.Cli). The conformance
            suite under ../compliance drives the reference implementation through
            this jar. It is attached with the "cli" classifier and is NOT the
            primary artifact, so the default build and Maven Central publishing
            are unaffected: `mvn -Pcli package` produces
            target/phisql-<version>-cli.jar alongside the normal library jar.
        -->
        <profile>
            <id>cli</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-shade-plugin</artifactId>
                        <version>3.5.3</version>
                        <executions>
                            <execution>
                                <id>cli-jar</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>shade</goal>
                                </goals>
                                <configuration>
                                    <shadedArtifactAttached>true</shadedArtifactAttached>
                                    <shadedClassifierName>cli</shadedClassifierName>
                                    <!-- The CLI jar is a build-time convenience, not a
                                         published artifact, so do not rewrite the POM. -->
                                    <createDependencyReducedPom>false</createDependencyReducedPom>
                                    <transformers>
                                        <transformer
                                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                            <mainClass>ai.philterd.phisql.Cli</mainClass>
                                        </transformer>
                                    </transformers>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <!--
            Activated with -Dsign (used by the Central release build) to GPG-sign
            all artifacts, which Maven Central requires.
        -->
        <profile>
            <id>sign</id>
            <activation>
                <property>
                    <name>sign</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>3.2.7</version>
                        <executions>
                            <execution>
                                <id>sign-artifacts</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
