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

    Copyright 2015 Red Hat, Inc. and/or its affiliates
    and other contributors as indicated by the @author tags.

    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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.hawkular</groupId>
    <artifactId>hawkular</artifactId>
    <version>1.0.0.Alpha2</version>
  </parent>

  <artifactId>hawkular-dist</artifactId>
  <packaging>pom</packaging>
  <name>Hawkular: Distribution</name>
  <description>Hawkular distribution that integrates all components into a single server.</description>

  <properties>
    <kettle.build.type>production</kettle.build.type>
    <checkstyle.suppressions.file>checkstyle-suppressions.xml</checkstyle.suppressions.file>
    <assembly.dir>${project.build.directory}/assembly</assembly.dir>
    <wildfly.dist.zip.root.dir>wildfly-${version.org.wildfly}</wildfly.dist.zip.root.dir>
    <hawkular.dist.zip.root.dir>hawkular-${project.version}</hawkular.dist.zip.root.dir>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.hawkular.agent</groupId>
      <artifactId>hawkular-monitor</artifactId>
      <classifier>module</classifier>
      <type>zip</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.bus</groupId>
      <artifactId>hawkular-bus-broker-wf-extension</artifactId>
      <version>${version.org.hawkular.bus}</version>
      <classifier>module</classifier>
      <type>zip</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.bus</groupId>
      <artifactId>hawkular-bus-ra-wf-module</artifactId>
      <version>${version.org.hawkular.bus}</version>
      <classifier>module</classifier>
      <type>zip</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.nest</groupId>
      <artifactId>hawkular-nest-wf-extension</artifactId>
      <version>${version.org.hawkular.bus}</version>
      <classifier>module</classifier>
      <type>zip</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.inventory</groupId>
      <artifactId>hawkular-integrated-inventory-rest</artifactId>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular</groupId>
      <artifactId>hawkular-pinger</artifactId>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular</groupId>
      <artifactId>hawkular-avail-creator</artifactId>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.alerts</groupId>
      <artifactId>hawkular-alerts-ear</artifactId>
      <type>ear</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.alerts</groupId>
      <artifactId>hawkular-actions-email</artifactId>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.metrics</groupId>
      <artifactId>hawkular-metrics-api-jaxrs</artifactId>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.commons</groupId>
      <artifactId>hawkular-commons-embedded-cassandra-ear</artifactId>
      <type>ear</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular</groupId>
      <artifactId>hawkular-console</artifactId>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.accounts</groupId>
      <artifactId>hawkular-accounts</artifactId>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>org.wildfly</groupId>
      <artifactId>wildfly-dist</artifactId>
      <type>zip</type>
      <exclusions>
        <exclusion>
          <groupId>jaxen</groupId>
          <artifactId>jaxen</artifactId>
        </exclusion>
        <exclusion>
          <groupId>javax.transaction</groupId>
          <artifactId>jta</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <!-- Keycloak-related dependencies -->
    <dependency>
      <groupId>org.keycloak</groupId>
      <artifactId>keycloak-wf9-modules</artifactId>
      <version>${version.org.keycloak}</version>
      <type>zip</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.accounts</groupId>
      <artifactId>hawkular-accounts-keycloak-theme</artifactId>
      <version>${version.org.hawkular.accounts}</version>
      <type>jar</type>
    </dependency>
  </dependencies>

  <build>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>unpack</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>unpack</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>org.keycloak</groupId>
                  <artifactId>keycloak-server-overlay</artifactId>
                  <type>zip</type>
                  <version>${version.org.keycloak}</version>
                  <outputDirectory>${project.build.directory}/keycloak</outputDirectory>
                </artifactItem>
                <artifactItem>
                  <groupId>org.keycloak</groupId>
                  <artifactId>keycloak-wf9-modules</artifactId>
                  <type>zip</type>
                  <outputDirectory>${project.build.directory}/keycloak/modules/system/layers/base</outputDirectory>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
          <execution>
            <id>unpack-wildfly</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/wildfly</outputDirectory>
              <includeArtifactIds>wildfly-dist</includeArtifactIds>
              <excludes>*/appclient/**,*/docs/**,*/domain/**</excludes>
            </configuration>
          </execution>
          <execution>
            <id>copy-hawkular-modules</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/hawkular-modules</outputDirectory>
              <includeArtifactIds>
                hawkular-nest-wf-extension,
                hawkular-bus-broker-wf-extension,
              </includeArtifactIds>
              <stripVersion>true</stripVersion>
              <stripClassifier>true</stripClassifier>
              <overWriteIfNew>true</overWriteIfNew>
              <excludeTransitive>true</excludeTransitive>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- install our hawkular extension modules and customize standalone.xml -->
      <plugin>
        <groupId>org.wildfly.plugins</groupId>
        <artifactId>wildfly-extension-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>install-bus-broker-module</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <jbossHome>${project.build.directory}/wildfly/${wildfly.dist.zip.root.dir}</jbossHome>
              <serverConfig>standalone/configuration/standalone.xml</serverConfig>
              <!-- we do not care about this backup, so put it somewhere so it won't overwrite first backup -->
              <serverConfigBackup>${project.build.directory}/standalone.xml.out</serverConfigBackup>
              <modulesHome>modules</modulesHome>
              <moduleZip>${project.build.directory}/hawkular-modules/hawkular-bus-broker-wf-extension.zip</moduleZip>
              <subsystem>${basedir}/src/main/resources/wildfly/snippets/subsystem-hawkular-bus-broker-wf-extension.xml</subsystem>
              <socketBinding>${basedir}/src/main/resources/wildfly/snippets/socketbinding-hawkular-bus-broker-wf-extension.xml</socketBinding>
              <edit>
                <insert>
                  <select>/server/socket-binding-group[@name='standard-sockets']</select>
                  <xml><![CDATA[<socket-binding name="org.hawkular.bus.broker.discovery" port="0" multicast-address="224.0.0.4" multicast-port="63636"/>]]></xml>
                  <attribute>name</attribute>
                </insert>
              </edit>
            </configuration>
          </execution>
          <execution>
            <id>install-nest-module</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <jbossHome>${project.build.directory}/wildfly/${wildfly.dist.zip.root.dir}</jbossHome>
              <serverConfig>standalone/configuration/standalone.xml</serverConfig>
              <!-- we do not care about this backup, so put it somewhere so it won't overwrite first backup -->
              <serverConfigBackup>${project.build.directory}/standalone.xml.out</serverConfigBackup>
              <modulesHome>modules</modulesHome>
              <moduleZip>${project.build.directory}/hawkular-modules/hawkular-nest-wf-extension.zip</moduleZip>
              <subsystem>${basedir}/src/main/resources/wildfly/snippets/subsystem-hawkular-nest-wf-extension.xml</subsystem>
              <socketBinding>${basedir}/src/main/resources/wildfly/snippets/socketbinding-hawkular-bus-broker-wf-extension.xml</socketBinding>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- further customize standalone.xml -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>xml-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>transform-standalone-xml</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>transform</goal>
            </goals>
            <configuration>
              <transformationSets>
                <!-- standalone.xml -->
                <transformationSet>
                  <dir>${project.build.directory}/wildfly/${wildfly.dist.zip.root.dir}/standalone/configuration</dir>
                  <stylesheet>src/main/resources/wildfly/patches/standalone.xsl</stylesheet>
                  <includes>
                    <include>standalone.xml</include>
                  </includes>
                  <outputDir>${assembly.dir}/${hawkular.dist.zip.root.dir}/standalone/configuration</outputDir>
                  <parameters>
                    <parameter>
                      <name>kettle.build.type</name>
                      <value>${kettle.build.type}</value>
                    </parameter>
                    <parameter>
                      <name>uuid.hawkular.accounts.backend</name>
                      <value>${uuid.hawkular.accounts.backend}</value>
                    </parameter>
                  </parameters>
                </transformationSet>
                <!-- standalone-itest.xml -->
                <transformationSet>
                  <dir>${project.build.directory}/wildfly/${wildfly.dist.zip.root.dir}/standalone/configuration</dir>
                  <stylesheet>src/main/resources/wildfly/patches/standalone.xsl</stylesheet>
                  <fileMappers>
                    <fileMapper implementation="org.codehaus.plexus.components.io.filemappers.RegExpFileMapper">
                      <pattern>\.xml$</pattern>
                      <replacement>-itest.xml</replacement>
                    </fileMapper>
                  </fileMappers>
                  <includes>
                    <include>standalone.xml</include>
                  </includes>
                  <outputDir>${assembly.dir}/${hawkular.dist.zip.root.dir}/standalone/configuration</outputDir>
                  <parameters>
                    <parameter>
                      <name>kettle.build.type</name>
                      <value>dev</value>
                    </parameter>
                    <parameter>
                      <name>uuid.hawkular.accounts.backend</name>
                      <value>${uuid.hawkular.accounts.backend}</value>
                    </parameter>
                  </parameters>
                </transformationSet>
              </transformationSets>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <finalName>hawkular-${project.version}</finalName>
          <appendAssemblyId>false</appendAssemblyId>
          <descriptors>
            <descriptor>assembly.xml</descriptor>
          </descriptors>
          <formats>
            <format>zip</format>
            <format>tar.gz</format>
          </formats>
        </configuration>
        <executions>
          <execution>
            <id>distro-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <version>0.1.2</version>
        <configuration>
          <imageName>hawkular:snapshot</imageName>
          <dockerDirectory>docker</dockerDirectory>
          <resources>
            <resource>
              <targetPath>/</targetPath>
              <directory>${project.build.directory}</directory>
              <include>hawkular-${project.version}.zip</include>
            </resource>
          </resources>
        </configuration>
      </plugin>

      <!-- Generate some keys for Keycloak integration, so that we don't ship with default credentials -->
      <plugin>
        <groupId>org.codehaus.gmaven</groupId>
        <artifactId>gmaven-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <id>set-custom-property</id>
            <phase>initialize</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <source>
                import java.util.UUID
                def uuidBackend = UUID.randomUUID().toString()
                def uuidUi = UUID.randomUUID().toString()
                project.properties.setProperty('uuid.hawkular.accounts.backend', uuidBackend.toString())
                project.properties.setProperty('uuid.hawkular.ui', uuidUi.toString())
              </source>
            </configuration>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>

  <profiles>
    <profile>
      <id>dev</id>
      <properties>
        <kettle.build.type>dev</kettle.build.type>
      </properties>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
              <formats>
                <format>dir</format>
                <format>zip</format>
                <format>tar.gz</format>
              </formats>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
