import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.springframework.boot.gradle.tasks.bundling.BootJar

group = "cc-lotus.gaf3"
version = "3.1.714"
extra["springCloudVersion"] = "Hoxton.SR5"
extra["jjwtVersion"] = "0.10.5"

val mavenUser: String? by project
val mavenPassword: String? by project
private val repoConf: String = System.getProperty("repoPath") ?: "/var/repo"
val repoPath: String = file("$rootDir").toPath().root.resolve(repoConf).toString()

plugins {
    id("java")
    id("maven-publish")
    id("io.spring.dependency-management") version "1.0.10.RELEASE"
    id("org.springframework.boot") version "2.3.3.RELEASE" apply false
    id("org.jetbrains.dokka") version "0.10.1"
    kotlin("jvm") version "1.3.72" apply false
    kotlin("plugin.spring") version "1.3.72" apply false
    kotlin("plugin.jpa") version "1.3.72" apply false
}

dependencyManagement {
    imports {
        mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
    }
}

repositories {
    // 阿里云镜像
    maven { url = uri("https://maven.aliyun.com/repository/public") }
    maven { url = uri("https://maven.aliyun.com/repository/gradle-plugin") }
    maven { url = uri("https://maven.aliyun.com/repository/spring") }
    maven { url = uri("https://maven.aliyun.com/repository/spring-plugin") }
    maven {
        name = "localRepo"
        url = uri("file://$repoPath")
    }
    maven {
        name = "cc-lotus"
        url = uri("http://maven.cc-lotus.info/repository/maven-public/")
    }
    // mavenCentral()
}

configure(subprojects.filter { it.name != "platform" }) {
    apply(plugin = "java")
    apply(plugin = "maven-publish")
    apply(plugin = "org.jetbrains.kotlin.jvm")
    apply(plugin = "org.jetbrains.dokka")

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

    tasks.withType<JavaCompile> {
        options.encoding = "UTF-8"
        sourceCompatibility = "1.8"
        targetCompatibility = "1.8"
    }

    tasks.withType<KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs = listOf("-Xjsr305=strict")
            jvmTarget = "1.8"
        }
    }

    tasks.withType<Javadoc> {
        options.encoding = "UTF-8"
    }

    repositories {
        maven {
            name = "localRepo"
            url = uri("file://$repoPath")
        }
        maven {
            name = "cc-lotus"
            url = uri("http://maven.cc-lotus.info/repository/maven-public/")
        }
        // 阿里云镜像
        maven { url = uri("https://maven.aliyun.com/repository/public") }
        maven { url = uri("https://maven.aliyun.com/repository/gradle-plugin") }
        maven { url = uri("https://maven.aliyun.com/repository/spring") }
        maven { url = uri("https://maven.aliyun.com/repository/spring-plugin") }
        // mavenCentral()
    }
    publishing {
//        publications {
//            create<MavenPublication>("maven") {
//                groupId = "cc-lotus.gaf3"
//                artifactId = tasks.jar.get().archiveBaseName.get()
//                version = "3.0.0"
//                from(components["java"])
//            }
//        }
        repositories {
            maven {
                name = "localRepo"
                url = uri("file://$repoPath")
            }
        }
        repositories {
            maven {
                name = "cc-lotus"
                url = uri("http://maven.cc-lotus.info/repository/maven-releases/")
                //认证用户和密码
                credentials {
                    username = mavenUser // """${property("mavenUser")}"""
                    password = mavenPassword // """${property("mavenPassword")}"""
                }
            }
        }
    }
}

task<Copy>("dist") {
    into("$buildDir/dist")
    project(":gateway") {
        from(tasks.withType<BootJar>())
    }
}

tasks {
//    processResources {
//         expand(project.properties)
//    }
    val dokka by getting(org.jetbrains.dokka.gradle.DokkaTask::class) {
        outputFormat = "html"
        outputDirectory = "$buildDir/dokka"
    }
}

//task("printRepo") {
//    println(org.jetbrains.kotlin.org.jline.utils.OSUtils.IS_WINDOWS)
//    println(repoPath)
//}
