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

    Copyright 2015-2016 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.inventory</groupId>
    <artifactId>hawkular-inventory-parent</artifactId>
    <version>1.0.2.Final</version>
  </parent>

  <artifactId>hawkular-inventory-rest-api</artifactId>
  <packaging>war</packaging>

  <name>Hawkular Inventory REST API Web Application</name>

  <scm>
    <url>https://github.com/hawkular/hawkular-inventory</url>
    <connection>scm:git:https://github.com/hawkular/hawkular-inventory</connection>
    <developerConnection>scm:git:git@github.com:hawkular/hawkular-inventory.git</developerConnection>
    <tag>1.0.2.Final</tag>
  </scm>

  <dependencies>

    <dependency>
      <groupId>org.jboss.logging</groupId>
      <artifactId>jboss-logging</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.jboss.logging</groupId>
      <artifactId>jboss-logging-annotations</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.hawkular.commons</groupId>
      <artifactId>hawkular-inventory-paths</artifactId>
      <scope>provided</scope> <!-- there is a WildFly module -->
    </dependency>

    <dependency>
      <groupId>org.hawkular.commons</groupId>
      <artifactId>hawkular-cors-jaxrs-filter</artifactId>
    </dependency>

    <dependency>
      <groupId>org.hawkular.inventory</groupId>
      <artifactId>hawkular-inventory-cdi</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>org.hawkular.inventory</groupId>
      <artifactId>hawkular-inventory-security-spi</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>com.datastax.cassandra</groupId>
      <artifactId>cassandra-driver-core</artifactId>
      <scope>provided</scope> <!-- there is a WildFly module -->
    </dependency>

    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <scope>provided</scope><!-- there is a WF module -->
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <scope>provided</scope><!-- there is a WF module -->
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <scope>provided</scope><!-- there is a WF module -->
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId><!-- there is a WF module -->
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <scope>provided</scope><!-- there is a WF module -->
    </dependency>

    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <scope>provided</scope><!-- there is a WF module -->
    </dependency>

    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <scope>provided</scope><!-- there is a WF module -->
    </dependency>

    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <scope>provided</scope><!-- there is a WF module -->
    </dependency>

    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <scope>provided</scope><!-- there is a WF module -->
    </dependency>

    <dependency>
      <groupId>commons-pool</groupId>
      <artifactId>commons-pool</artifactId>
      <scope>provided</scope><!-- there is a WF module -->
    </dependency>

    <dependency>
      <groupId>javax.mail</groupId>
      <artifactId>javax.mail-api</artifactId>
      <scope>provided</scope><!-- there is a WF module -->
    </dependency>

    <dependency>
      <groupId>org.hawkular.inventory</groupId>
      <artifactId>hawkular-inventory-api</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>org.hawkular.inventory</groupId>
      <artifactId>hawkular-inventory-impl-tinkerpop</artifactId>
      <version>${project.version}</version>
      <exclusions>
        <exclusion>
          <groupId>commons-beanutils</groupId>
          <artifactId>commons-beanutils-core</artifactId><!-- provided by commons-beanutils since 1.9.x -->
        </exclusion>
        <exclusion>
          <groupId>org.codehaus.jettison</groupId>
          <artifactId>jettison</artifactId> <!-- there is a WF module -->
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>org.hawkular.inventory</groupId>
      <artifactId>hawkular-inventory-bus</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>org.hawkular.inventory</groupId>
      <artifactId>hawkular-inventory-json-helper</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>org.hawkular.commons</groupId>
      <artifactId>hawkular-rest-status</artifactId>
      <version>${version.org.hawkular.commons}</version>
    </dependency>

    <dependency>
      <groupId>org.hawkular.commons</groupId>
      <artifactId>hawkular-tenant-jaxrs-filter</artifactId>
      <version>${version.org.hawkular.commons}</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>

    <!-- Wildfly provided -->
    <dependency>
      <groupId>org.jboss.resteasy</groupId>
      <artifactId>resteasy-jaxrs</artifactId>
      <scope>provided</scope><!-- there is a WF module -->
    </dependency>

    <dependency>
      <groupId>org.jboss.spec.javax.websocket</groupId>
      <artifactId>jboss-websocket-api_1.1_spec</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.inject</groupId>
      <artifactId>javax.inject</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.enterprise</groupId>
      <artifactId>cdi-api</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.jboss.logging</groupId>
      <artifactId>jboss-logging-processor</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.jboss.spec.javax.transaction</groupId>
      <artifactId>jboss-transaction-api_1.2_spec</artifactId>
      <scope>provided</scope>
    </dependency>

    <!-- docs -->
    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-annotations</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-core</artifactId>
      <scope>provided</scope>
    </dependency>

    <!-- javadoc requires all the annotations on the classpath -->
    <!-- https://stackoverflow.com/questions/27808734/jdk8-error-class-file-for-javax-interceptor-interceptorbinding-not-found-whe -->
    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.1-api</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.interceptor</groupId>
      <artifactId>javax.interceptor-api</artifactId>
      <version>1.2</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.antlr</groupId>
      <artifactId>antlr4-runtime</artifactId>
      <version>${version.org.antlr}</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>hawkular-${project.artifactId}-${project.version}</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>${version.maven-war-plugin}</version>
        <configuration>
          <archive>
            <manifest>
              <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
              <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
            </manifest>
            <!-- <manifestEntries> <Build-Number>${buildNumber}</Build-Number>
              </manifestEntries> -->
          </archive>
          <webResources>
            <resource>
              <filtering>false</filtering>
              <directory>${basedir}/src/main/webapp</directory>
            </resource>
          </webResources>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.revapi</groupId>
        <artifactId>revapi-maven-plugin</artifactId>
        <configuration>
          <skip>true</skip>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.antlr</groupId>
        <artifactId>antlr4-maven-plugin</artifactId>
        <version>${version.org.antlr}</version>
        <executions>
          <execution>
            <goals><goal>antlr4</goal></goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>docgen</id>
      <properties>
        <restDocDirectory>${project.basedir}/src/main/rest-doc</restDocDirectory>
        <swaggerDirectory>${project.build.directory}/generated/swagger-ui</swaggerDirectory>
      </properties>
      <build>
        <resources>
          <resource>
            <directory>${restDocDirectory}</directory>
            <includes>
              <include>*.json</include>
            </includes>
            <filtering>true</filtering>
            <targetPath>${project.build.directory}/processed/rest-doc</targetPath>
          </resource>
        </resources>

        <!-- Document generation from the Swagger annotations on the REST-API. -->
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <id>unpack</id>
                <phase>process-resources</phase>
                <goals>
                  <goal>unpack</goal>
                </goals>
                <configuration>
                  <artifactItems>
                    <artifactItem>
                      <groupId>org.hawkular</groupId>
                      <artifactId>hawkular-build-tools</artifactId>
                      <version>${version.org.hawkular.hawkular-build-tools}</version>
                      <type>jar</type>
                      <includes>**/*.groovy</includes>
                    </artifactItem>
                  </artifactItems>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>com.github.kongchen</groupId>
            <artifactId>swagger-maven-plugin</artifactId>
            <configuration>
              <apiSources>
                <apiSource>
                  <springmvc>false</springmvc>
                  <locations>org.hawkular.inventory.rest</locations>
                  <basePath>/hawkular/inventory</basePath>
                  <info>
                    <title>Hawkular Inventory</title>
                    <!-- The version of the REST API - not of the project -->
                    <version>0.2</version>
                  </info>
                  <swaggerDirectory>${swaggerDirectory}</swaggerDirectory>
                </apiSource>
              </apiSources>
            </configuration>
            <executions>
              <execution>
                <phase>compile</phase>
                <goals>
                  <goal>generate</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <dependencies>
              <dependency>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-all</artifactId>
                <version>${version.org.codehaus.groovy}</version>
              </dependency>
            </dependencies>
            <configuration>
              <properties>
                <baseFile>${restDocDirectory}/base.adoc</baseFile>
                <baseSwaggerFile>${project.build.directory}/processed/rest-doc/swagger.json</baseSwaggerFile>
                <swaggerFile>${swaggerDirectory}/swagger.json</swaggerFile>
                <outputFile>${project.build.directory}/generated/rest-inventory.adoc</outputFile>
              </properties>
              <scripts>
                <script>file:///${project.build.directory}/dependency/hawkular-documentation/apidoc.groovy</script>
              </scripts>
            </configuration>
            <executions>
              <execution>
                <!-- We have the docs for the deprecated API defined using swagger annotations in code, but the new
                     API is too much to swallow for swagger annotation, so it is documented in a standalone
                     swagger json file: main/rest-doc/swagger.json. This groovy snippet merged the two together
                     so that we can have nice docs for both the new API and the old API. -->
                <id>merge-swagger-jsons</id>
                <phase>compile</phase>
                <goals>
                  <goal>execute</goal>
                </goals>
                <configuration>
                  <scripts>
                    <script><![CDATA[
                      class Updater {
                        static void updateBase(Object base, Object overrides) {
                            if (base instanceof List) {
                                if (overrides instanceof List) {
                                    def b = (List) base
                                    def o = (List) overrides
                                    o.eachWithIndex { ov, i ->
                                        if (b[i] == ov) {
                                            updateBase(b[i], ov)
                                        } else {
                                            b.add(ov)
                                        }
                                    }
                                }
                            } else if (base instanceof Map) {
                                if (overrides instanceof Map) {
                                    def b = (Map) base
                                    def o = (Map) overrides
                                    o.each { k, v ->
                                        if (b.containsKey(k)) {
                                            updateBase(b[k], v)
                                        } else {
                                            b[k] = v
                                        }
                                    }
                                }
                            }
                        }
                      }

                      def js = new groovy.json.JsonSlurper()
                      def bsf = new File(baseSwaggerFile)
                      def sf = new File(swaggerFile)

                      def base = js.parse(bsf)
                      def updates = js.parse(sf)

                      //deep copy
                      def defs = evaluate(base["definitions"].inspect())

                      Updater.updateBase(base, updates)

                      //the definitions in the deprecated API are royally messed up.
                      base["definitions"] = defs

                      def json = groovy.json.JsonOutput.toJson(base)
                      json = groovy.json.JsonOutput.prettyPrint(json)

                      sf.newWriter().withWriter { w -> w << json } ]]></script>
                  </scripts>
                </configuration>
              </execution>
              <execution>
                <id>generate-api-doc</id>
                <phase>compile</phase>
                <goals>
                  <goal>execute</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

    <profile>
      <id>dev</id>
      <properties>
        <wf.home>${project.build.directory}/wildfly-${version.org.wildfly}</wf.home>
      </properties>
      <dependencies>
        <dependency>
          <groupId>org.wildfly</groupId>
          <artifactId>wildfly-dist</artifactId>
          <version>${version.org.wildfly}</version>
          <type>zip</type>
        </dependency>
      </dependencies>
      <build>
        <plugins>
          <!-- download and unzip wildfly -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <id>unpack-wildfly</id>
                <phase>package</phase>
                <goals>
                  <goal>unpack-dependencies</goal>
                </goals>
                <configuration>
                  <includeGroupIds>org.wildfly</includeGroupIds>
                  <includeArtifactIds>wildfly-dist</includeArtifactIds>
                  <outputDirectory>${project.build.directory}/</outputDirectory>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <!-- deploy the war -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
              <execution>
                <id>deploy-war</id>
                <phase>install</phase>
                <goals>
                  <goal>copy-resources</goal>
                </goals>
                <configuration>
                  <outputDirectory>${wf.home}/standalone/deployments/</outputDirectory>
                  <resources>
                    <resource>
                      <directory>${project.build.directory}</directory>
                      <includes>
                        <include>*.war</include>
                      </includes>
                    </resource>
                  </resources>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <!-- echo the usage -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <phase>install</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <tasks>
                    <echo>Run the server with inventory by${line.separator}${wf.home}/bin/standalone.sh${line.separator}${line.separator}</echo>
                  </tasks>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
