当前位置:   article > 正文

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

build.gradle.kts

kotlin springboot工程kotlin_app

kotlin_app/build.gradle.kts:  

  1. import org.apache.tools.ant.filters.FixCrLfFilter
  2. import org.apache.tools.ant.filters.ReplaceTokens
  3. import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
  4. plugins {
  5. id("org.springframework.boot") version "2.4.4"
  6. id("io.spring.dependency-management") version "1.0.11.RELEASE"
  7. kotlin("jvm") version "1.4.31"
  8. kotlin("plugin.spring") version "1.4.31"
  9. }
  10. group = "com.example"
  11. version = "0.0.1"
  12. java.sourceCompatibility = JavaVersion.VERSION_1_8
  13. repositories {
  14. // mavenCentral()
  15. maven { setUrl("http://maven.aliyun.com/nexus/content/groups/public") }
  16. }
  17. dependencies {
  18. //implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
  19. implementation("org.springframework.boot:spring-boot-starter")
  20. implementation("org.springframework.boot:spring-boot-starter-web") {
  21. exclude(module = "spring-boot-starter-tomcat")
  22. }
  23. implementation("org.springframework.boot:spring-boot-starter-jetty")
  24. implementation("org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4")
  25. implementation("org.jetbrains.kotlin:kotlin-reflect")
  26. implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
  27. testImplementation("org.springframework.boot:spring-boot-starter-test")
  28. implementation("org.jetbrains.kotlin:kotlin-maven-allopen:1.4.31")
  29. implementation("org.jetbrains.kotlin:kotlin-maven-noarg:1.4.31")
  30. implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.12.2")
  31. implementation("cn.hutool:hutool-all:5.6.1")
  32. implementation("com.google.guava:guava:29.0-jre")
  33. }
  34. tasks.withType<KotlinCompile> {
  35. kotlinOptions {
  36. freeCompilerArgs = listOf("-Xjsr305=strict")
  37. jvmTarget = "1.8"
  38. }
  39. }
  40. tasks.withType<Test> {
  41. useJUnitPlatform()
  42. }
  43. //val tokens = mapOf(
  44. // "version" to "2.3.1",
  45. // "copyright" to "2009",
  46. // "project" to project.name,
  47. // "beginToken" to "@",
  48. // "endToken" to "@",
  49. //)
  50. fun loadEnv(): Any {
  51. val configFile = file("env/config.groovy")
  52. val env = if (project.hasProperty("env")) project.property("env").toString() else "dev"
  53. logger.lifecycle("1-----------env: $env")
  54. val confProp = groovy.util.ConfigSlurper(env).parse(configFile.toURL())
  55. //config.groovy配置文件,配置占位符 (@key@)
  56. val tokens = confProp.toProperties()
  57. logger.lifecycle("2-----------tokens: $tokens")
  58. return tokens;
  59. }
  60. fun delFiles(dir:String){
  61. val configs=fileTree(dir)
  62. for (f in configs) {
  63. f.delete()
  64. }
  65. }
  66. task("copyLibs", Copy::class) {
  67. logger.lifecycle("------------------copyLibs")
  68. from(configurations.runtimeClasspath)
  69. into("$buildDir/libs")
  70. }.dependsOn(tasks.jar)
  71. //本工程的jar包,排除配置文件
  72. tasks.jar {
  73. logger.lifecycle("------------------tasks.jar")
  74. enabled = true
  75. // from("build/classes/java/main")
  76. exclude("*.properties", "*.yml", "*.xml", "*.setting")
  77. }
  78. tasks.processResources {
  79. logger.lifecycle("------------------processResources")
  80. delFiles("$buildDir/resources")
  81. // filteringCharset = "UTF-8"
  82. // from(sourceSets.main.get().resources.srcDirs) {
  83. // include("**")
  84. // filter<ReplaceTokens>("tokens" to tokens)
  85. // }
  86. filter(ReplaceTokens::class, "tokens" to loadEnv())
  87. filter(
  88. FixCrLfFilter::class,
  89. "eol" to FixCrLfFilter.CrLf.newInstance("lf"),
  90. "tab" to FixCrLfFilter.AddAsisRemove.newInstance("asis"),
  91. "tablength" to 4,
  92. "eof" to FixCrLfFilter.AddAsisRemove.newInstance("remove"),
  93. "fixlast" to true
  94. )
  95. }
  96. task<Copy>("processShell") {
  97. logger.lifecycle("------------------processShell")
  98. //必须先删除原sh目录下文件后重新生成,不然使用gradle build -Penv参数切换环境时,无法替换占位符变量
  99. delFiles("$buildDir/sh")
  100. from("src/main/sh") {
  101. filter(ReplaceTokens::class, "tokens" to loadEnv())
  102. filter(
  103. FixCrLfFilter::class,
  104. "eol" to FixCrLfFilter.CrLf.newInstance("lf"),
  105. "tab" to FixCrLfFilter.AddAsisRemove.newInstance("asis"),
  106. "tablength" to 4,
  107. "eof" to FixCrLfFilter.AddAsisRemove.newInstance("remove"),
  108. "fixlast" to true
  109. )
  110. }
  111. into("$buildDir/sh")
  112. }
  113. //build命令依赖的其他命令
  114. tasks.build {
  115. dependsOn("copyLibs","processShell",tasks.processResources)
  116. }
  117. // 安装 gradle-6.7
  118. // 编译打包命令: gradle build -Penv=prod -Dfile.encoding=UTF-8 --daemon

