buildscript {

  repositories {
    mavenCentral()
    gradlePluginPortal()
  }

  dependencies {
    classpath('io.spring.gradle:dependency-management-plugin:1.1.6')
    classpath('org.springframework.boot:spring-boot-gradle-plugin:3.3.5')
  }
}

plugins {
  id 'org.sonarqube' version '5.1.0.4882'   // https://plugins.gradle.org/plugin/org.sonarqube
  id 'jacoco'
  id 'com.github.spotbugs' version '6.0.26' // https://plugins.gradle.org/plugin/com.github.spotbugs
}

buildScan {
  termsOfServiceUrl = 'https://gradle.com/terms-of-service'
  termsOfServiceAgree = 'yes'
}

ext.versions = [
    springBootVersion        : '3.3.5',
    springCloudVersion       : '4.1.4',

    // Should be same as dependency used by springBootVersion
    springCoreVersion        : '6.1.14',

    hibernateVaildatorVersion: '8.0.1.Final',
    jjwtVersion              : '0.11.5'
]

ext.libraries = [
    spring_boot_starter                     : dependencies.create("org.springframework.boot:spring-boot-starter:" + ext.versions.springBootVersion) {
      exclude module: "spring-boot-starter-logging"
    },
    spring_boot_starter_log4j2              : dependencies.create("org.springframework.boot:spring-boot-starter-log4j2:" + ext.versions.springBootVersion),
    spring_boot_starter_web                 : dependencies.create("org.springframework.boot:spring-boot-starter-web:" + ext.versions.springBootVersion) {
      exclude module: "spring-boot-starter-logging"
    },
    spring_boot_starter_data_jpa            : dependencies.create("org.springframework.boot:spring-boot-starter-data-jpa:" + ext.versions.springBootVersion) {
      exclude module: "spring-boot-starter-logging"
    },
    spring_boot_starter_data_rest           : dependencies.create("org.springframework.boot:spring-boot-starter-data-rest:" + ext.versions.springBootVersion) {
      exclude module: "spring-boot-starter-logging"
    },
    spring_boot_starter_actuator            : dependencies.create("org.springframework.boot:spring-boot-starter-actuator:" + ext.versions.springBootVersion) {
      exclude module: "spring-boot-starter-logging"
    },
    spring_boot_starter_security            : dependencies.create("org.springframework.boot:spring-boot-starter-security:" + ext.versions.springBootVersion) {
      exclude module: "spring-boot-starter-logging"
    },
    spring_cloud_starter                    : dependencies.create("org.springframework.cloud:spring-cloud-starter:" + ext.versions.springCloudVersion) {
      exclude module: "spring-boot-starter-logging"
    },

    spring_tx                               : dependencies.create("org.springframework:spring-tx:" + ext.versions.springCoreVersion),

    jjwt_api                                : dependencies.create("io.jsonwebtoken:jjwt-api:" + ext.versions.jjwtVersion),
    jjwt_impl                               : dependencies.create("io.jsonwebtoken:jjwt-impl:" + ext.versions.jjwtVersion),
    jjwt_jackson                            : dependencies.create("io.jsonwebtoken:jjwt-jackson:" + ext.versions.jjwtVersion),

    google_guava                            : dependencies.create("com.google.guava:guava:33.3.1-jre"),
    google_gson                             : dependencies.create("com.google.code.gson:gson:2.11.0"),
    h2                                      : dependencies.create("com.h2database:h2:2.3.232"),

    jakarta_mail_api                        : dependencies.create("jakarta.mail:jakarta.mail-api:2.1.3"),
    jakarta_mail_sun                        : dependencies.create("org.eclipse.angus:angus-mail:2.0.3"),

    jakarta_xml_api                         : dependencies.create("jakarta.xml.bind:jakarta.xml.bind-api:4.0.2"),

    snake_yaml                              : dependencies.create("org.yaml:snakeyaml:2.2"),

    springdoc_openapi_ui                    : dependencies.create("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0"),

    lombok                                  : dependencies.create("org.projectlombok:lombok:1.18.34"),

    validation_api                          : dependencies.create("jakarta.validation:jakarta.validation-api:3.0.2"),
    hibernate_validator                     : dependencies.create("org.hibernate.validator:hibernate-validator:" + ext.versions.hibernateVaildatorVersion),
    hibernate_validator_annotation_processor: dependencies.create("org.hibernate.validator:hibernate-validator-annotation-processor:" + ext.versions.hibernateVaildatorVersion),

    powermock_junit                         : dependencies.create("org.powermock:powermock-module-junit4:2.0.9"),
    powermock_api_easymock                  : dependencies.create("org.powermock:powermock-api-easymock:2.0.9"),
    easymock                                : dependencies.create("org.easymock:easymock:5.4.0"),

    // JUnit 4 still as Powermock does not play with Junit 5: https://github.com/powermock/powermock/issues/929
    junit_vintage_engine                    : dependencies.create("org.junit.vintage:junit-vintage-engine:5.11.3"),

    spring_boot_starter_test                : dependencies.create("org.springframework.boot:spring-boot-starter-test:" + ext.versions.springBootVersion) {
      exclude module: "spring-boot-starter-logging"
    },
    spring_security_test                    : dependencies.create("org.springframework.security:spring-security-test:6.3.4"),
    awaitility                              : dependencies.create("org.awaitility:awaitility:4.2.2")
]

