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

    Copyright The OpenZipkin Authors
    SPDX-License-Identifier: Apache-2.0

-->
<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.zipkin</groupId>
    <artifactId>zipkin-parent</artifactId>
    <version>3.4.0</version>
  </parent>

  <artifactId>zipkin-lens</artifactId>
  <name>Zipkin Lens</name>
  <description>Repackages Zipkin Lens into a jar, so we can use it in zipkin-server</description>

  <properties>
    <main.basedir>${project.basedir}/..</main.basedir>
    <npm.skipTests>false</npm.skipTests>

    <!-- Update occasionally based on LTS, but don't overrun what's in Alpine:
         https://nodejs.org/en/download/
         https://pkgs.alpinelinux.org/packages?name=nodejs&branch=edge -->
    <node.version>20.12.2</node.version>
    <exec-maven-plugin.version>3.2.0</exec-maven-plugin.version>
    <frontend-maven-plugin.version>1.15.0</frontend-maven-plugin.version>
    <maven-clean-plugin.version>3.3.2</maven-clean-plugin.version>
    <maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>
  </properties>

  <dependencies>
    <!-- no dependencies as this is just javascript -->
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>${exec-maven-plugin.version}</version>
        </plugin>
        <plugin>
          <groupId>com.github.eirslett</groupId>
          <artifactId>frontend-maven-plugin</artifactId>
          <version>${frontend-maven-plugin.version}</version>
        </plugin>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>${maven-clean-plugin.version}</version>
        </plugin>
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>${maven-resources-plugin.version}</version>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <executions>
          <execution>
            <id>remove existing NPM build</id>
            <phase>compile</phase>
            <goals>
              <goal>clean</goal>
            </goals>
            <configuration>
              <excludeDefaultDirectories>true</excludeDefaultDirectories>
              <filesets>
                <fileset>
                  <directory>build</directory>
                </fileset>
              </filesets>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>com.github.eirslett</groupId>
        <artifactId>frontend-maven-plugin</artifactId>
        <configuration>
          <installDirectory>target</installDirectory>
          <nodeVersion>v${node.version}</nodeVersion>
          <environmentVariables>
            <!--
            create-react-app runs tests in watch mode unless this is defined. We define it here for running Maven locally.
            -->
            <CI>true</CI>
          </environmentVariables>
        </configuration>
        <executions>
          <execution>
            <id>install node and npm</id>
            <goals>
              <goal>install-node-and-npm</goal>
            </goals>
          </execution>
          <execution>
            <id>npm install</id>
            <goals>
              <goal>npm</goal>
            </goals>
            <configuration>
              <arguments>install</arguments>
            </configuration>
          </execution>
          <execution>
            <id>npm lint</id>
            <goals>
              <goal>npm</goal>
            </goals>
            <phase>compile</phase>
            <configuration>
              <arguments>run lint</arguments>
            </configuration>
          </execution>
          <execution>
            <id>npm run build</id>
            <goals>
              <goal>npm</goal>
            </goals>
            <phase>compile</phase>
            <configuration>
              <arguments>run build</arguments>
            </configuration>
          </execution>
          <execution>
            <id>npm run test</id>
            <goals>
              <goal>npm</goal>
            </goals>
            <phase>test</phase>
            <configuration>
              <skipTests>${npm.skipTests}</skipTests>
              <arguments>run test</arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>${maven-resources-plugin.version}</version>
        <executions>
          <execution>
            <id>copy NPM build to zipkin-lens directory</id>
            <!-- This needs to happen after compile, or it could copy an empty directory! -->
            <phase>prepare-package</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <!-- NPM build output will end up in the jar under the 'zipkin-lens' directory -->
              <outputDirectory>${project.build.directory}/classes/zipkin-lens</outputDirectory>
              <resources>
                <resource>
                  <directory>build</directory>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <!-- remove the META-INF directory, as this is just static assets -->
      <plugin>
        <artifactId>maven-shade-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <filters>
                <filter>
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>META-INF/</exclude>
                  </excludes>
                </filter>
              </filters>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <!-- Allows us to use a true value in maven-exec-plugin when skipTests exist only in name -->
    <profile>
      <id>normalize skipTests</id>
      <activation>
        <property>
          <name>skipTests</name>
        </property>
      </activation>
      <properties>
        <npm.skipTests>true</npm.skipTests>
      </properties>
    </profile>

    <!-- frontend-maven-plugin requires downloading via a public URL, and suggests exec-maven-plugin
         otherwise.

         ARM64 is not supported with musl, yet https://github.com/nodejs/node/blob/master/BUILDING.md
         See issue #3166

         There are problems on alpine+arm64 with posix_spawn. https://github.com/openzipkin/docker-java/issues/34
         So, always execute exporting MAVEN_OPTS=-Djdk.lang.Process.launchMechanism=vfork -->
    <profile>
      <id>exec-maven-plugin</id>
      <activation>
        <os>
          <arch>aarch64</arch>
        </os>
        <file>
          <exists>/etc/alpine-release</exists>
        </file>
      </activation>
      <build>
        <plugins>
          <!-- It isn't currently possible to disable frontend-maven-plugin.
               Instead, we set each execution to none -->
          <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <executions>
              <execution>
                <id>install node and npm</id>
                <phase>none</phase>
              </execution>
              <execution>
                <id>npm install</id>
                <phase>none</phase>
              </execution>
              <execution>
                <id>npm lint</id>
                <phase>none</phase>
              </execution>
              <execution>
                <id>npm run build</id>
                <phase>none</phase>
              </execution>
              <execution>
                <id>npm run test</id>
                <phase>none</phase>
              </execution>
            </executions>
          </plugin>
          <!-- This duplicates exactly what we did in frontend-maven-plugin -->
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <configuration>
              <environmentVariables>
                <!--
                create-react-app runs tests in watch mode unless this is defined. We define it here for running Maven locally.
                -->
                <CI>true</CI>
              </environmentVariables>
            </configuration>
            <executions>
              <execution>
                <id>npm install</id>
                <goals>
                  <goal>exec</goal>
                </goals>
                <phase>generate-resources</phase>
                <configuration>
                  <executable>npm</executable>
                  <arguments>
                    <argument>install</argument>
                  </arguments>
                </configuration>
              </execution>
              <execution>
                <id>npm run build</id>
                <goals>
                  <goal>exec</goal>
                </goals>
                <phase>compile</phase>
                <configuration>
                  <executable>npm</executable>
                  <arguments>
                    <argument>run</argument>
                    <argument>build</argument>
                  </arguments>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

    <profile>
      <id>release</id>
      <build>
        <plugins>
          <!-- Creates empty javadoc jar -->
          <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
              <execution>
                <id>empty-javadoc-jar</id>
                <phase>package</phase>
                <goals>
                  <goal>jar</goal>
                </goals>
                <configuration>
                  <classifier>javadoc</classifier>
                  <classesDirectory>${project.basedir}/javadoc</classesDirectory>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
