buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.owasp:dependency-check-gradle:${owasp_plugin_version}"
    }
}
plugins {
    id 'org.springframework.boot'
    id 'io.spring.dependency-management'
    id 'java-library'
    id 'com.gorylenko.gradle-git-properties'
    id 'com.github.ben-manes.versions'
    id "com.github.sherter.google-java-format"
    id "org.sonarqube" apply false
}
apply from: "gradle/code-quality.gradle"
if (project.hasProperty("ci")) {
    apply from: "gradle/owasp.gradle"
}

group = '<%= packageName %>'
version = '<%= DEFAULT_APP_VERSION %>'
sourceCompatibility = <%= JAVA_VERSION %>
targetCompatibility = <%= JAVA_VERSION %>

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    <%_ if (features.includes('localstack')) { _%>
    implementation "io.awspring.cloud:spring-cloud-aws-autoconfigure:${spring_cloud_aws_version}"
    implementation "io.awspring.cloud:spring-cloud-aws-messaging:${spring_cloud_aws_version}"
    <%_ } _%>
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'com.h2database:h2'
    <%_ if (databaseType === 'mysql') { _%>
    runtimeOnly 'mysql:mysql-connector-java'
    <%_ } _%>
    <%_ if (databaseType === 'postgresql') { _%>
    runtimeOnly 'org.postgresql:postgresql'
    <%_ } _%>
    <%_ if (databaseType === 'mariadb') { _%>
    runtimeOnly "org.mariadb.jdbc:mariadb-java-client"
    <%_ } _%>
    <%_ if (dbMigrationTool  === 'flywaydb') { _%>
    implementation 'org.flywaydb:flyway-core'
    <%_ } _%>
    implementation "org.springdoc:springdoc-openapi-ui:${springdoc_openapi_version}"
    implementation "org.zalando:problem-spring-web-starter:${problem_spring_web_version}"
    implementation "org.apache.commons:commons-lang3:${commons_lang_version}"
    implementation "commons-io:commons-io:${commons_io_version}"

    testImplementation "org.springframework.boot:spring-boot-starter-test"
    testImplementation "org.projectlombok:lombok"
    testImplementation "org.awaitility:awaitility:${awaitility_version}"
    testImplementation "org.testcontainers:junit-jupiter"
    <%_ if (databaseType === 'mysql') { _%>
    testImplementation "org.testcontainers:mysql"
    <%_ } _%>
    <%_ if (databaseType === 'postgresql') { _%>
    testImplementation "org.testcontainers:postgresql"
    <%_ } _%>
    <%_ if (databaseType === 'mariadb') { _%>
    testImplementation "org.testcontainers:mariadb"
    <%_ } _%>
    <%_ if (features.includes('localstack')) { _%>
    testImplementation "org.testcontainers:localstack"
    <%_ } _%>
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${spring_cloud_version}"
        mavenBom "org.testcontainers:testcontainers-bom:${testcontainers_version}"
    }
}

defaultTasks "bootRun"

springBoot {
    buildInfo()
}

bootJar {
    //launchScript()
}

bootBuildImage {
    imageName = "DOCKER_USERNAME/<%= appName %>"
}

compileJava.dependsOn processResources
processResources.dependsOn bootBuildInfo

if (project.hasProperty("local")) {
    bootRun {
        args = ["--spring.profiles.active=local"]
    }
}

gitProperties {
    failOnNoGitDirectory = false
    keys = ["git.branch", "git.commit.id.abbrev", "git.commit.id.describe"]
}

googleJavaFormat {
    toolVersion = '1.15.0'
    options style: 'AOSP'
}

check.dependsOn verifyGoogleJavaFormat

test {
    useJUnitPlatform()
    exclude "**/*IT*", "**/*IntegrationTest*", "**/*IntTest*"
    testLogging {
        events = ["PASSED", "FAILED", "SKIPPED"]
        showStandardStreams = true
        exceptionFormat = "full"
    }
}

task integrationTest(type: Test) {
    useJUnitPlatform()

    include "**/*IT*", "**/*IntegrationTest*", "**/*IntTest*"
    shouldRunAfter test

    testLogging {
        events = ["PASSED", "FAILED", "SKIPPED"]
        showStandardStreams = true
        exceptionFormat = "full"
    }
}

check.dependsOn integrationTest

task testReport(type: TestReport) {
    destinationDir = file("$buildDir/reports/tests")
    reportOn test
}

task integrationTestReport(type: TestReport) {
    destinationDir = file("$buildDir/reports/tests")
    reportOn integrationTest
}
