<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You 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.apache.tez</groupId>
    <artifactId>tez</artifactId>
    <version>0.10.2.460-dep-933</version>
  </parent>
  <artifactId>tez-ui</artifactId>
  <packaging>war</packaging>

  <properties>
    <webappDir>src/main/webapp</webappDir>

    <nodeVersion>v8.9.0</nodeVersion>
    <nodeExecutable>${basedir}/src/main/webapp/node/node</nodeExecutable>
    <servlet.version>3.1.0</servlet.version>
    <packageManagerScript>node/yarn/dist/bin/yarn.js</packageManagerScript>
    <test.tmp.dir>${project.build.directory}/tmp</test.tmp.dir>
    <test.conf.dir>${project.build.directory}/testconf</test.conf.dir>
    <test.warehouse.dir>${project.build.directory}/warehouse</test.warehouse.dir>

    <allow-root-build>--allow-root</allow-root-build>

    <skipTests>true</skipTests>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.mapr.security</groupId>
      <artifactId>mapr-security-web</artifactId>
      <version>${mapr.release.version}</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>${commons-lang3.version}</version>
    </dependency>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>${servlet.version}</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-yarn-api</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-yarn-common</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-bundle</artifactId>
    </dependency>
    <dependency>
      <groupId>javax.ws.rs</groupId>
      <artifactId>javax.ws.rs-api</artifactId>
    </dependency>
  </dependencies>

  <profiles>
    <!-- profile to be activated if -Dtest is not provided: 'mvn clean install' -->
    <!-- runs 'ember test' execution if -DskipTests is not provided -->
    <profile>
      <id>default</id>
      <activation>
        <property>
          <name>!test</name>
        </property>
      </activation>
      <build>
        <plugins>
        <plugin>
          <artifactId>exec-maven-plugin</artifactId>
          <groupId>org.codehaus.mojo</groupId>
          <executions>
            <!-- Build -->
            <execution>
              <id>ember build</id>
              <phase>generate-resources</phase>
              <goals>
                <goal>exec</goal>
              </goals>
              <configuration>
                <workingDirectory>${webappDir}</workingDirectory>
                <executable>${nodeExecutable}</executable>
                <arguments>
                  <argument>${packageManagerScript}</argument>
                  <argument>run</argument>
                  <argument>build:mvn</argument>
                </arguments>
              </configuration>
            </execution>

            <!-- Test if enabled-->
            <execution>
              <id>ember test</id>
              <phase>test</phase>
              <goals>
                <goal>exec</goal>
              </goals>
              <configuration>
                <skip>${skipTests}</skip>
                <workingDirectory>${webappDir}</workingDirectory>
                <executable>${nodeExecutable}</executable>
                <arguments>
                  <argument>${packageManagerScript}</argument>
                  <argument>run</argument>
                  <argument>test:mvn</argument>
                </arguments>
              </configuration>
            </execution>
          </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

    <!-- profile to be activated if -Dtest=ember: 'mvn clean install -Dtest=ember' -->
    <!-- runs 'ember test' execution -->
    <profile>
      <id>ui-tests</id>
      <activation>
        <property>
          <name>test</name>
          <value>ember</value>
        </property>
      </activation>
      <build>
        <plugins>
        <plugin>
          <artifactId>exec-maven-plugin</artifactId>
          <groupId>org.codehaus.mojo</groupId>
          <executions>
            <!-- Build -->
            <execution>
              <id>ember build</id>
              <phase>generate-resources</phase>
              <goals>
                <goal>exec</goal>
              </goals>
              <configuration>
                <workingDirectory>${webappDir}</workingDirectory>
                <executable>${nodeExecutable}</executable>
                <arguments>
                  <argument>${packageManagerScript}</argument>
                  <argument>run</argument>
                  <argument>build:mvn</argument>
                </arguments>
              </configuration>
            </execution>

            <!-- Test if enabled-->
            <execution>
              <id>ember test</id>
              <phase>test</phase>
              <goals>
                <goal>exec</goal>
              </goals>
              <configuration>
                <skip>${skipTests}</skip>
                <workingDirectory>${webappDir}</workingDirectory>
                <executable>${nodeExecutable}</executable>
                <arguments>
                  <argument>${packageManagerScript}</argument>
                  <argument>run</argument>
                  <argument>test:mvn</argument>
                </arguments>
              </configuration>
            </execution>
          </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

    <!-- profile to be activated if -Dtest is defined but it doesn't equal to 'ember': 'mvn clean install -Dtest=TestAnythingElse' -->
    <!-- doesn't run 'ember test' execution -->
    <profile>
      <id>no-ui-tests</id>
      <activation>
        <property>
          <name>test</name>
          <value>.*</value>
        </property>
      </activation>
      <build>
        <plugins>
        <plugin>
          <artifactId>exec-maven-plugin</artifactId>
          <groupId>org.codehaus.mojo</groupId>
          <executions>
            <!-- Build -->
            <execution>
              <id>ember build</id>
              <phase>generate-resources</phase>
              <goals>
                <goal>exec</goal>
              </goals>
              <configuration>
                <workingDirectory>${webappDir}</workingDirectory>
                <executable>${nodeExecutable}</executable>
                <arguments>
                  <argument>${packageManagerScript}</argument>
                  <argument>run</argument>
                  <argument>build:mvn</argument>
                </arguments>
              </configuration>
            </execution>

            <!-- Test - to be skipped -->
            <execution>
                <id>ember test</id>
                <phase>test</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
                <configuration>
                  <executable>${nodeExecutable}</executable>
                  <skip>true</skip>
                </configuration>
              </execution>
          </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

    <!-- Clear temporary directories -->
    <profile>
      <id>cleanUICache</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <configuration>
              <filesets>
                <fileset>
                  <directory>${webappDir}/dist</directory>
                </fileset>
                <fileset>
                  <directory>${webappDir}/tmp</directory>
                </fileset>
                <fileset>
                  <directory>${webappDir}/node</directory>
                </fileset>
                <fileset>
                  <directory>${webappDir}/node_modules</directory>
                </fileset>
                <fileset>
                  <directory>${webappDir}/bower_components</directory>
                </fileset>
              </filesets>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>allowRootBuild</id>
      <activation>
        <property>
          <name>allow.root.build</name>
          <value>true</value>
        </property>
      </activation>
      <properties>
        <allow-root-build>--allow-root=true</allow-root-build>
      </properties>
    </profile>
    <profile>
      <id>noui</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <build>
      <plugins>
        <plugin>
          <groupId>org.apache.rat</groupId>
          <artifactId>apache-rat-plugin</artifactId>
          <configuration>
            <skip>true</skip>
          </configuration>
        </plugin>
        <plugin>
          <groupId>com.github.eirslett</groupId>
          <artifactId>frontend-maven-plugin</artifactId>
          <configuration>
            <skip>true</skip>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>exec-maven-plugin</artifactId>
          <groupId>org.codehaus.mojo</groupId>
          <configuration>
            <skip>true</skip>
          </configuration>
        </plugin>
        <plugin>
          <groupId>ro.isdc.wro4j</groupId>
          <artifactId>wro4j-maven-plugin</artifactId>
          <configuration>
            <skip>true</skip>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <configuration>
            <skip>true</skip>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <configuration>
            <skip>true</skip>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <configuration>
            <skip>true</skip>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-install-plugin</artifactId>
          <configuration>
            <skip>true</skip>
          </configuration>
        </plugin>
      </plugins>
      </build>
    </profile>
  </profiles>

  <build>
    <plugins>

      <!-- Apache RAT check -->
      <plugin>
        <groupId>org.apache.rat</groupId>
        <artifactId>apache-rat-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>src/main/webapp/node/**/*</exclude>
            <exclude>src/main/webapp/node_modules/**/*</exclude>
            <exclude>src/main/webapp/bower_components/**/*</exclude>
            <exclude>src/main/webapp/.tmp/**/*</exclude>
            <exclude>src/main/webapp/dist/**/*</exclude>
            <exclude>src/main/webapp/tmp/**/*</exclude>
            <exclude>src/main/webapp/.bowerrc</exclude>
            <exclude>src/main/webapp/.editorconfig</exclude>
            <exclude>src/main/webapp/.ember-cli</exclude>
            <exclude>src/main/webapp/.gitignore</exclude>
            <exclude>src/main/webapp/.jshintrc</exclude>
            <exclude>src/main/webapp/tests/.jshintrc</exclude>
            <exclude>src/main/webapp/blueprints/.jshintrc</exclude>
            <exclude>src/main/webapp/.watchmanconfig</exclude>
            <exclude>src/main/webapp/bower.json</exclude>
            <exclude>src/main/webapp/bower-shrinkwrap.json</exclude>
            <exclude>src/main/webapp/ember-cli-build.js</exclude>
            <exclude>src/main/webapp/package.json</exclude>
            <exclude>src/main/webapp/yarn.lock</exclude>
            <exclude>src/main/webapp/testem.json</exclude>
            <exclude>src/main/webapp/public/assets/images/*</exclude>
            <exclude>src/main/webapp/WEB-INF/wro.xml</exclude>
          </excludes>
        </configuration>
      </plugin>

      <!-- Install Node, Yarn, Bower & dependencies -->
      <plugin>
        <groupId>com.github.eirslett</groupId>
        <artifactId>frontend-maven-plugin</artifactId>
        <configuration>
          <workingDirectory>${webappDir}</workingDirectory>
        </configuration>
        <executions>

          <!-- Install all dependencies -->
          <execution>
            <phase>generate-resources</phase>
            <id>install node and yarn</id>
            <goals>
              <goal>install-node-and-yarn</goal>
            </goals>
            <configuration>
              <nodeVersion>${nodeVersion}</nodeVersion>
              <yarnVersion>v1.6.0</yarnVersion>
            </configuration>
          </execution>
          <execution>
            <phase>generate-resources</phase>
            <id>yarn install</id>
            <goals>
              <goal>yarn</goal>
            </goals>
            <configuration>
              <arguments>install --ignore-engines</arguments>
            </configuration>
          </execution>
          <execution>
            <phase>generate-resources</phase>
            <id>bower install</id>
            <configuration>
              <arguments>install ${allow-root-build}</arguments>
            </configuration>
            <goals>
              <goal>bower</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <!-- Asset minifier -->
      <plugin>
        <groupId>ro.isdc.wro4j</groupId>
        <artifactId>wro4j-maven-plugin</artifactId>
        <executions>
          <execution>
            <phase>prepare-package</phase>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <minimize>true</minimize>
          <targetGroups>tez-ui,vendor</targetGroups>
          <destinationFolder>${basedir}/target/minified-resources/assets</destinationFolder>
          <contextFolder>${basedir}/src/main/webapp/dist/assets</contextFolder>
          <wroFile>${basedir}/src/main/webapp/WEB-INF/wro.xml</wroFile>
        </configuration>
      </plugin>

      <!-- Package into war -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
          </execution>
        </executions>
        <configuration>
          <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>
          <warSourceDirectory>${webappDir}/dist</warSourceDirectory>
          <webResources>
            <resource>
              <filtering>false</filtering>
              <directory>${basedir}/src/main/resources/</directory>
            </resource>
            <resource>
              <filtering>false</filtering>
              <directory>${basedir}/target/minified-resources</directory>
            </resource>
          </webResources>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <skipIfEmpty>true</skipIfEmpty>
        </configuration>
      </plugin>
      <!-- Downloading Tomcat TAR.GZ, using downloads/ dir to avoid downloading over an over -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <id>dist</id>
            <goals>
              <goal>run</goal>
            </goals>
            <phase>package</phase>
            <configuration>
              <target>
                <mkdir dir="downloads"/>
                <get
                        src="https://archive.apache.org/dist/tomcat/tomcat-9/v${tomcat.version}/bin/apache-tomcat-${tomcat.version}.tar.gz"
                        dest="downloads/tomcat.tar.gz" verbose="true" skipexisting="true"/>

                <!-- Add server.xml and  tez-ui war to tomcat.tar.gz -->
                <echo file="${project.build.directory}/tomcat-add-files.sh">

                  cd ${basedir}/downloads/
                  tar -zxvf ${basedir}/downloads/tomcat.tar.gz
                  rm ${basedir}/downloads/tomcat.tar.gz
                  rm -r ${basedir}/downloads/apache-tomcat-${tomcat.version}/webapps/*
                  mkdir ${basedir}/downloads/apache-tomcat-${tomcat.version}/webapps/ROOT
                  mkdir ${basedir}/downloads/apache-tomcat-${tomcat.version}/webapps/tez-ui
                  cp ${basedir}/target/tez-ui-${version}.war ${basedir}/downloads/apache-tomcat-${tomcat.version}/webapps/tez-ui/tez-ui.war
                  cd ${basedir}/downloads/apache-tomcat-${tomcat.version}/webapps/tez-ui
                  jar xvf tez-ui.war
                  rm tez-ui.war
                  mkdir ${basedir}/downloads/apache-tomcat-${tomcat.version}/shared
                  mkdir ${basedir}/downloads/apache-tomcat-${tomcat.version}/shared/lib
                  mkdir ${basedir}/downloads/apache-tomcat-${tomcat.version}/shared/classes
                  mv ${basedir}/downloads/apache-tomcat-${tomcat.version}/webapps/tez-ui/WEB-INF/classes/* ${basedir}/downloads/apache-tomcat-${tomcat.version}/shared/classes
                  mv ${basedir}/downloads/apache-tomcat-${tomcat.version}/webapps/tez-ui/WEB-INF/lib/* ${basedir}/downloads/apache-tomcat-${tomcat.version}/shared/lib
                  cp ${basedir}/conf/server.xml ${basedir}/downloads/apache-tomcat-${tomcat.version}/conf
                  cp ${basedir}/conf/web.xml ${basedir}/downloads/apache-tomcat-${tomcat.version}/conf
                  cp ${basedir}/conf/catalina.properties ${basedir}/downloads/apache-tomcat-${tomcat.version}/conf
                  cp ${basedir}/conf/index.jsp ${basedir}/downloads/apache-tomcat-${tomcat.version}/webapps/ROOT
                  sed -i -- 's/tezwebui.admin.port/${tezwebui.admin.port}/g' ${basedir}/downloads/apache-tomcat-${tomcat.version}/conf/server.xml
                  cd ${basedir}/downloads/
                  tar -czvf tomcat.tar.gz apache-tomcat-${tomcat.version}
                </echo>

                <exec executable="sh" dir="${project.build.directory}" failonerror="true">
                  <arg line="./tomcat-add-files.sh"/>
                </exec>

              </target>
            </configuration>
          </execution>
          <execution>
            <id>setup-test-dirs</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <target>
                <delete dir="${test.tmp.dir}" />
                <delete dir="${test.conf.dir}" />
                <mkdir dir="${test.tmp.dir}" />
                <mkdir dir="${test.conf.dir}" />
              </target>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <environmentVariables>
            <MAPR_HOME>${test.tmp.dir}/opt/mapr</MAPR_HOME>
          </environmentVariables>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
