<?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</artifactId>
  <name>Apiman Manager</name>
  <description>Apiman 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>

    <maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
    <timestamp>${maven.build.timestamp}</timestamp>

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

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>io.apiman</groupId>
        <artifactId>apiman</artifactId>
        <version>${apiman.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>io.apiman</groupId>
      <artifactId>apiman-manager-api-micro</artifactId>
      <type>jar</type>
      <version>${apiman.version}</version>
    </dependency>
    <dependency>
      <groupId>io.apiman</groupId>
      <artifactId>apiman-distro-data</artifactId>
    </dependency>

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

    <!-- Override versions -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    <dependency>
      <groupId>com.squareup.okhttp</groupId>
      <artifactId>okhttp</artifactId>
      <version>2.7.2</version>
    </dependency>

    <!-- testing -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </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_SSL>${docker.env.apiman.ssl}</APIMAN_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>
    <pluginManagement>
      <plugins>
        <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
        <plugin>
          <groupId>org.eclipse.m2e</groupId>
          <artifactId>lifecycle-mapping</artifactId>
          <version>1.0.0</version>
          <configuration>
            <lifecycleMappingMetadata>
              <pluginExecutions>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>keytool-maven-plugin</artifactId>
                    <versionRange>[1.5,)</versionRange>
                    <goals>
                      <goal>generateKeyPair</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <ignore/>
                  </action>
                </pluginExecution>
              </pluginExecutions>
            </lifecycleMappingMetadata>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <profiles>
    <!-- activate the ssl profile to start apiman with a selfsigned certificate -->
    <profile>
      <id>ssl</id>
      <properties>
        <docker.env.apiman.ssl>true</docker.env.apiman.ssl>
        <keystore.password>supersecret</keystore.password>
        <client.keystore.password>supersecret</client.keystore.password>
        <truststore.password>supersecret</truststore.password>
        <fabric8.volume.keystore.secret>apiman-keystore</fabric8.volume.keystore.secret>
        <fabric8.volume.keystore.mountPath>/secret/apiman</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>
            </executions>
            <configuration>
              <keystore>${project.build.directory}/secret/apiman/keystore</keystore>
              <storepass>${keystore.password}</storepass>
              <keypass>${keystore.password}</keypass>
              <alias>SSLforAPIMAN</alias>
              <dname>CN=apiman.${KUBERNETES_DOMAIN}, 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>
            </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/apiman</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>
