import org.springframework.boot.gradle.tasks.bundling.BootJar

group = "cc-lotus.gaf3"
version = rootProject.version

plugins {
    id("java")
    id("io.spring.dependency-management")
    id("org.springframework.boot") apply false
    kotlin("jvm")
    kotlin("plugin.spring")
    kotlin("plugin.jpa") apply false
}

java {
    disableAutoTargetJvm()
    // withSourcesJar()
    // withJavadocJar()
}

repositories {
    maven {
        name = "aliyun"
        url = uri("http://maven.aliyun.com/nexus/content/groups/public/")
    }
    // mavenCentral()
}

allprojects {
    apply(plugin = "java")

    tasks {
        processResources {
            // expand("db.user" to dbUser)
            // expand 在处理奇数个中文的时候会出现乱码情况
            // 所以这里只处理application.yml，忽略其他资源文件，避免处理后产生乱码
            filesMatching("application.yml") {
                expand(project.properties)
            }
            // expand(project.properties)
        }
    }

}

subprojects {
    group = "cc-lotus.gaf3"
    version = rootProject.version

    apply(plugin = "org.springframework.boot")
    apply(plugin = "io.spring.dependency-management")
    apply(plugin = "org.jetbrains.kotlin.jvm")
    apply(plugin = "org.jetbrains.kotlin.plugin.spring")
    apply(plugin = "org.jetbrains.kotlin.plugin.jpa")

    dependencies {
        api(platform(project(":platform")))
        api(project(path = ":shared:cloud"))
        api(project(path = ":shared:util"))
        api(kotlin("reflect"))
        api(kotlin("stdlib-jdk8"))
        implementation("org.springframework.boot:spring-boot-starter-webflux")
        implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
        implementation("org.springframework.cloud:spring-cloud-starter-openfeign")
        implementation("org.springframework.boot:spring-boot-configuration-processor")
        implementation("com.alibaba:fastjson")
        implementation("commons-io:commons-io")
        implementation("io.jsonwebtoken:jjwt-api")
        implementation("io.jsonwebtoken:jjwt-impl")
        implementation("io.jsonwebtoken:jjwt-jackson")
        implementation("javax.validation:validation-api")
        runtimeOnly("org.hibernate.validator:hibernate-validator")
        runtimeOnly(fileTree("$rootDir/libs") { include("*.jar") })
    }
    dependencyManagement {
        imports {
            mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
        }
    }

//    tasks.register<Jar>(name = "libJar") {
//        archiveBaseName.set("gaf3-${project.name}-lib")
//        from(sourceSets["main"].output)
//        include("gaf3/**")
//        exclude {
//            it.name.endsWith("Application.class")
//        }
//        exclude {
//            it.name.endsWith("ApplicationKt.class")
//        }
//        exclude {
//            it.name.endsWith("Application${'$'}Companion.class")
//        }
//    }
//    tasks.build { dependsOn(tasks.named("libJar")) }

    configurations {
        create("lib")
    }

    artifacts {
        add("lib", tasks.jar)
    }

    tasks.withType<Jar> {
        archiveBaseName.set("gaf3-${project.name}")
    }
    tasks.named<BootJar>(name = "bootJar") {
        archiveClassifier.set("application")
    }
    tasks.jar {
        enabled = true
        // archiveAppendix.set("lib")
        exclude {
            it.name.endsWith("Application.class")
        }
        exclude {
            it.name.endsWith("ApplicationKt.class")
        }
        exclude {
            it.name.endsWith("Application${'$'}Companion.class")
        }
    }
}

configure(subprojects.filter { it.name != "service-api" && it.name != "service-verify" && it.name != "service-token" }) {
    dependencies {
        implementation("org.springframework.boot:spring-boot-starter-data-jpa")
        implementation("org.springframework.boot:spring-boot-starter-webflux")
        implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
        runtimeOnly("mysql:mysql-connector-java")
        testImplementation("org.springframework.boot:spring-boot-starter-test")
        // "testImplementation"("io.projectreactor:reactor-test")
        // testCompile("junit", "junit", "4.12")
    }
}

project("service-all") {
    dependencies {
        implementation(project(path = ":services:service-auth", configuration = "lib"))
        implementation(project(path = ":services:service-bind", configuration = "lib"))
        implementation(project(path = ":services:service-role", configuration = "lib"))
        implementation(project(path = ":services:service-code", configuration = "lib"))
        implementation(project(path = ":services:service-user", configuration = "lib"))
        implementation(project(path = ":services:service-log", configuration = "lib"))
        implementation(project(path = ":services:service-dept", configuration = "lib"))
        implementation(project(path = ":services:service-menu", configuration = "lib"))
        implementation(project(path = ":services:service-bff", configuration = "lib"))
    }
}
project("service-bff") {
    dependencies {
        api(project(path = ":shared:cloud"))
        api(project(path = ":shared:util"))
        api(project(path = ":services:service-bind", configuration = "lib"))
        api(project(path = ":services:service-user", configuration = "lib"))
        api(project(path = ":services:service-dept", configuration = "lib"))
        api(project(path = ":services:service-role", configuration = "lib"))
    }
}

project("service-auth") {
    dependencies {
        implementation(project(path = ":services:service-bff", configuration = "lib"))
        api(project(path = ":services:service-user", configuration = "lib"))
        api(project(path = ":services:service-role", configuration = "lib"))
        api(project(path = ":services:service-bind", configuration = "lib"))
    }
}

project(":services") {
    tasks.jar {
        archiveBaseName.set("gaf3-core-${project.name}")
        subprojects {
            from(tasks.withType<JavaCompile>())
            from(tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>())
            include("gaf3/**")
            exclude {
                it.name.endsWith("Application.class")
            }
            exclude {
                it.name.endsWith("ApplicationKt.class")
            }
            exclude {
                it.name.endsWith("Application${'$'}Companion.class")
            }
        }
    }
    publishing {
        publications {
            create<MavenPublication>("maven") {
                artifactId = "gaf-core-${project.name}"
                from(components["java"])
            }
        }
    }
}
