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

  <parent>
    <groupId>org.hawkular.agent</groupId>
    <artifactId>hawkular-agent-itest-parent</artifactId>
    <version>0.10.0.Final</version>
  </parent>

  <artifactId>hawkular-agent-itest-dist</artifactId>
  <packaging>pom</packaging>

  <name>Hawkular Agent Distribution for Integration Tests</name>
  <description>An Agent installed on a stripped-down Hawkular for testing of Agent and Command Gateway</description>

  <properties>
    <kettle.build.type>production</kettle.build.type>
    <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>${project.artifactId}-${project.version}</hawkular.dist.zip.root.dir>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.hawkular.agent</groupId>
      <artifactId>hawkular-monitor</artifactId>
      <version>0.10.0.Final</version>
      <classifier>module</classifier>
      <type>zip</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.agent</groupId>
      <artifactId>hawkular-helloworld-war</artifactId>
      <version>0.10.0.Final</version>
      <type>war</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-inventory-dist</artifactId>
      <version>${version.org.hawkular.inventory}</version>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.cmdgw</groupId>
      <artifactId>hawkular-command-gateway-war</artifactId>
      <version>${version.org.hawkular.cmdgw}</version>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.metrics</groupId>
      <artifactId>hawkular-metrics-api-jaxrs</artifactId>
      <version>${version.org.hawkular.metrics}</version>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.commons</groupId>
      <artifactId>hawkular-commons-embedded-cassandra-ear</artifactId>
      <version>${version.org.hawkular.commons}</version>
      <type>ear</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.accounts</groupId>
      <artifactId>hawkular-accounts</artifactId>
      <version>${version.org.hawkular.accounts}</version>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.accounts</groupId>
      <artifactId>hawkular-accounts-events-backend</artifactId>
      <version>${version.org.hawkular.accounts}</version>
      <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>
    <dependency>
      <groupId>org.hawkular.accounts</groupId>
      <artifactId>hawkular-accounts-keycloak-event-listener-jms</artifactId>
      <version>${version.org.hawkular.accounts}</version>
      <classifier>module</classifier>
      <type>zip</type>
    </dependency>
    <dependency>
      <groupId>org.hawkular.accounts</groupId>
      <artifactId>hawkular-accounts-keycloak-event-listener-rest</artifactId>
      <version>${version.org.hawkular.accounts}</version>
      <classifier>module</classifier>
      <type>zip</type>
    </dependency>
  </dependencies>

  <profiles>
    <profile>
      <id>itest</id>
      <properties>
        <kettle.build.type>dev</kettle.build.type>
      </properties>
      <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>
                  <!-- In */domain, we remove everything except for /configuration/*.properties that are need because of
                    HAWKULAR-430 -->
                  <excludes>*/appclient/**,*/docs/**,*/domain/data/**,*/domain/tmp/**,*/domain/configuration/*.xml</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>${project.artifactId}-${project.version}</finalName>
              <appendAssemblyId>false</appendAssemblyId>
              <descriptors>
                <descriptor>assembly.xml</descriptor>
              </descriptors>
            </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.2.12</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.5</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>
    </profile>
  </profiles>
</project>
