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

    Copyright (C) 2015 Red Hat, Inc.

    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>io.fabric8</groupId>
    <artifactId>kubernetes-model-generator</artifactId>
    <version>7.9.0</version>
  </parent>

  <artifactId>kubernetes-model-common</artifactId>
  <packaging>bundle</packaging>
  <name>Fabric8 :: Kubernetes Model :: Common</name>

  <dependencies>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
    </dependency>
    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>${maven.bundle.plugin.version}</version>
        <extensions>true</extensions>
        <configuration>
          <instructions>
            <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
            <Import-Package>*</Import-Package>
            <Export-Package>
              io.fabric8.kubernetes.model**,
              io.fabric8.kubernetes.api.builder**
            </Export-Package>
          </instructions>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <!-- #5622 — regeneration of the io.fabric8.kubernetes.api.builder runtime.

         This module is the only one whose @Buildable sets generateBuilderPackage = true, which is what makes sundrio
         emit that runtime (BaseFluent, Visitable, VisitableBuilder, ...). sundrio keeps one BuilderContext per javac
         run and throws IllegalStateException if two @Buildable types in a module disagree on the flag, so the trigger
         has to sit alone in a module: every other @Buildable in the repository sets it to false.

         The trigger (src/generate/java/.../util/Dummy.java) is therefore a build-time artefact, not API. It is
         compiled only here, under -Pgenerate; the default build compiles the committed runtime in
         src/generated-builders/java as plain source and never sees Dummy. -->
    <profile>
      <id>generate</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
              <execution>
                <id>add-builder-package-trigger-source</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>add-source</goal>
                </goals>
                <configuration>
                  <sources>
                    <source>${basedir}/src/generate/java</source>
                  </sources>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <id>remove-builder-package-trigger-output</id>
                <!-- `compile`, not a later phase: same-phase executions run after the ones the packaging's default
                     lifecycle injects, so this fires right after default-compile (and after APT with it). Binding it
                     any later would leave `mvn -Pgenerate compile` - and IDE builds delegated to Maven, which stop at
                     that goal - with DummyBuilder/DummyFluent stranded in the committed tree, breaking every
                     subsequent default build with `cannot find symbol: class Dummy`, which `mvn clean` cannot undo
                     (clean only removes target/). An IDE compiling with its own javac runs no Maven plugin at all, so
                     recovery there is still `git clean` on this directory.

                     DO NOT move this to a later phase. jandex:jandex is bound to process-classes and indexes
                     ${project.build.outputDirectory}; because this antrun comes from a profile it merges last among
                     same-phase executions, so at process-classes jandex would index Dummy/DummyBuilder/DummyFluent
                     *before* this deletes them, and `make generate-model` would install a jar whose META-INF/jandex.idx
                     names three classes the jar does not contain (measured: 3 entries vs 0 class files). At compile,
                     default-compile still wins (lifecycle-injected executions merge ahead of POM-declared ones) and
                     jandex runs afterwards, so the index matches the default build's byte for byte.

                     (The `package` binding this replaces fired after maven-bundle-plugin had already assembled the
                     jar, which is why Dummy*.class shipped in every release since a3efaa3d77.) -->
                <phase>compile</phase>
                <configuration>
                  <target>
                    <echo>Removing the builder package trigger and its generated Builder/Fluent</echo>
                    <!-- Mandatory, not cosmetic: these two reference Dummy, which the default build does not
                         compile, so committing them would break every subsequent build of this module. -->
                    <delete verbose="true" includeemptydirs="true">
                      <!-- Matched as a subtree, not as the fileset's base dir, so includeemptydirs also removes the
                           emptied package directories. Everything sundrio can emit under io/fabric8/kubernetes/model
                           here is the trigger's own output, since Dummy is the module's only @Buildable; the
                           committed runtime lives under io/fabric8/kubernetes/api/builder and is untouched. -->
                      <fileset dir="${sundrio.generatedBuildersDirectory}" erroronmissingdir="false">
                        <include name="io/fabric8/kubernetes/model/**" />
                      </fileset>
                      <!-- Files only: this directory also holds Helper.class and must survive. -->
                      <fileset dir="${project.build.outputDirectory}/io/fabric8/kubernetes/model/util"
                            erroronmissingdir="false">
                        <include name="Dummy*.class" />
                      </fileset>
                    </delete>
                  </target>
                </configuration>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>
