#!/bin/bash
# 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.

set -ex

. `dirname $0`/bigtop.bom

BUILD_OPTS="-Divy.home=${HOME}/.ivy2 -Dsbt.ivy.home=${HOME}/.ivy2 -Duser.home=${HOME} \
            -Drepo.maven.org=$IVY_MIRROR_PROP \
            -Dreactor.repo=file://${HOME}/.m2/repository \
            -Dhadoop.version=$HADOOP_VERSION \
            -Dkafka.version=$KAFKA_VERSION \
            -Dzookeeper.version=$ZOOKEEPER_VERSION \
            -Dyarn.version=$HADOOP_VERSION \
            -Pyarn -Phadoop-3 \
            -Phive -Phive-thriftserver \
            -Psparkr -Pkubernetes \
            -Pscala-${SCALA_VERSION%.*} \
            -Dguava.version=27.0-jre \
            $SPARK_BUILD_OPTS"

# BIGTOP-3762
export MAVEN_OPTS="${MAVEN_OPTS:--Xss64m -Xmx4g -XX:ReservedCodeCacheSize=1g}"

./dev/make-distribution.sh --mvn mvn --r $BUILD_OPTS -DskipTests

SPARK_SKIP_TESTS=$([ "$SPARK_RUN_TESTS" = "true" ] && echo false || echo true)

# make-distribution.sh will only run "mvn clean package", so in order to get the
# Spark packages installed in the local Maven repository (or to run the tests),
# we need to run "mvn install" again. However, it will be relatively fast because
# we are not running the "clean" phase.
#
# This is also the point that we can run the tests if desired, since tests must
# be run after Spark has already been packaged.
# See http://spark.apache.org/docs/latest/building-spark.html#spark-tests-in-maven
mvn $BUILD_OPTS install -DskipTests=$SPARK_SKIP_TESTS
