<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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.ipaas.apps</groupId>
    <artifactId>fabric8-ipaas</artifactId>
    <version>2.2.168</version>
  </parent>
  <artifactId>apiman-gateway</artifactId>
  <name>Apiman Gateway</name>
  <description>Apiman Gateway deployed on Jetty</description>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <version.compiler.plugin>2.3.1</version.compiler.plugin>
    <version.maven-surefire-plugin>2.15</version.maven-surefire-plugin>
    <version.maven-bundle-plugin>2.3.7</version.maven-bundle-plugin>

    <http.port>7777</http.port>
    <docker.image>${fabric8.dockerUser}${project.artifactId}:${project.version}</docker.image>
    <docker.env.MAIN>io.fabric8.apiman.gateway.ApimanGatewayStarter</docker.env.MAIN>
    <docker.env.apiman-gateway.ssl>false</docker.env.apiman-gateway.ssl>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>io.apiman</groupId>
        <artifactId>apiman</artifactId>
        <version>${apiman.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!-- aligning with apiman -->
      <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.4</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>io.apiman</groupId>
      <artifactId>apiman-gateway-platforms-war-micro</artifactId>
      <type>jar</type>
      <version>${apiman.version}</version>
    </dependency>

    <!-- fabric8 -->
    <dependency>
      <groupId>io.fabric8</groupId>
      <artifactId>kubernetes-api</artifactId>
    </dependency>
    <dependency>
      <groupId>io.fabric8</groupId>
      <artifactId>kubernetes-client</artifactId>
    </dependency>

    <!-- overriding versions from dependencyManagement -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
    </dependency>
    <dependency>
      <artifactId>slf4j-log4j12</artifactId>
      <groupId>org.slf4j</groupId>
    </dependency>

  </dependencies>

  <build>
    <finalName>${project.artifactId}</finalName>

    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering>
      </resource>
      <resource>
        <directory>src/main/resources-filtered</directory>
        <filtering>true</filtering>
      </resource>
    </resources>

    <plugins>
      <plugin>
        <groupId>io.fabric8</groupId>
        <artifactId>fabric8-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>fmp</id>
            <goals>
              <goal>resource</goal>
              <goal>helm</goal>
              <goal>build</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <images>
            <image>
              <name>${docker.image}</name>
              <build>
                <from>${docker.from}</from>
                <assembly>
                  <basedir>/deployments</basedir>
                  <descriptorRef>hawt-app</descriptorRef>
                </assembly>
                <env>
                  <JAVA_LIB_DIR>/deployments/lib</JAVA_LIB_DIR>
                  <JAVA_MAIN_CLASS>${docker.env.MAIN}</JAVA_MAIN_CLASS>
                  <HTTP_PORT>${http.port}</HTTP_PORT>
                  <APIMAN_GATEWAY_SSL>${docker.env.apiman-gateway.ssl}</APIMAN_GATEWAY_SSL>
                </env>
                <ports>
                  <port>8778</port>
                  <port>${http.port}</port>
                </ports>
              </build>
            </image>
          </images>
        </configuration>
      </plugin>

      <plugin>
        <groupId>io.fabric8</groupId>
        <artifactId>hawt-app-maven-plugin</artifactId>
        <version>${fabric8.version}</version>
        <executions>
          <execution>
            <id>hawt-app</id>
            <goals>
              <goal>build</goal>
            </goals>
          </execution>
        </executions>
      </plugin>


      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.4.0</version>
        <configuration>
          <mainClass>${docker.env.MAIN}</mainClass>
          <includePluginDependencies>false</includePluginDependencies>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <!-- activate the ssl profile to start apiman with a selfsigned certificate -->
    <profile>
      <id>ssl</id>
      <properties>
        <docker.env.apiman-gateway.ssl>true</docker.env.apiman-gateway.ssl>
        <keystore.password>supersecret</keystore.password>
        <client.keystore.password>supersecret</client.keystore.password>
        <truststore.password>supersecret</truststore.password>
        <fabric8.volume.keystore.secret>apiman-gateway-keystore</fabric8.volume.keystore.secret>
        <fabric8.volume.keystore.mountPath>/secret/apiman-gateway</fabric8.volume.keystore.mountPath>
        <fabric8.annotations.service.api.service.kubernetes.io.scheme>https
        </fabric8.annotations.service.api.service.kubernetes.io.scheme>
        <fabric8.readinessProbe.httpGet.scheme>HTTPS</fabric8.readinessProbe.httpGet.scheme>
        <fabric8.extra.json>${project.build.directory}/classes/route-tls-passthrough.json</fabric8.extra.json>
      </properties>
      <build>
        <plugins>

          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>keytool-maven-plugin</artifactId>
            <version>1.5</version>
            <executions>
              <execution>
                <goals>
                  <goal>generateKeyPair</goal>
                </goals>
                <phase>generate-sources</phase>
              </execution>
              <execution>
                <id>export-public-key</id>
                <goals>
                  <goal>exportCertificate</goal>
                </goals>
                <phase>generate-resources</phase>
              </execution>
            </executions>
            <configuration>
              <keystore>${project.build.directory}/secret/${project.artifactId}/keystore</keystore>
              <storepass>${keystore.password}</storepass>
              <keypass>${keystore.password}</keypass>
              <alias>SSLforAPIMAN-GATEWAY</alias>
              <dname>CN=${project.artifactId}.default.svc.cluster.local, OU=Example, O=Fabric8 Inc.,
                L=Ipswich, S=Massachusetts, C=US
              </dname>
              <sigalg>SHA1withRSA</sigalg>
              <ext/>
              <validity>100</validity>
              <keyalg>RSA</keyalg>
              <keysize>2048</keysize>
              <rfc>true</rfc>
              <file>${project.build.directory}/secret/${project.artifactId}/public.key</file>
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <executions>
              <execution>
                <id>copy-resources</id>
                <phase>validate</phase>
                <goals>
                  <goal>copy-resources</goal>
                </goals>
                <configuration>
                  <outputDirectory>${basedir}/target/secret/${project.artifactId}</outputDirectory>
                  <resources>
                    <resource>
                      <directory>${basedir}/ssl</directory>
                      <filtering>true</filtering>
                    </resource>
                  </resources>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <artifactId>exec-maven-plugin</artifactId>
            <groupId>org.codehaus.mojo</groupId>
            <executions>
              <execution>
                <id>Deploy Secret</id>
                <phase>package</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
                <configuration>
                  <executable>${basedir}/ssl.sh</executable>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <!--
  Default sample commands:

  mvn -Pf8-build         ... build Docker images and create OpenShift deployment configs
  mvn -Pf8-deploy        ... f8-build + push to Docker registry + applying deployment config to OpenShift
  mvn -Pf8-local-deploy ... deployment for a single node setup without pushing to a registry

  For individual goal usage please consult the fabric8 documentation
  -->
    <profile>
      <id>f8-build</id>
      <build>
        <defaultGoal>clean install docker:build fabric8:json</defaultGoal>
      </build>
    </profile>
    <profile>
      <id>f8-deploy</id>
      <properties>
        <fabric8.imagePullPolicySnapshot>Always</fabric8.imagePullPolicySnapshot>
        <fabric8.recreate>true</fabric8.recreate>
      </properties>
      <build>
        <defaultGoal>clean install docker:build docker:push fabric8:json fabric8:apply</defaultGoal>
      </build>
    </profile>
    <profile>
      <id>f8-local-deploy</id>
      <properties>
        <fabric8.recreate>true</fabric8.recreate>
      </properties>
      <build>
        <defaultGoal>clean install docker:build fabric8:json fabric8:apply</defaultGoal>
      </build>
    </profile>
  </profiles>

</project>