allprojects {

  apply plugin: 'application'
  apply plugin: 'io.spring.dependency-management'
  apply plugin: 'maven-publish'
  apply plugin: 'checkstyle'
  apply plugin: 'jacoco'
  apply plugin: 'com.github.spotbugs'
  apply plugin: 'org.sonarqube'

  group = 'com.gazbert.bxbot'
  version = '2.2.2-SNAPSHOT'

  dependencyManagement {
    applyMavenExclusions = false
  }
}

subprojects {

  apply plugin: 'java'

  sourceCompatibility = 1.21
  targetCompatibility = 1.21

  repositories {
    mavenCentral()
  }

  test {
    testLogging {
      events "passed", "skipped", "failed"
    }
  }

  javadoc {
    include "**/api/**"
  }

  checkstyle {
    toolVersion "10.15.0"
    configFile = file("${rootDir}/etc/google_checks.xml")
    configProperties = ["org.checkstyle.google.suppressionfilter.config": file("${rootDir}/etc/checkstyle-suppressions.xml")]
    ignoreFailures = false
    showViolations = true
    maxErrors = 0
    maxWarnings = 0
  }
  checkstyleMain {
    source = 'src/main/java'
  }
  checkstyleTest {
    source = 'src/test/java'
  }

  jacoco {
    toolVersion = "0.8.12" // https://docs.gradle.org/current/userguide/jacoco_plugin.html
  }
  jacocoTestReport {
    reports {
      xml.required = true
      csv.required = false
      html.required = true
    }
  }
  test.finalizedBy jacocoTestReport
  check.dependsOn jacocoTestCoverageVerification

  spotbugs {
    ignoreFailures = false
    excludeFilter = file("${rootDir}/etc/spotbugs-exclude-filter.xml")
    reportsDir = file("$project.buildDir/reports/spotbugs")
    effort = com.github.spotbugs.snom.Effort.MAX
  }
  spotbugsMain {
    reports {
      html {
        required = true
      }
    }
  }
  tasks {
    spotbugsTest {
      onlyIf { false }
    }
  }
}

sonarqube {
  properties {
    property 'sonar.host.url', 'https://sonarcloud.io'
    property 'sonar.organization', 'gazbert-github'
    property 'sonar.projectKey', 'gazbert_bxbot'
  }
}

// TODO: Strip out common stuff into separate method?
task buildZipDist(type: Zip) {

  archiveBaseName = 'bxbot-app'
  archiveExtension = 'zip'

  into("bxbot-app-$project.version") {

    into('libs') {
      from 'bxbot-app/build/libs'
      include "bxbot-app-${project.version}.jar" //copy the fat jar created by bootRepackage
    }

    into('') {
      from 'README.md'
    }

    into('') {
      from 'CONTRIBUTING.md'
    }

    into('') {
      from 'LICENSE'
    }

    into('') {
      from '3rd-party-licenses.md'
    }

    into('') {
      from 'bxbot.sh'
    }

    into('') {
      from 'bxbot.bat'
    }

    from('config/application.properties') {
      into 'config'
    }

    into('config') {
      from 'config'
      include('*.xml')
      include('*.yaml')
    }
  }
}

// TODO: Strip out common stuff into separate method?
task buildTarGzipDist(type: Tar) {

  archiveBaseName = 'bxbot-app'
  archiveExtension = 'tar.gz'
  compression = Compression.GZIP

  into("bxbot-app-$project.version") {

    into('libs') {
      from 'bxbot-app/build/libs'
      include "bxbot-app-${project.version}.jar" //copy the fat jar created by bootRepackage
    }

    into('') {
      from 'README.md'
    }

    into('') {
      from 'CONTRIBUTING.md'
    }

    into('') {
      from 'LICENSE'
    }

    into('') {
      from '3rd-party-licenses.md'
    }

    into('') {
      from 'bxbot.sh'
    }

    into('') {
      from 'bxbot.bat'
    }

    from('config/application.properties') {
      into 'config'
    }

    into('config') {
      from 'config'
      include('*.xml')
      include('*.yaml')
    }
  }
}

