plugins {
    id 'org.springframework.boot' version '3.2.9'
    id 'io.spring.dependency-management' version '1.1.6'
    id 'java'
    id 'jacoco'
    id 'maven-publish'
    id 'com.github.kt3k.coveralls' version '2.8.2'
}

apply plugin: 'idea'
//apply plugin: 'eclipse'


ext {

    springBootVersion = "3.2.9"
    springCloudVersion = "2023.0.3"
    mybatisStarterVersion = '3.0.3'
    springdocVersion = '1.6.12'
    lombokVersion = '1.18.34'
    ehcacheVersion = '3.8.1'
    commonsPoolVersion = '2.11.1'
    //apache commons pool. redis pool 需要 https://commons.apache.org/proper/commons-pool/
}


allprojects {
    group = 'io.github.alphajiang'
//    version = '0.0.29.20241026.1-SNAPSHOT'
    version = '0.0.28'
    apply plugin: 'java'
//    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'jacoco'
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17

    compileJava {
        options.encoding = "UTF-8"
    }


    test.onlyIf { !Boolean.getBoolean('skipTests') }

    repositories {
        maven { url 'https://maven.aliyun.com/repository/central' }
//        jcenter { url 'https://maven.aliyun.com/repository/jcenter' }
        mavenCentral()
    }
    repositories {
        mavenCentral()
    }


}

bootJar {
    enabled = false
}
jar {
    enabled = false
}


subprojects {
    apply plugin: 'java-library'
    apply plugin: 'signing'
    apply plugin: 'maven-publish'

    java {
        withJavadocJar()
        withSourcesJar()
    }

    tasks.sourcesJar {
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    }

    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
                pom {
                    name = 'hyena'
                    description = 'Micro service for user score hosting. '
                    url = 'https://github.com/alphajiang/hyena'
                    properties = [
                            myProp          : "value",
                            "prop.with.dots": "anotherValue"
                    ]
                    licenses {
                        license {
                            name = 'The Apache License, Version 2.0'
                            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        }
                    }
                    developers {
                        developer {
                            id = 'alphajiang'
                            name = 'Alpha Jiang'
                            email = 'alphajiang@163.com'
                        }
                    }
                    scm {
                        connection = 'https://github.com/alphajiang/hyena.git'
                        developerConnection = 'https://github.com/alphajiang/hyena.git'
                        url = 'https://github.com/alphajiang/hyena.git'
                    }
                }
            }
            library(MavenPublication) {
                from components.java
            }
        }
        repositories {
            maven {
                // change URLs to point to your repos, e.g. http://my.org/repo
                def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2"
                def snapshotsRepoUrl = snapshotRepoUrl
                credentials {
                    username = ossrhUsername
                    password = ossrhPassword
                }
                url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
            }
        }
    }


    signing {
        // un-comment the line for release signing
        sign publishing.publications.mavenJava
//        sign configurations.archives
    }

    javadoc {
        if (JavaVersion.current().isJava9Compatible()) {
            options.addBooleanOption('html5', true)
        }
    }

    dependencies {


        compileOnly("org.projectlombok:lombok:${lombokVersion}")
        annotationProcessor("org.projectlombok:lombok:${lombokVersion}")
        testAnnotationProcessor("org.projectlombok:lombok:${lombokVersion}")
        testCompileOnly("org.projectlombok:lombok:${lombokVersion}")

        testImplementation('org.junit.jupiter:junit-jupiter:5.7.1')
        testImplementation('org.junit.platform:junit-platform-launcher:1.7.1')
        testImplementation("org.springframework.boot:spring-boot-starter-test:${springBootVersion}") {
            exclude group: 'junit'
        }

    }
}
// Only report code coverage for projects that are distributed
def publishedProjects = subprojects.findAll { it.path == ':hyena-core' }

//task jacocoMerge(type: JacocoMerge) {
//    publishedProjects.each { subproject ->
//        executionData subproject.tasks.withType(Test)
//    }
//    doFirst {
//        executionData = files(executionData.findAll { it.exists() })
//    }
//}

task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
    description = 'Generates an aggregate report from all subprojects'
    dependsOn publishedProjects.test    //, jacocoMerge

    additionalSourceDirs.from = publishedProjects.sourceSets.main.allSource.srcDirs
    sourceDirectories.from = publishedProjects.sourceSets.main.allSource.srcDirs
    classDirectories.from = publishedProjects.sourceSets.main.output
//    executionData jacocoMerge.destinationFile

    reports {
        html.required = true // human readable
        xml.required = true // required by coveralls
    }
}

coveralls {
    sourceDirs = publishedProjects.sourceSets.main.allSource.srcDirs.flatten()
    jacocoReportPath = "${buildDir}/reports/jacoco/jacocoRootReport/jacocoRootReport.xml"
}

tasks.coveralls {
    group = 'Coverage reports'
    description = 'Uploads the aggregated coverage report to Coveralls'

    dependsOn jacocoRootReport
    //onlyIf { System.env.'CI' && !JavaVersion.current().isJava9Compatible() }
}