/*
 * 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.
 */

buildscript {
  repositories {
    mavenCentral()
  }

  configurations.classpath {
    resolutionStrategy.eachDependency {  details ->
      if (details.requested.group == 'com.burgstaller' && details.requested.name == 'okhttp-digest' && details.requested.version == '1.10') {
        details.useTarget "io.github.rburgst:${details.requested.name}:1.21"
        details.because 'Dependency has moved'
      }
    }
  }

  apply from: file('gradle/buildscript.gradle'), to: buildscript
}

allprojects {
  repositories {
    mavenCentral()
    mavenLocal()
  }
}

apply from: file("gradle/dependency-versions.gradle")
apply from: file("gradle/dependency-versions-scala-" + scalaSuffix + ".gradle")
apply from: file("gradle/release.gradle")
apply from: file("gradle/rat.gradle")
apply from: file('gradle/customize.gradle')
apply from: file('gradle/stacktrace.gradle')


rat {
  excludes = [
    '*.patch',
    '**/*.eot',
    '**/*.graffle',
    '**/*.iml',
    '**/*.ipr',
    '**/*.iws',
    '**/*.json',
    '**/*.otf',
    '**/*.svg',
    '**/*.ttf',
    '**/*.woff',
    '*.log',
    '**/hs_err_pid*.log',
    '**/.classpath',
    '**/.cache/**',
    '**/.git/**',
    '**/.gitignore',
    '**/.gradle/**',
    '**/.idea/**',
    '**/.project',
    '**/.settings/**',
    '**/.DS_Store/**',
    '**/bootstrap.css.map',
    '**/bootstrap.min.css',
    '**/bootstrap.min.js',
    '**/build/**',
    '**/ionicons.min.css',
    '**/font-awesome.min.css',
    '**/jquery-1.11.1.min.js',
    '**/jquery.tablesorter.min.js',
    '**/flickity.js',
    '**/non-responsive.less',
    '**/ropa-sans.css',
    '**/syntax.css',
    '**/d3.v3.min.js',
    '**/dagre-d3.min.js',
    '.idea/**',
    '.reviewboardrc',
    'docs/_site/**',
    'docs/sitemap.xml',
    'docs/learn/documentation/*/api/javadocs/**',
    'docs/learn/documentation/*/rest/javadocs/**',
    'docs/.jekyll-cache/**',
    'gradle/wrapper/**',
    'gradlew',
    'gradlew.bat',
    'samza-test/state/**',
    'README.md',
    'RELEASE.md',
    'samza-core/src/test/resources/classloader/samza-framework-api-classes.txt',
    'samza-core/src/test/resources/*.txt',
    'samza-core/bin/test/*.txt',
    'samza-core/bin/test/classloader/samza-framework-api-classes.txt',
    'samza-test/src/main/resources/**',
    'samza-hdfs/src/main/resources/**',
    'samza-hdfs/src/test/resources/**',
    'out/**',
    'state/**'
  ]
}

allprojects {
  apply plugin: 'idea'

  // For all scala compilation, add extra compiler options, taken from version-specific
  // dependency-versions-scala file applied above.
  tasks.withType(ScalaCompile) {
    scalaCompileOptions.additionalParameters = scalaOptions
  }
}

idea {
  project {
    languageLevel = 1.8
  }
}

subprojects {
  apply plugin: 'eclipse'
  apply plugin: 'project-report'
  apply plugin: 'jacoco'
  apply plugin: 'checkstyle'

  checkstyle {
    configFile = new File(rootDir, "checkstyle/checkstyle.xml")
    toolVersion = "$checkstyleVersion"
  }

  tasks.withType(Test) {
    test {
      testLogging {
        showStackTraces = true
        showExceptions = true
        showCauses = true
        displayGranularity = maxGranularity
        exceptionFormat = 'full'
      }
    }
  }
}

project(":docs") {
  apply plugin: "com.github.jruby-gradle.base"
  apply plugin: 'java'

  repositories {
    ruby.gems()
  }

  jruby {
      jrubyVersion = '9.3.9.0'
  }

  dependencies {
      gems "rubygems:jekyll-sass-converter:2.2.0"
      gems "rubygems:jekyll:4.3.1"
      gems "rubygems:http_parser.rb:0.6.0"
      gems "rubygems:webrick:1.7.0"
      gems "rubygems:json:2.6.3"
      gems "rubygems:rouge:3.30.0"
      gems "rubygems:kramdown:2.4.0"
      gems "rubygems:kramdown-parser-gfm:1.1.0"
  }

  task jekyllBuild(type: com.github.jrubygradle.JRubyExec) {
      script "jekyll"
      scriptArgs "build", "$projectDir", "-d", "$projectDir/_site"
  }

  task jekyllServeLocal(type: com.github.jrubygradle.JRubyExec) {
      def baseurl = ""
      script "jekyll"
      scriptArgs "serve", "--watch", "--source", "$projectDir" , "--destination", "$projectDir/_site", "--trace", "--baseurl", "$baseurl"
  }

  task jekyllServePublic(type: com.github.jrubygradle.JRubyExec) {
      def baseurl = ""
      script "jekyll"
      scriptArgs "serve", "--watch", "--source", "$projectDir" , "--destination", "$projectDir/_site", "--trace", "--baseurl", "$baseurl", "--host", "0.0.0.0"
  }

}

