plugins { 
    id 'org.springframework.boot' version '3.1.0'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'io.freefair.lombok' version '8.1.0'
    id 'io.freefair.maven-publish-java' version '8.1.0'
    id 'org.owasp.dependencycheck' version '8.3.1'
    id 'org.asciidoctor.jvm.convert' version '3.3.2'
    id 'net.ltgt.errorprone' version '3.1.0'
    id 'net.researchgate.release' version '3.0.2'
    id 'com.gorylenko.gradle-git-properties' version '2.4.1'
    id 'java'
    id 'jacoco'
}

description = 'General purpose metadata repository and schema registry service'
group = 'edu.kit.datamanager'

ext {
    // versions of dependencies
    springDocVersion  = '2.1.0'
    javersVersion     = '7.0.0'
    keycloakVersion   = '19.0.0'
    errorproneVersion = '2.20.0'
    // directory for generated code snippets during tests
    snippetsDir = file("build/generated-snippets")
}

println "Running gradle version: $gradle.gradleVersion"
println "Building ${name} version: ${version}"
println "JDK version: ${JavaVersion.current()}"

repositories {
    mavenLocal() 
    mavenCentral()
}

sourceCompatibility = 1.17
targetCompatibility = 1.17
 
if (System.getProperty('profile') == 'minimal') {
    println 'Using minimal profile for building ' + project.getName()
    apply from: 'gradle/profile-minimal.gradle'   
} else {
    println 'Using default profile executing all tests for building ' + project.getName()
    apply from: 'gradle/profile-complete.gradle'
}

dependencies {
    // Spring
    implementation 'org.springframework:spring-messaging:6.0.2'
    implementation 'org.springframework.cloud:spring-cloud-gateway-mvc:4.0.6'

    // Spring Boot
    implementation "org.springframework.boot:spring-boot-starter-data-rest"
    implementation "org.springframework.boot:spring-boot-starter-amqp"
    implementation "org.springframework.boot:spring-boot-starter-web"
    implementation "org.springframework.boot:spring-boot-starter-security"
    implementation "org.springframework.boot:spring-boot-starter-actuator"
    implementation "org.springframework.boot:spring-boot-starter-data-jpa"
   
    // boot starter
    implementation "org.springframework.boot:spring-boot-starter-validation"    
    implementation "org.springframework.boot:spring-boot-starter-mail"
    
    // JSON Parser for Metadata Editor
    implementation 'com.googlecode.json-simple:json-simple:1.1.1'

    
    // cloud support
    implementation "org.springframework.cloud:spring-cloud-starter-config:4.0.3"
    implementation "org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:4.0.2"

    // springdoc
    implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:${springDocVersion}"
    implementation "org.springdoc:springdoc-openapi-starter-common:${springDocVersion}"
    implementation "org.springdoc:springdoc-openapi-starter-webmvc-api:${springDocVersion}"

    // CLI Parser
    implementation "com.beust:jcommander:1.82"

    // apache
    implementation "commons-configuration:commons-configuration:1.10"
    implementation "org.apache.commons:commons-collections4:4.4"
    // includes commons-lang3
    implementation "org.apache.commons:commons-text:1.10.0"

    // UI (website)
    implementation "org.springframework.boot:spring-boot-starter-thymeleaf"

    // javers
    implementation "org.javers:javers-spring-boot-starter-sql:${javersVersion}"
    implementation "org.javers:javers-core:${javersVersion}"

    // driver for postgres
    implementation "org.postgresql:postgresql:42.6.0"
    //driver for h2
    implementation "com.h2database:h2:2.1.214"
    
    // apache
    implementation "commons-io:commons-io:2.13.0"
    implementation "org.apache.tika:tika-core:2.8.0"

    // JSON validator
    implementation "com.networknt:json-schema-validator:1.0.85"
  
    // XML validator // https://mvnrepository.com/artifact/xerces/xercesImpl
    implementation 'xerces:xercesImpl:2.12.2'

    // datamanager
    implementation "edu.kit.datamanager:repo-core:1.2.1"
    implementation "edu.kit.datamanager:service-base:1.2.0"

    // elasticsearch (since service-base 1.1.0)
    implementation "org.springframework.data:spring-data-elasticsearch:5.1.0"

    // DOIP SDK
    implementation "net.dona.doip:doip-sdk:2.2.0"

    runtimeOnly    "org.apache.httpcomponents:httpclient:4.5.14"
    
    // Additional libraries for tests
    testImplementation "org.springframework.restdocs:spring-restdocs-mockmvc:3.0.0"
    testImplementation "org.springframework.boot:spring-boot-starter-test"
    testImplementation "org.springframework:spring-test"
    testImplementation "org.springframework.security:spring-security-test"
   
    //Java 11 Support 
    testImplementation "org.mockito:mockito-core:5.4.0"
    testImplementation "junit:junit:4.13.2"
    testImplementation "com.github.stefanbirkner:system-lambda:1.2.1"

    // errorprone
    errorprone "com.google.errorprone:error_prone_core:${errorproneVersion}"
}

compileJava {
//    options.errorprone.disableAllWarnings = true
    options.errorprone.disableWarningsInGeneratedCode = true
    options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" << "-Xmaxwarns" << "200"
}

compileTestJava {
    // disable errorprone for tests
    options.errorprone.disableWarningsInGeneratedCode = true
    options.errorprone.enabled = false // change it to true to enable
}

test {
    finalizedBy jacocoTestReport
    environment "spring.config.location", "classpath:/test-config/"
    
    maxHeapSize = "8192m"
    testLogging {
        outputs.upToDateWhen {false}
        showStandardStreams = true
    }
}

tasks.withType(Test) {
    testLogging {
        events 'started', 'passed'
    }
}

jacoco {
    toolVersion = "0.8.10"
}

import java.text.SimpleDateFormat

tasks.register('testForSnippetsDir') {
  doFirst {
    println 'snippetsDir exists: ' + snippetsDir.exists()
    if (!snippetsDir.exists()) {
      println 'Create snippets dir...'
      println 'WARNING: Don\'t skip tests for building production ready jar file!'
      snippetsDir.mkdirs()
    }
  }
} 

asciidoctor {
  dependsOn testForSnippetsDir
     attributes "snippets": snippetsDir,
               "version":  jar.archiveVersion,
               "date":     new SimpleDateFormat("yyyy-MM-dd").format(new Date())
    inputs.dir snippetsDir
    dependsOn test  
    sourceDir "docs/"
    outputDir "build/docs/html5"
    inProcess = JAVA_EXEC
    forkOptions {
      jvmArgs "--add-opens", "java.base/sun.nio.ch=ALL-UNNAMED", "--add-opens", "java.base/java.io=ALL-UNNAMED"
    }
}

jar {
    // disable plain jar file
    enabled = false
}
   
springBoot {    
    buildInfo()
}

bootJar {
    println 'Create bootable jar...'

    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    manifest {
        attributes 'Main-Class': 'org.springframework.boot.loader.PropertiesLauncher'
    }
    dependsOn asciidoctor
    from ("${asciidoctor.outputDir}") {
        into 'static/docs'
    }
    launchScript()
}

release {
    tagTemplate = 'v${version}'
}

// task for printing project name.
task printProjectName {
    doLast {
        println "${project.name}"
    }
}