kotlin_app/env/config.groovy:

  1. prefix='/output'
  2. environments {
  3. //192.168.3.165
  4. dev {
  5. sh{
  6. WORK_HOME='/home/boy/kotlin_app';
  7. PNAME='kotlin_app';
  8. LANG='zh_CN.utf8';
  9. JAVA_HOME='/home/boy/jdk/jdk8';
  10. }
  11. }
  12. prod{
  13. sh{
  14. WORK_HOME='/root/kotlin_app';
  15. PNAME='kotlin_app';
  16. LANG='zh_CN.utf8';
  17. JAVA_HOME='/soft/jre1.8.0_131';
  18. }
  19. }
  20. }

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration scan="true" scanPeriod="60 seconds">
  3. <property name="LOG_HOME" value="@sh.WORK_HOME@/log" />
  4. <property name="LOG_NAME" value="${LOG_HOME}/main.log%d{yyyyMMdd}" />
  5. <property name="PATTERN" value="%date %p %logger{2} %L - %m%n" />
  6. <appender name="C" class="ch.qos.logback.core.ConsoleAppender">
  7. <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
  8. <charset>UTF-8</charset>
  9. </encoder>
  10. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  11. <pattern>${PATTERN}</pattern>
  12. </encoder>
  13. </appender>
  14. <appender name="F1" class="ch.qos.logback.core.rolling.RollingFileAppender">
  15. <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
  16. <charset>UTF-8</charset>
  17. </encoder>
  18. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  19. <FileNamePattern>${LOG_NAME}</FileNamePattern>
  20. <MaxHistory>30</MaxHistory>
  21. </rollingPolicy>
  22. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  23. <pattern>${PATTERN}</pattern>
  24. </encoder>
  25. </appender>
  26. <logger name="org.springframework" level="error" additivity="true" />
  27. <logger name="org.eclipse.jetty" level="error" additivity="true" />
  28. <!-- <logger name="oracle.jdbc.driver" level="error" additivity="true" /> -->
  29. <logger name="com.zaxxer.hikari" level="error" additivity="true" />
  30. <logger name="com.soft.mapper" level="debug" />
  31. <root level="info">
  32. <appender-ref ref="C" />
  33. </root>
  34. </configuration>

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/293387
推荐阅读
相关标签
  

闽ICP备14008679号