project(':samza-api') {
  apply plugin: 'java'

  dependencies {
    compile "org.apache.commons:commons-lang3:$commonsLang3Version"
    compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
    compile "com.google.guava:guava:$guavaVersion"
    compile "com.google.code.gson:gson:$gsonVersion"
    compile "org.slf4j:slf4j-api:$slf4jVersion"
    compile "io.dropwizard.metrics:metrics-core:3.1.2"
    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-core:$mockitoVersion"
  }
}

project(":samza-core_$scalaSuffix") {
  apply plugin: 'scala'

  // Force scala joint compilation
  sourceSets.main.scala.srcDir "src/main/java"
  sourceSets.test.scala.srcDir "src/test/java"

  // Disable the Javac compiler by forcing joint compilation by scalac. This is equivalent to setting
  // tasks.compileTestJava.enabled = false
  sourceSets.main.java.srcDirs = []
  sourceSets.test.java.srcDirs = []

  jar {
    manifest {
      attributes("Implementation-Version": "$version")
    }
  }

  dependencies {
    compile project(':samza-api')
    compile("org.apache.helix:zookeeper-api:$helixVersion") {
      exclude module: 'junit:junit'
      // exclude the slf4j implementation since it should be up to the application to choose the logging implementation
      exclude group: 'org.slf4j', module: 'slf4j-log4j12'
    }
    compile "org.xerial.snappy:snappy-java:$snappyVersion"
    compile "com.google.guava:guava:$guavaVersion"
    compile "net.sf.jopt-simple:jopt-simple:$joptSimpleVersion"
    compile "org.apache.commons:commons-collections4:$apacheCommonsCollections4Version"
    compile "org.apache.commons:commons-lang3:$commonsLang3Version"
    compile "commons-io:commons-io:$commonsIoVersion"
    compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
    compile "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:$jacksonVersion"
    compile "org.eclipse.jetty:jetty-webapp:$jettyVersion"
    compile "org.scala-lang:scala-library:$scalaVersion"
    compile "org.slf4j:slf4j-api:$slf4jVersion"
    compile "net.jodah:failsafe:$failsafeVersion"
    compile "com.github.oshi:oshi-core:$oshiVersion"
    compile "net.java.dev.jna:jna:$jnaVersion"
    compile "net.java.dev.jna:jna-platform:$jnaVersion"
    testCompile project(":samza-api").sourceSets.test.output
    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-core:$mockitoVersion"
    testCompile "org.powermock:powermock-api-mockito:$powerMockVersion"
    testCompile "org.powermock:powermock-core:$powerMockVersion"
    testCompile "org.powermock:powermock-module-junit4:$powerMockVersion"
    testCompile "org.scalatest:scalatest_$scalaSuffix:$scalaTestVersion"
    testCompile "org.hamcrest:hamcrest-all:$hamcrestVersion"
    // zookeeper debug logging causes some memory issues in tests; avoid those issues by using log4j2, which defaults to error log level
    testRuntime "org.apache.logging.log4j:log4j-slf4j-impl:$log4j2Version"
  }

  test {
    // some unit tests use embedded zookeeper, so adding some extra memory for those
    maxHeapSize = "1560m"
    jvmArgs = ["-XX:+UseConcMarkSweepGC", "-server"]
  }
}

project(":samza-azure_$scalaSuffix") {
  apply plugin: 'java'

  dependencies {
    compile "com.azure:azure-storage-blob:12.21.1"
    compile "com.azure:azure-identity:1.8.1"
    compile "com.microsoft.azure:azure-storage:8.6.6"
    compile "com.microsoft.azure:azure-eventhubs:3.3.0"
    compile "com.fasterxml.jackson.core:jackson-core:$jacksonVersion"
    compile "io.dropwizard.metrics:metrics-core:3.1.2"
    compile "org.apache.avro:avro:$avroVersion"
    compile project(':samza-api')
    compile project(":samza-core_$scalaSuffix")
    compile "org.slf4j:slf4j-api:$slf4jVersion"
    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-core:$mockitoVersion"
    testCompile "org.powermock:powermock-api-mockito:$powerMockVersion"
    testCompile "org.powermock:powermock-core:$powerMockVersion"
    testCompile "org.powermock:powermock-module-junit4:$powerMockVersion"
  }
}

project(":samza-aws_$scalaSuffix") {
  apply plugin: 'java'

  dependencies {
    compile "com.amazonaws:aws-java-sdk-kinesis:1.11.152"
    compile "com.amazonaws:amazon-kinesis-client:1.7.5"
    compile "com.amazonaws:amazon-kinesis-producer:0.10.0"
    compile "io.dropwizard.metrics:metrics-core:3.1.2"
    compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
    compile project(':samza-api')
    compile project(":samza-core_$scalaSuffix")
    compile "org.slf4j:slf4j-api:$slf4jVersion"
    runtime "org.apache.httpcomponents:httpclient:4.5.2"
    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-all:$mockitoVersion"
  }

  repositories {
    maven {
      url "https://repo1.maven.org/maven2/"
    }
  }
}

