<?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.nest</groupId>
    <artifactId>hawkular-nest-parent</artifactId>
    <version>0.3.3.Final</version>
  </parent>

  <artifactId>hawkular-nest-distro</artifactId>
  <packaging>pom</packaging>
  <name>Hawkular Nest: Nest Distribution</name>
  <inceptionYear>2015</inceptionYear>

  <properties>
    <nest.build.type>production</nest.build.type>
    <wildfly.dist.zip.root.dir>wildfly-${version.org.wildfly}</wildfly.dist.zip.root.dir>
    <assembly.dir>${project.build.directory}/assembly</assembly.dir>
    <checkstyle.suppressions.file>${basedir}/src/main/scripts/checkstyle-suppressions.xml</checkstyle.suppressions.file>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.hawkular.nest</groupId>
      <artifactId>hawkular-nest-wf-extension</artifactId>
      <version>${project.version}</version>
      <classifier>module</classifier>
      <type>zip</type>
    </dependency>

    <dependency>
      <groupId>org.hawkular.bus</groupId>
      <artifactId>hawkular-bus-broker-wf-extension</artifactId>
      <classifier>module</classifier>
      <type>zip</type>
    </dependency>

    <dependency>
      <groupId>org.hawkular.bus</groupId>
      <artifactId>hawkular-bus-ra-wf-module</artifactId>
      <classifier>module</classifier>
      <type>zip</type>
    </dependency>

    <dependency>
      <groupId>org.wildfly</groupId>
      <artifactId>wildfly-dist</artifactId>
      <type>zip</type>
    </dependency>
  </dependencies>

  <build>

    <plugins>
      <plugin>
        <groupId>com.mycila</groupId>
        <artifactId>license-maven-plugin</artifactId>
        <configuration>
          <excludes combine.children="append">
            <exclude>src/main/scripts/subsystem*.xml</exclude>
            <exclude>src/main/scripts/socketbinding*.xml</exclude>
          </excludes>
        </configuration>
      </plugin>

      <!-- purge any previous nest distro dependency we extracted, ensures a fresh standalone.xml -->
      <!-- THIS SCREWS US UP WHEN USING RELEASE PLUGIN
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <id>clean-old-nest</id>
            <phase>initialize</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <target>
                <delete dir="${assembly.dir}" />
                <delete dir="${project.build.directory}/dependency-maven-plugin-markers" />
              </target>
            </configuration>
          </execution>
        </executions>
      </plugin>
      -->

      <!-- unpack the wildfly distribution and copy our hawkular modules in a tmp location -->
      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>unpack-wildfly</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${assembly.dir}</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>
              <overWriteIfNewer>true</overWriteIfNewer>
              <excludeTransitive>true</excludeTransitive>
            </configuration>
          </execution>
          <execution>
            <id>unpack-ra-module</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${assembly.dir}/${wildfly.dist.zip.root.dir}/modules/system/layers/base</outputDirectory>
              <includeArtifactIds>
                hawkular-bus-ra-wf-module
              </includeArtifactIds>
              <excludeTransitive>true</excludeTransitive>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- put some custom files in the out-of-box wildfly we unpacked above -->
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-distro-resources-to-wildfly</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${assembly.dir}/${wildfly.dist.zip.root.dir}</outputDirectory>
              <resources>
                <resource>
                  <directory>src/main/distro-resources</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>
            </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>${assembly.dir}/${wildfly.dist.zip.root.dir}</jbossHome>
              <serverConfig>standalone/configuration/standalone.xml</serverConfig>
              <modulesHome>modules/system/layers/base</modulesHome>
              <moduleZip>${project.build.directory}/hawkular-modules/hawkular-bus-broker-wf-extension.zip</moduleZip>
              <subsystem>${basedir}/src/main/scripts/subsystem-hawkular-bus-broker-wf-extension.xml</subsystem>
              <socketBinding>${basedir}/src/main/scripts/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>${assembly.dir}/${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/system/layers/base</modulesHome>
              <moduleZip>${project.build.directory}/hawkular-modules/hawkular-nest-wf-extension.zip</moduleZip>
              <subsystem>${basedir}/src/main/scripts/subsystem-hawkular-nest-wf-extension.xml</subsystem>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- further customize standalone.xml -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>xml-maven-plugin</artifactId>
        <version>1.0</version> <!-- TODO delete once we point to latest hawkular parent -->
        <executions>
          <execution>
            <id>transform-standalone-xml</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>transform</goal>
            </goals>
            <configuration>
              <transformationSets>
                <transformationSet>
                  <dir>${assembly.dir}/${wildfly.dist.zip.root.dir}/standalone/configuration</dir>
                  <stylesheet>src/main/scripts/standalone.xsl</stylesheet>
                  <includes>
                    <include>standalone.xml</include>
                  </includes>
                  <outputDir>${assembly.dir}/${wildfly.dist.zip.root.dir}/standalone/configuration</outputDir>
                  <parameters>
                    <parameter>
                      <name>nest.build.type</name>
                      <value>${nest.build.type}</value>
                    </parameter>
                  </parameters>
                </transformationSet>
              </transformationSets>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptors>
            <descriptor>src/main/scripts/distro-assembly.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <id>distro-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>

  <profiles>
    <!-- unzips the assembly so devs can run it right out of target/ -->
    <profile>
      <id>dev</id>
      <properties>
        <nest.build.type>dev</nest.build.type>
      </properties>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <id>unzip-distro</id>
                <phase>install</phase>
                <configuration>
                  <target>
                    <unzip src="${project.build.directory}/${project.build.finalName}-distribution.zip" dest="${project.build.directory}" />
                    <chmod dir="${project.build.directory}/${wildfly.dist.zip.root.dir}/bin" includes="*.sh" perm="u+x" type="file" />
                  </target>
                </configuration>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
