/*
 * 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 {
        jcenter() // this applies only to the Gradle 'Shadow' plugin
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.1'
        // classpath 'com.github.sherter.google-java-format:0.7.1' TODO check if Dummy.java is needed if added
    }
}

plugins {
    id 'java'
    id 'application'
    // shadow plugin to produce fat JARs
    id 'com.github.johnrengelman.shadow' version '4.0.1'
    id "com.github.sherter.google-java-format" version "0.8"
}

ext {
    javaVersion = '1.8'
    flinkVersion = '1.9.0'
    scalaBinaryVersion = '2.11'
    slf4jVersion = '1.7.7'
    log4jVersion = '2.10.0'
    scalaVersion = '2.11'
    mockitoVersion = '1.10.19'
    junitVersion = '4.12'
}

// artifact properties
group = 'com.ververica.field.dynamicrules'
version = '1.2'
jar.archiveName = "${jar.baseName}-${version}-flink-${flinkVersion}.${jar.extension}"
mainClassName = 'com.ververica.field.dynamicrules.Main'
description = """Dynamic Fraud Detection Demo"""

sourceCompatibility = javaVersion
targetCompatibility = javaVersion
tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

applicationDefaultJvmArgs = ["-Dlog4j.configuration=log4j.properties"]

wrapper {
    gradleVersion = '4.4'
}

// declare where to find the dependencies of your project
repositories {
    mavenCentral()
    maven { url "https://repository.apache.org/content/repositories/snapshots/" }
}

// NOTE: We cannot use "compileOnly" or "shadow" configurations since then we could not run code
// in the IDE or with "gradle run". We also cannot exclude transitive dependencies from the
// shadowJar yet (see https://github.com/johnrengelman/shadow/issues/159).
// -> Explicitly define the // libraries we want to be included in the "flinkShadowJar" configuration!
configurations {
    flinkShadowJar // dependencies which go into the shadowJar

    // always exclude these (also from transitive dependencies) since they are provided by Flink
    flinkShadowJar.exclude group: 'org.apache.flink', module: 'force-shading'
    flinkShadowJar.exclude group: 'com.google.code.findbugs', module: 'jsr305'
    flinkShadowJar.exclude group: 'org.slf4j'
    flinkShadowJar.exclude group: 'log4j'
}

// declare the dependencies for your production and test code
dependencies {
    // --------------------------------------------------------------
    // Compile-time dependencies that should NOT be part of the
    // shadow jar and are provided in the lib folder of Flink
    // --------------------------------------------------------------
    compile "org.apache.flink:flink-java:${flinkVersion}"
    compile "org.apache.flink:flink-streaming-java_${scalaBinaryVersion}:${flinkVersion}"
    compile "org.apache.flink:flink-runtime-web_${scalaBinaryVersion}:${flinkVersion}"

    // --------------------------------------------------------------
    // Dependencies that should be part of the shadow jar, e.g.
    // connectors. These must be in the flinkShadowJar configuration!
    // --------------------------------------------------------------
    //flinkShadowJar "org.apache.flink:flink-connector-kafka-0.11_${scalaBinaryVersion}:${flinkVersion}"



    compile "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion"
    compile "org.apache.logging.log4j:log4j-api:$log4jVersion"
    compile "org.apache.logging.log4j:log4j-core:$log4jVersion"
    compile "org.apache.logging.log4j:log4j-jcl:$log4jVersion"


    // Test dependencies here.
    // testCompile "junit:junit:4.12"

    compile "junit:junit:$junitVersion"
    compile "org.apache.flink:flink-test-utils_$scalaVersion:$flinkVersion"
    compile "org.apache.flink:flink-streaming-java_$scalaVersion:$flinkVersion:tests"
    compile "org.apache.flink:flink-runtime_$scalaVersion:$flinkVersion:tests"
    compile "org.mockito:mockito-all:$mockitoVersion"

    //Project dependencies
    compile "org.projectlombok:lombok:1.18.4"
//    For Gradle 6 use:
//    compileOnly 'org.projectlombok:lombok:1.18.10'
//    annotationProcessor 'org.projectlombok:lombok:1.18.10'

    flinkShadowJar "org.apache.flink:flink-connector-kafka-0.11_$scalaBinaryVersion:$flinkVersion"
    flinkShadowJar "org.apache.flink:flink-connector-gcp-pubsub_$scalaBinaryVersion:$flinkVersion"
    flinkShadowJar group: 'com.google.cloud', name: 'google-cloud-pubsub', version: '1.51.0'
}

// make compileOnly dependencies available for tests:
sourceSets {
    main.compileClasspath += configurations.flinkShadowJar
    main.runtimeClasspath += configurations.flinkShadowJar

    test.compileClasspath += configurations.flinkShadowJar
    test.runtimeClasspath += configurations.flinkShadowJar

    javadoc.classpath += configurations.flinkShadowJar
}

run.classpath = sourceSets.main.runtimeClasspath

jar {
    manifest {
        attributes 'Built-By': System.getProperty('user.name'),
                'Build-Jdk': System.getProperty('java.version')
    }
}

configurations { all*.exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j' }

shadowJar {
    configurations = [project.configurations.flinkShadowJar]
    archiveName = "${jar.baseName}-${version}-flink-${flinkVersion}-deploy.${jar.extension}"
}

task resolveDependencies {
    doLast {
        project.rootProject.allprojects.each { subProject ->
            subProject.buildscript.configurations.each { configuration ->
                if (configuration.canBeResolved) configuration.resolve()
            }
            subProject.configurations.each { configuration ->
                if (configuration.canBeResolved) configuration.resolve()
            }
        }
    }
}