project(":samza-elasticsearch_$scalaSuffix") {
  apply plugin: 'java'

  dependencies {
    compile project(':samza-api')
    compile project(":samza-core_$scalaSuffix")
    compile "org.elasticsearch:elasticsearch:$elasticsearchVersion"
    compile "org.slf4j:slf4j-api:$slf4jVersion"
    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-core:$mockitoVersion"

    testRuntime "org.slf4j:slf4j-simple:$slf4jVersion"
  }
}

project(":samza-sql_$scalaSuffix") {
  apply plugin: 'java'

  // Disabling assertions while running test because of a calcite bug in {@link RelOptUtil#eq} method
  // it checks for the type1 != type2 rather than !type1.equals(type2)
  tasks.withType(Test) {
    enableAssertions = false
  }

  dependencies {
    compile project(':samza-api')
    compile project(":samza-core_$scalaSuffix")
    compile project(":samza-kafka_$scalaSuffix")
    compile project(":samza-kv-inmemory_$scalaSuffix")
    compile project(":samza-kv-rocksdb_$scalaSuffix")
    compile "org.apache.avro:avro:$avroVersion"
    compile "org.apache.calcite:calcite-core:$calciteVersion"
    compile "org.codehaus.janino:commons-compiler:3.0.11"
    compile "org.slf4j:slf4j-api:$slf4jVersion"
    compile "org.reflections:reflections:0.9.10"

    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-core:$mockitoVersion"
    testCompile "org.powermock:powermock-api-mockito:$powerMockVersion"
    testCompile "org.powermock:powermock-core:$powerMockVersion"
    testCompile "org.powermock:powermock-module-junit4:$powerMockVersion"

    testRuntime "org.slf4j:slf4j-simple:$slf4jVersion"
  }
}

project(":samza-sql-shell_$scalaSuffix") {
  apply plugin: 'java'

  jar {
    manifest {
      attributes("Implementation-Version": "$version")
    }
  }

  dependencies {
    compile project(":samza-sql_$scalaSuffix")
    compile project(":samza-tools_$scalaSuffix")
    compile project(":samza-core_$scalaSuffix")
    compile project(':samza-api')
    compile project(":samza-kafka_$scalaSuffix")
    compile project(":samza-azure_$scalaSuffix")
    compile "net.java.dev.jna:jna:$jnaVersion"
    compile "org.jline:jline:$jlineVersion"

    testCompile "junit:junit:$junitVersion"
  }

  tasks.create(name: "releaseSqlShellTarGz", dependsOn: configurations.archives.artifacts, type: Tar) {
    into "samza-sql-shell-${version}"
    compression = Compression.GZIP
    from(project.file("./scripts")) { into "scripts/" }
    from(project.file("./conf")) { into "conf/" }
    from(project(':samza-shell').file("src/main/bash/run-class.sh")) { into "scripts/" }
    from(configurations.runtime) { into("lib/") }
    from(configurations.archives.artifacts.files) { into("lib/") }
    duplicatesStrategy 'exclude'
  }
}

project(":samza-tools_$scalaSuffix") {
  apply plugin: 'java'

  dependencies {
    compile project(":samza-sql_$scalaSuffix")
    compile project(':samza-api')
    compile project(":samza-azure_$scalaSuffix")
    compile project(":samza-core_$scalaSuffix")
    compile "org.slf4j:slf4j-api:$slf4jVersion"
    compile "commons-cli:commons-cli:$commonsCliVersion"
    compile "org.apache.avro:avro:$avroVersion"
    compile "org.apache.commons:commons-lang3:$commonsLang3Version"
    compile "org.apache.kafka:kafka-clients:$kafkaVersion"
  }

  tasks.create(name: "releaseToolsTarGz", dependsOn: configurations.archives.artifacts, type: Tar) {
    into "samza-tools-${version}"
    compression = Compression.GZIP
    from(project.file("./scripts")) { into "scripts/" }
    from(project.file("./config")) { into "config/" }
    from(project(':samza-shell').file("src/main/bash/run-class.sh")) { into "scripts/" }
    from(configurations.runtime) { into("lib/") }
    from(configurations.archives.artifacts.files) { into("lib/") }
    duplicatesStrategy 'exclude'
  }
}

