plugins {
    id 'java'
    id 'application'
    id "com.github.johnrengelman.shadow" version "7.1.2"
}

group = 'com.incubator.game'
version = '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

application {
    // 使用新的方法设置主类,适应 Gradle 7.0 及以上版本
    mainClass.set('com.incubator.game.GameServerStart')
}

// 禁用默认的 JAR 任务,因为我们使用 Shadow JAR
tasks.jar {
    enabled = false
}

// 配置 Shadow JAR 任务
tasks.shadowJar {
    zip64 = true
    // 设置 JAR 包的基本名称
    archiveBaseName.set('incubator-game')
    // 使用项目的版本号作为 JAR 的版本
    archiveVersion.set(project.version)
    // 移除分类器,生成的 JAR 没有后缀
    archiveClassifier.set('')

    // 添加自定义清单
    manifest {
        attributes(
                'Main-Class': application.mainClass.get() // 使用动态设置的主类
        )
    }

    // 可选:排除不必要的依赖
    exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA'
}

// 依赖版本集中管理
ext {
    junitVersion = '5.9.0'
    shadowVersion = '7.1.2'
}

// 项目依赖
dependencies {
    // 子模块依赖
    implementation project(':incubator-core')

    // 测试依赖
    testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
}

// 配置测试任务
tasks.test {
    useJUnitPlatform()
    testLogging {
        events 'PASSED', 'FAILED', 'SKIPPED'
    }
}

// 将 Shadow JAR 绑定到 assemble 阶段
tasks.assemble.dependsOn(tasks.shadowJar)

// 增加构建信息任务
tasks.register("buildInfo") {
    doLast {
        println "Building project '${project.name}' version '${project.version}'"
        println "Main class: ${application.mainClass.get()}"
        println "Shadow JAR: ${tasks.shadowJar.get().archiveFile.get()}"
    }
}

// 清理任务优化
tasks.clean {
    delete "$buildDir/libs" // 确保清理构建目录
}