kotlin build.gradle.kts配置,支持占位符替换文件中变量

kotlin springboot工程kotlin_app

kotlin_app/build.gradle.kts:  

import org.apache.tools.ant.filters.FixCrLfFilter
import org.apache.tools.ant.filters.ReplaceTokens
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.4.4"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.4.31"
    kotlin("plugin.spring") version "1.4.31"

}

group = "com.example"
version = "0.0.1"
java.sourceCompatibility = JavaVersion.VERSION_1_8


repositories {
//	mavenCentral()
    maven { setUrl("http://maven.aliyun.com/nexus/content/groups/public") }
}

dependencies {
    //implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
    implementation("org.springframework.boot:spring-boot-starter")
    implementation("org.springframework.boot:spring-boot-starter-web") {
        exclude(module = "spring-boot-starter-tomcat")
    }
    implementation("org.springframework.boot:spring-boot-starter-jetty")

    implementation("org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4")

    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    testImplementation("org.springframework.boot:spring-boot-starter-test")

    implementation("org.jetbrains.kotlin:kotlin-maven-allopen:1.4.31")
    implementation("org.jetbrains.kotlin:kotlin-maven-noarg:1.4.31")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.12.2")
    implementation("cn.hutool:hutool-all:5.6.1")
    implementation("com.google.guava:guava:29.0-jre")
}

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

tasks.withType {
    useJUnitPlatform()
}

//val tokens = mapOf(
//    "version" to "2.3.1",
//    "copyright" to "2009",
//    "project" to project.name,
//    "beginToken" to "@",
//    "endToken" to "@",
//)

fun loadEnv(): Any {
    val configFile = file("env/config.groovy")
    val env = if (project.hasProperty("env")) project.property("env").toString() else "dev"
    logger.lifecycle("1-----------env: $env")
    val confProp = groovy.util.ConfigSlurper(env).parse(configFile.toURL())
    //config.groovy配置文件,配置占位符 (@key@)
    val tokens = confProp.toProperties()
    logger.lifecycle("2-----------tokens: $tokens")
    return tokens;
}

fun delFiles(dir:String){
    val configs=fileTree(dir)
    for (f in configs) {
        f.delete()
    }
}


task("copyLibs", Copy::class) {
    logger.lifecycle("------------------copyLibs")
    from(configurations.runtimeClasspath)
    into("$buildDir/libs")
}.dependsOn(tasks.jar)

//本工程的jar包,排除配置文件
tasks.jar {
    logger.lifecycle("------------------tasks.jar")
    enabled = true
    // from("build/classes/java/main")
    exclude("*.properties", "*.yml", "*.xml", "*.setting")
}

tasks.processResources {
    logger.lifecycle("------------------processResources")
    delFiles("$buildDir/resources")
//    filteringCharset = "UTF-8"
//    from(sourceSets.main.get().resources.srcDirs) {
//        include("**")
//        filter("tokens" to tokens)
//    }
    filter(ReplaceTokens::class, "tokens" to loadEnv())
    filter(
        FixCrLfFilter::class,
        "eol" to FixCrLfFilter.CrLf.newInstance("lf"),
        "tab" to FixCrLfFilter.AddAsisRemove.newInstance("asis"),
        "tablength" to 4,
        "eof" to FixCrLfFilter.AddAsisRemove.newInstance("remove"),
        "fixlast" to true
    )

}

task("processShell") {
    logger.lifecycle("------------------processShell")
    //必须先删除原sh目录下文件后重新生成,不然使用gradle build -Penv参数切换环境时,无法替换占位符变量
    delFiles("$buildDir/sh")
    from("src/main/sh") {
        filter(ReplaceTokens::class, "tokens" to loadEnv())
        filter(
            FixCrLfFilter::class,
            "eol" to FixCrLfFilter.CrLf.newInstance("lf"),
            "tab" to FixCrLfFilter.AddAsisRemove.newInstance("asis"),
            "tablength" to 4,
            "eof" to FixCrLfFilter.AddAsisRemove.newInstance("remove"),
            "fixlast" to true
        )
    }
    into("$buildDir/sh")
}

//build命令依赖的其他命令
tasks.build {
    dependsOn("copyLibs","processShell",tasks.processResources)
}

// 安装 gradle-6.7
// 编译打包命令: gradle build -Penv=prod -Dfile.encoding=UTF-8 --daemon

kotlin_app/env/config.groovy:

prefix='/output'

environments {
    //192.168.3.165
    dev {
        sh{
            WORK_HOME='/home/boy/kotlin_app';
            PNAME='kotlin_app';
            LANG='zh_CN.utf8';
            JAVA_HOME='/home/boy/jdk/jdk8';
        }
    }

    prod{
        sh{
            WORK_HOME='/root/kotlin_app';
            PNAME='kotlin_app';
            LANG='zh_CN.utf8';
            JAVA_HOME='/soft/jre1.8.0_131';
        }

    }
}

src/main/resources/logback.xml:  其中包含占位符 @sh.WORK_HOME@



    
    
    

    
        
            UTF-8
        
        
            ${PATTERN}
        
    
    
        
            UTF-8
        
        
            ${LOG_NAME}
            30
        
        
            ${PATTERN}
        
    
    
    
    
    
    
    
        
    

settings.gradle.kts:

     rootProject.name = "a-restful"

 

使用命令编译打包:

gradle build -Penv=dev -Dfile.encoding=UTF-8 --daemon
gradle build -Penv=prod -Dfile.encoding=UTF-8 --daemon

-Penv的值为config.groovy中的配置

执行编译打包命令后,会将config.groovy中配置的变量替换文件中的占位符@xx@

kotlin_app\build\libs下生成jar包名: a-restful-0.0.1.jar

你可能感兴趣的:(kotlin,kotlin,gradle)