project(":samza-kafka_$scalaSuffix") {
  apply plugin: 'scala'

  // Force scala joint compilation
  sourceSets.main.scala.srcDir "src/main/java"
  sourceSets.test.scala.srcDir "src/test/java"

  // Disable the Javac compiler by forcing joint compilation by scalac. This is equivalent to setting
  // tasks.compileTestJava.enabled = false
  sourceSets.main.java.srcDirs = []
  sourceSets.test.java.srcDirs = []

  configurations {
    // Remove transitive dependencies from Zookeeper that we don't want.
    compile.exclude group: 'javax.jms', module: 'jms'
    compile.exclude group: 'com.sun.jdmk', module: 'jmxtools'
    compile.exclude group: 'com.sun.jmx', module: 'jmxri'
  }

  dependencies {
    compile project(':samza-api')
    compile project(":samza-core_$scalaSuffix")
    compile "org.scala-lang:scala-library:$scalaVersion"
    compile "org.apache.helix:zookeeper-api:$helixVersion"
    compile "org.xerial.snappy:snappy-java:$snappyVersion"
    compile "org.apache.zookeeper:zookeeper:$zookeeperVersion"
    compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
    compile "org.apache.kafka:kafka_$scalaSuffix:$kafkaVersion"
    compile "org.apache.kafka:kafka-clients:$kafkaVersion"
    testCompile "org.apache.kafka:kafka_$scalaSuffix:$kafkaVersion:test"
    testCompile "org.apache.kafka:kafka-clients:$kafkaVersion:test"
    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-core:$mockitoVersion"
    testCompile "org.scalatest:scalatest_$scalaSuffix:$scalaTestVersion"
    testCompile project(":samza-core_$scalaSuffix").sourceSets.test.output

    // Logging in tests is good.
    testRuntime "org.slf4j:slf4j-simple:$slf4jVersion"

    constraints {
      compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
      compile "com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion"
      compile "com.fasterxml.jackson.module:jackson-module-scala_$scalaSuffix:$jacksonVersion"
      compile "com.fasterxml.jackson.dataformat:jackson-dataformat-csv:$jacksonVersion"
      compile "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:$jacksonVersion"
    }
  }

  test {
    // Bump up the heap so we can start ZooKeeper and Kafka brokers.
    minHeapSize = "1560m"
    maxHeapSize = "1560m"
    jvmArgs = ["-XX:+UseConcMarkSweepGC", "-server"]
    // There appear to be issues between TestKafkaSystemAdmin and
    // TestKafkaCheckpointManager both running brokeres and ZK. Restarting the
    // gradle worker after every test clears things up. These tests should be
    // moved to the integration test suite.
    forkEvery = 1
  }
}

project(":samza-log4j_$scalaSuffix") {
  apply plugin: 'java'

  dependencies {
    compile "log4j:log4j:$log4jVersion"
    compile "org.slf4j:slf4j-log4j12:$slf4jVersion"
    compile project(':samza-api')
    compile project(":samza-core_$scalaSuffix")
    compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
    testCompile "junit:junit:$junitVersion"
  }
}

project(":samza-log4j2_$scalaSuffix") {
  apply plugin: 'java'

  dependencies {
    compile "org.apache.logging.log4j:log4j-1.2-api:$log4j2Version"
    compile "org.apache.logging.log4j:log4j-api:$log4j2Version"
    compile "org.apache.logging.log4j:log4j-core:$log4j2Version"
    compile "org.apache.logging.log4j:log4j-slf4j-impl:$log4j2Version"
    compile project(':samza-api')
    compile project(":samza-core_$scalaSuffix")
    compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-core:$mockitoVersion"
  }
}

project(":samza-yarn_$scalaSuffix") {
  apply plugin: 'scala'
  apply plugin: 'lesscss'

  // Force scala joint compilation
  sourceSets.main.scala.srcDir "src/main/java"
  sourceSets.test.scala.srcDir "src/test/java"

  // Disable the Javac compiler by forcing joint compilation by scalac. This is equivalent to setting
  // tasks.compileTestJava.enabled = false
  sourceSets.main.java.srcDirs = []
  sourceSets.test.java.srcDirs = []

  dependencies {
    compile project(':samza-api')
    compile project(":samza-core_$scalaSuffix")
    compile "org.scala-lang:scala-library:$scalaVersion"
    compile "org.scala-lang:scala-compiler:$scalaVersion"
    compile "com.google.guava:guava:$guavaVersion"
    compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
    compile "commons-httpclient:commons-httpclient:$commonsHttpClientVersion"
    compile "org.apache.httpcomponents:httpclient:$httpClientVersion"
    compile("org.apache.hadoop:hadoop-yarn-api:$yarnVersion") {
    }
    compile("org.apache.hadoop:hadoop-yarn-common:$yarnVersion") {
      exclude module: 'servlet-api'
    }
    compile("org.apache.hadoop:hadoop-yarn-client:$yarnVersion") {
      exclude module: 'servlet-api'
    }
    compile("org.apache.hadoop:hadoop-common:$yarnVersion") {
      exclude module: 'servlet-api'
      // Exclude because YARN's 3.4.5 ZK version is incompatbile with Kafka's 3.3.4.
      exclude module: 'zookeeper'
    }
    compile("org.apache.hadoop:hadoop-hdfs:$yarnVersion") {
      exclude module: 'servlet-api'
    }
    compile("org.scalatra:scalatra_$scalaSuffix:$scalatraVersion") {
      exclude module: 'scala-compiler'
      exclude module: 'slf4j-api'
    }
    compile("org.scalatra:scalatra-scalate_$scalaSuffix:$scalatraVersion") {
      exclude module: 'scala-compiler'
      exclude module: 'slf4j-api'
    }
    compile "joda-time:joda-time:$jodaTimeVersion"
    compile "org.apache.zookeeper:zookeeper:$zookeeperVersion"
    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-core:$mockitoVersion"
    testCompile project(":samza-core_$scalaSuffix").sourceSets.test.output
    testCompile "org.scalatest:scalatest_$scalaSuffix:$scalaTestVersion"
  }

  repositories {
    maven {
      url "https://repo.typesafe.com/typesafe/releases"
    }
  }

  lesscss {
    source = fileTree('src/main/less') {
      include 'main.less'
    }
    dest = "$buildDir/resources/main/scalate/css"
  }

  jar.dependsOn("lesscss")
}

project(":samza-yarn3_$scalaSuffix") {
  apply plugin: 'scala'
  apply plugin: 'lesscss'

  // Force scala joint compilation
  sourceSets.main.scala.srcDir "src/main/java"
  sourceSets.test.scala.srcDir "src/test/java"

  // Disable the Javac compiler by forcing joint compilation by scalac. This is equivalent to setting
  // tasks.compileTestJava.enabled = false
  sourceSets.main.java.srcDirs = []
  sourceSets.test.java.srcDirs = []

  dependencies {
    compile project(':samza-api')
    compile project(":samza-core_$scalaSuffix")
    compile "org.scala-lang:scala-library:$scalaVersion"
    compile "org.scala-lang:scala-compiler:$scalaVersion"
    compile "com.google.guava:guava:$guavaVersion"
    compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
    compile "commons-httpclient:commons-httpclient:$commonsHttpClientVersion"
    compile "org.apache.httpcomponents:httpclient:$httpClientVersion"
    compile("org.apache.hadoop:hadoop-yarn-api:$yarn3Version") {
    }
    compile("org.apache.hadoop:hadoop-yarn-common:$yarn3Version") {
      exclude module: 'servlet-api'
    }
    compile("org.apache.hadoop:hadoop-yarn-client:$yarn3Version") {
      exclude module: 'servlet-api'
    }
    compile("org.apache.hadoop:hadoop-common:$yarn3Version") {
      exclude module: 'servlet-api'
      // Exclude because YARN's 3.4.5 ZK version is incompatbile with Kafka's 3.3.4.
      exclude module: 'zookeeper'
    }
    compile("org.apache.hadoop:hadoop-hdfs:$yarn3Version") {
      exclude module: 'servlet-api'
    }
    compile("org.scalatra:scalatra_$scalaSuffix:$scalatraVersion") {
      exclude module: 'scala-compiler'
      exclude module: 'slf4j-api'
    }
    compile("org.scalatra:scalatra-scalate_$scalaSuffix:$scalatraVersion") {
      exclude module: 'scala-compiler'
      exclude module: 'slf4j-api'
    }
    compile "joda-time:joda-time:$jodaTimeVersion"
    compile "org.apache.zookeeper:zookeeper:$zookeeperVersion"
    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-core:$mockitoVersion"
    testCompile project(":samza-core_$scalaSuffix").sourceSets.test.output
    testCompile "org.scalatest:scalatest_$scalaSuffix:$scalaTestVersion"
  }

  repositories {
    maven {
      url "https://repo.typesafe.com/typesafe/releases"
    }
  }

  lesscss {
    source = fileTree('src/main/less') {
      include 'main.less'
    }
    dest = "$buildDir/resources/main/scalate/css"
  }

  jar.dependsOn("lesscss")
}

project(":samza-shell") {
  apply plugin: 'java'

  configurations {
    gradleShell
  }

  dependencies {
    gradleShell project(":samza-core_$scalaSuffix")
    gradleShell project(":samza-kafka_$scalaSuffix")
    gradleShell project(":samza-test_$scalaSuffix")
    gradleShell project(":samza-yarn_$scalaSuffix")
  }

  task shellTarGz(type: Tar) {
    compression = Compression.GZIP
    classifier = 'dist'
    from 'src/main/bash'
    from 'src/main/resources'
    from 'src/main/visualizer'
  }

  artifacts {
    archives(shellTarGz) {
      name 'samza-shell'
      classifier 'dist'
    }
  }

  // Usage: ./gradlew samza-shell:runJob \
  //  -PconfigPath=/path/to/job/config.properties
  task runJob(type:JavaExec) {
    description 'To run a job (defined in a properties file)'
    main = 'org.apache.samza.job.JobRunner'
    classpath = configurations.gradleShell
    if (project.hasProperty('configPath')) args += [
        '--config', 'job.config.loader.factory=org.apache.samza.config.loaders.PropertiesConfigLoaderFactory',
        '--config', 'job.config.loader.properties.path=' + configPath]
    jvmArgs = ["-Dlog4j.configurationFile=file:src/main/resources/log4j2-console.xml"]
  }

  // Usage: ./gradlew samza-shell:checkpointTool \
  //  -PconfigPath=/path/to/job/config.properties -PnewOffsets=/path/to/new/offsets.properties
  task checkpointTool(type:JavaExec) {
    description 'Command-line tool to inspect and manipulate the job’s checkpoint'
    main = 'org.apache.samza.checkpoint.CheckpointTool'
    classpath = configurations.gradleShell
    if (project.hasProperty('configPath')) args += [
        '--config', 'job.config.loader.factory=org.apache.samza.config.loaders.PropertiesConfigLoaderFactory',
        '--config', 'job.config.loader.properties.path=' + configPath]
    if (project.hasProperty('newOffsets')) args += ['--new-offsets', newOffsets]
    jvmArgs = ["-Dlog4j.configurationFile=file:src/main/resources/log4j2-console.xml"]
  }

  // Usage: ./gradlew samza-shell:kvPerformanceTest
  //  -PconfigPath=/path/to/job/config.properties
  task kvPerformanceTest(type:JavaExec) {
    description 'Command-line tool to run key-value performance tests'
    main = 'org.apache.samza.test.performance.TestKeyValuePerformance'
    classpath = configurations.gradleShell
    if (project.hasProperty('configPath')) args += [
        '--config', 'job.config.loader.factory=org.apache.samza.config.loaders.PropertiesConfigLoaderFactory',
        '--config', 'job.config.loader.properties.path=' + configPath]
    jvmArgs = ["-Dlog4j.configurationFile=file:src/main/resources/log4j2-console.xml"]
  }
}

project(":samza-shell-yarn3") {
  apply plugin: 'java'

  configurations {
    gradleShell
  }

  dependencies {
    gradleShell project(":samza-core_$scalaSuffix")
    gradleShell project(":samza-kafka_$scalaSuffix")
    gradleShell project(":samza-test_$scalaSuffix")
    gradleShell project(":samza-yarn3_$scalaSuffix")
  }

  task shellTarGz(type: Tar) {
    compression = Compression.GZIP
    classifier = 'dist'
    from(project(":samza-shell").file("./src/main/bash"))
    from(project(":samza-shell").file("./src/main/resources"))
    from(project(":samza-shell").file("./src/main/visualizer"))
  }

  artifacts {
    archives(shellTarGz) {
      name 'samza-shell-yarn3'
      classifier 'dist'
    }
  }

  // Usage: ./gradlew samza-shell:runJob \
  //  -PconfigPath=/path/to/job/config.properties
  task runJob(type:JavaExec) {
    description 'To run a job (defined in a properties file)'
    main = 'org.apache.samza.job.JobRunner'
    classpath = configurations.gradleShell
    if (project.hasProperty('configPath')) args += [
            '--config', 'job.config.loader.factory=org.apache.samza.config.loaders.PropertiesConfigLoaderFactory',
            '--config', 'job.config.loader.properties.path=' + configPath]
    jvmArgs = ["-Dlog4j.configurationFile=file:src/main/resources/log4j2-console.xml"]
  }

  // Usage: ./gradlew samza-shell:checkpointTool \
  //  -PconfigPath=/path/to/job/config.properties -PnewOffsets=/path/to/new/offsets.properties
  task checkpointTool(type:JavaExec) {
    description 'Command-line tool to inspect and manipulate the job’s checkpoint'
    main = 'org.apache.samza.checkpoint.CheckpointTool'
    classpath = configurations.gradleShell
    if (project.hasProperty('configPath')) args += [
            '--config', 'job.config.loader.factory=org.apache.samza.config.loaders.PropertiesConfigLoaderFactory',
            '--config', 'job.config.loader.properties.path=' + configPath]
    if (project.hasProperty('newOffsets')) args += ['--new-offsets', newOffsets]
    jvmArgs = ["-Dlog4j.configurationFile=file:src/main/resources/log4j2-console.xml"]
  }

  // Usage: ./gradlew samza-shell:kvPerformanceTest
  //  -PconfigPath=/path/to/job/config.properties
  task kvPerformanceTest(type:JavaExec) {
    description 'Command-line tool to run key-value performance tests'
    main = 'org.apache.samza.test.performance.TestKeyValuePerformance'
    classpath = configurations.gradleShell
    if (project.hasProperty('configPath')) args += [
            '--config', 'job.config.loader.factory=org.apache.samza.config.loaders.PropertiesConfigLoaderFactory',
            '--config', 'job.config.loader.properties.path=' + configPath]
    jvmArgs = ["-Dlog4j.configurationFile=file:src/main/resources/log4j2-console.xml"]
  }
}

project(":samza-kv_$scalaSuffix") {
  apply plugin: 'scala'

  // Force scala joint compilation
  sourceSets.main.scala.srcDir "src/main/java"
  sourceSets.test.scala.srcDir "src/test/java"

  // Disable the Javac compiler by forcing joint compilation by scalac. This is equivalent to setting
  // tasks.compileTestJava.enabled = false
  sourceSets.main.java.srcDirs = []
  sourceSets.test.java.srcDirs = []


  dependencies {
    compile project(':samza-api')
    compile project(":samza-core_$scalaSuffix")
    compile "org.scala-lang:scala-library:$scalaVersion"
    testCompile "com.google.guava:guava:$guavaVersion"
    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-core:$mockitoVersion"
  }
}

project(":samza-kv-inmemory_$scalaSuffix") {
  apply plugin: 'java'

  dependencies {
    compile project(':samza-api')
    compile project(":samza-core_$scalaSuffix")
    compile project(":samza-kv_$scalaSuffix")
    compile "com.google.guava:guava:$guavaVersion"
    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-core:$mockitoVersion"
  }
}

project(":samza-kv-rocksdb_$scalaSuffix") {
  apply plugin: 'scala'

  // Force scala joint compilation
  sourceSets.main.scala.srcDir "src/main/java"
  sourceSets.test.scala.srcDir "src/test/java"

  // Disable the Javac compiler by forcing joint compilation by scalac. This is equivalent to setting
  // tasks.compileTestJava.enabled = false
  sourceSets.main.java.srcDirs = []
  sourceSets.test.java.srcDirs = []

  dependencies {
    compile project(':samza-api')
    compile project(":samza-core_$scalaSuffix")
    compile project(":samza-kv_$scalaSuffix")
    compile "org.scala-lang:scala-library:$scalaVersion"
    compile "org.rocksdb:rocksdbjni:$rocksdbVersion"
    testCompile "junit:junit:$junitVersion"
    testCompile "org.scalatest:scalatest_$scalaSuffix:$scalaTestVersion"
  }
}

project(":samza-kv-couchbase_$scalaSuffix") {
  apply plugin: 'java'

  dependencies {
    compile project(':samza-api')
    compile project(":samza-core_$scalaSuffix")
    compile "com.couchbase.client:java-client:$couchbaseClientVersion"
    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-core:$mockitoVersion"
    testCompile "org.powermock:powermock-api-mockito:$powerMockVersion"
    testCompile "org.powermock:powermock-core:$powerMockVersion"
    testCompile "org.powermock:powermock-module-junit4:$powerMockVersion"
  }
}

project(":samza-hdfs_$scalaSuffix") {
  apply plugin: 'scala'

  // Force scala joint compilation
  sourceSets.main.scala.srcDir "src/main/java"
  sourceSets.test.scala.srcDir "src/test/java"
  sourceSets.main.java.srcDirs = []

  dependencies {
    compile project(':samza-api')
    compile project(":samza-core_$scalaSuffix")
    // currently hdfs system producer/consumer do depend on yarn for two things:
    // 1. staging directory 2. security
    // SAMZA-1032 to solve the staging directory dependency
    compile project(":samza-yarn_$scalaSuffix")
    compile "org.scala-lang:scala-library:$scalaVersion"
    compile("org.apache.hadoop:hadoop-hdfs:$yarnVersion") {
      exclude module: 'servlet-api'
    }
    compile("org.apache.hadoop:hadoop-common:$yarnVersion") {
      exclude module: 'servlet-api'
      exclude module: 'zookeeper'
    }

    testCompile "junit:junit:$junitVersion"
    testCompile "org.scalatest:scalatest_$scalaSuffix:$scalaTestVersion"
    testCompile "org.apache.hadoop:hadoop-minicluster:$yarnVersion"
    testCompile "org.apache.hadoop:hadoop-hdfs:$yarnVersion:tests"
  }
}

project(":samza-rest_$scalaSuffix") {
  apply plugin: 'java'

  dependencies {
    compile project(":samza-shell")
    compile project(":samza-core_$scalaSuffix")

    runtime "org.slf4j:slf4j-log4j12:$slf4jVersion"
    compile "com.google.guava:guava:$guavaVersion"
    compile "org.glassfish.jersey.core:jersey-server:$jerseyVersion"
    compile "org.glassfish.jersey.containers:jersey-container-servlet-core:$jerseyVersion"
    compile "org.glassfish.jersey.containers:jersey-container-jetty-http:$jerseyVersion"
    compile "org.glassfish.jersey.media:jersey-media-moxy:$jerseyVersion"
    compile "org.eclipse.jetty.aggregate:jetty-all:$jettyVersion"
    compile "commons-httpclient:commons-httpclient:$commonsHttpClientVersion"
    compile "com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:$jacksonVersion"
    compile("org.apache.hadoop:hadoop-yarn-client:$yarnVersion") {
      exclude module: 'servlet-api'
      exclude group: 'com.sun.jersey'
    }
    runtime("org.apache.hadoop:hadoop-yarn-api:$yarnVersion") {
      exclude module: 'servlet-api'
      exclude group: 'com.sun.jersey'
    }

    testCompile "junit:junit:$junitVersion"
    testCompile "org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-grizzly2:$jerseyVersion"
    testCompile "org.mockito:mockito-core:$mockitoVersion"
  }

  tasks.create(name: "releaseRestServiceTar", type: Tar) {
    description 'Build a tarball containing the samza-rest component and its dependencies'
    compression = Compression.GZIP
    from(file("$projectDir/src/main/config")) { into "config/" }
    from(file("$projectDir/src/main/resources/log4j2.xml")) { into "bin/" }
    from(file("$projectDir/src/main/bash/run-samza-rest-service.sh")) { into "bin/" }
    from(project(':samza-shell').file("src/main/bash/run-class.sh")) { into "bin/" }
    from '../LICENSE'
    from '../NOTICE'
    from(configurations.runtime) { into("lib/") }
    from(jar) { into("lib/") }
  }

  tasks.create(name: "restTarGz", type: Tar) {
    description 'Build a tarball containing the samza-rest supplementary files'
    compression = Compression.GZIP
    from 'src/main/bash'
    from 'src/main/resources'
    from(project(':samza-shell').file("src/main/bash/run-class.sh"))
  }

  artifacts {
    archives(restTarGz) {
      name 'samza-rest-scripts'
      classifier 'dist'
    }
  }
}

project(":samza-test_$scalaSuffix") {
  apply plugin: 'scala'

  // Force scala joint compilation
  sourceSets.main.scala.srcDir "src/main/java"
  sourceSets.test.scala.srcDir "src/test/java"

  // Disable the Javac compiler by forcing joint compilation by scalac. This is equivalent to setting
  // tasks.compileTestJava.enabled = false
  sourceSets.main.java.srcDirs = []
  sourceSets.test.java.srcDirs = []

  // Disabling assertions while running test because of a calcite bug in {@link RelOptUtil#eq} method
  // it checks for the type1 != type2 rather than !type1.equals(type2)
  tasks.withType(Test) {
    enableAssertions = false
  }

  configurations {
    // Remove transitive dependencies from Zookeeper that we don't want.
    compile.exclude group: 'javax.jms', module: 'jms'
    compile.exclude group: 'com.sun.jdmk', module: 'jmxtools'
    compile.exclude group: 'com.sun.jmx', module: 'jmxri'
  }

  dependencies {
    compile project(':samza-api')
    compile project(":samza-kv-inmemory_$scalaSuffix")
    compile project(":samza-kv-couchbase_$scalaSuffix")
    compile project(":samza-kv-rocksdb_$scalaSuffix")
    compile project(":samza-core_$scalaSuffix")
    compile project(":samza-kafka_$scalaSuffix")
    compile project(":samza-sql_$scalaSuffix")
    runtime project(":samza-log4j2_$scalaSuffix")
    runtime project(":samza-yarn_$scalaSuffix")
    runtime project(":samza-hdfs_$scalaSuffix")
    compile "org.scala-lang:scala-library:$scalaVersion"
    compile "net.sf.jopt-simple:jopt-simple:$joptSimpleVersion"
    compile "javax.mail:mail:1.4"
    compile "org.apache.kafka:kafka_$scalaSuffix:$kafkaVersion"
    compile "junit:junit:$junitVersion"
    compile "org.hamcrest:hamcrest-all:$hamcrestVersion"
    testCompile "org.apache.kafka:kafka_$scalaSuffix:$kafkaVersion:test"
    testCompile "org.apache.helix:zookeeper-api:$helixVersion"
    testCompile "org.xerial.snappy:snappy-java:$snappyVersion"
    testCompile project(":samza-kafka_$scalaSuffix")
    testCompile "org.apache.kafka:kafka_$scalaSuffix:$kafkaVersion:test"
    testCompile "org.apache.kafka:kafka-clients:$kafkaVersion:test"
    testCompile project(":samza-core_$scalaSuffix").sourceSets.test.output
    testCompile project(":samza-sql_$scalaSuffix").sourceSets.test.output
    testCompile "org.scalatest:scalatest_$scalaSuffix:$scalaTestVersion"
    testCompile "org.mockito:mockito-core:$mockitoVersion"
    testCompile "com.couchbase.mock:CouchbaseMock:$couchbaseMockVersion"
    testRuntime "org.slf4j:slf4j-simple:$slf4jVersion"
  }

  test {
    // Bump up the heap so we can run KV store tests.
    minHeapSize = "1560m"
    maxHeapSize = "1560m"
    jvmArgs = ["-XX:+UseConcMarkSweepGC", "-server"]
  }

  tasks.create(name: "releaseTestJobs", dependsOn: configurations.archives.artifacts, type: Tar) {
    description 'Build an integration test tarball'
    compression = Compression.GZIP
    from(file("$projectDir/src/main/config")) { into "config/" }
    from(file("$projectDir/src/main/resources")) { into "lib/" }
    from(project(':samza-shell').file("src/main/bash")) { into "bin/" }
    from(file("$projectDir/src/main/python/ghostface_killah.py")) { into "bin/"}
    from(project(':samza-shell').file("src/main/resources")) { into "lib/" }
    from(project(':samza-shell').file("src/main/resources/log4j2-console.xml")) { into "bin/" }
    from '../LICENSE'
    from '../NOTICE'
    from(configurations.runtime) { into("lib/") }
    from(configurations.archives.artifacts.files) { into("lib/") }
  }
}

// SAMZA-2473 read wrapper version from gradle.properties
wrapper {
  gradleVersion = project.gradleVersion
}

