Bryan

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            37 Posts :: 3 Stories :: 24 Comments :: 0 Trackbacks
          Once I provided some gradle scripts for building a runnable jar here, and now I tried to change them to gradle script kotlin and the following are the different versions of them

          Custom build(Building a runnable jar by the way "Copy required libraries into a sub-folder next to the generated Jar" in eclipse)


          //Builing a runnable jar by the way "Copy required libraries into a sub-folder next to the generated Jar"

          import org.gradle.jvm.tasks.Jar

          plugins {
              java
              eclipse
          }

          //profile configuration
          configure<JavaPluginConvention> {
              setSourceCompatibility(1.7)
              setTargetCompatibility(1.7)
              
          if(project.hasProperty("env")){
                  var env = project.property("env");
                  sourceSets.getByName("main").resources.srcDirs("src/main/profile/" + env)
              }
          }

          dependencies {
              compile("junit:junit:4.12")
          }

          repositories {
              jcenter()
          }

          //copy dependencies jars to a folder
          val copyDependencies = task<Copy>("copyDependencies") {
              from(configurations.compile)
              into("libs/lib")
          }

          //the jar tasks for building a runnable jar, 
          //and the denpendcies jars are in a folder next to the generated jar
          val jar: Jar by tasks
          jar.apply {
              dependsOn(copyDependencies)
              manifest.attributes.apply {
                 put("Main-Class""samples.HelloWorld")
                 
          if (!configurations.runtime.isEmpty()) {    
                      put("Class-Path","./ lib/" + configurations.runtime.map{ it.name }.joinToString(" lib/"))
                 }
              }
          }


          The following are differents ways of building runnable jar with spring boot gradle plugin and kotlin-dsl, and most of them I make the change according to the internet resources on stackoverflow and github.

          version1
          import org.springframework.boot.gradle.plugin.SpringBootPlugin
          import org.springframework.boot.gradle.SpringBootPluginExtension

          buildscript {
              dependencies {
                  classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE")
              }
              
              repositories {
                  jcenter()
              }
          }

          plugins {
              java
              eclipse
              id("org.springframework.boot") version "1.5.6.RELEASE"
          }

          //java {
              
          //sourceCompatibility = JavaVersion.VERSION_1_7
              
          //targetCompatibility = JavaVersion.VERSION_1_7
          //}

          dependencies {
              testCompile("junit:junit:4.12")
          }

          repositories {
              jcenter()
          }

          configure<JavaPluginConvention> {
              setSourceCompatibility(1.7)
              setTargetCompatibility(1.7)
              
          if(project.hasProperty("env")){
                  var env = project.property("env");
                  sourceSets.getByName("main").resources.srcDirs("src/main/profile/" + env)
              }
          }

          configure<ProcessResources>("processResources") {
              
          //exclude shell scripts in profile folder
              if(project.hasProperty("env")){
                  exclude("src/main/profile/scripts")
              }
          }

          //apply<SpringBootPlugin>()
          configure<SpringBootPluginExtension> {
              mainClass = "samples.HelloWorld"
          }

          inline fun <reified C> Project.configure(name: String, configuration: C.() -> Unit) {
              (this.tasks.getByName(name) as C).configuration()
          }


          version2

          import org.springframework.boot.gradle.SpringBootPluginExtension

          buildscript {
              dependencies {
                  classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE")
              }
              
              repositories {
                  jcenter()
              }
          }

          plugins {
              java
              eclipse
              id("org.springframework.boot") version "1.5.6.RELEASE"
          }

          //java {
              
          //sourceCompatibility = JavaVersion.VERSION_1_7
              
          //targetCompatibility = JavaVersion.VERSION_1_7
          //}

          dependencies {
              testCompile("junit:junit:4.12")
          }

          repositories {
              jcenter()
          }

          configure<JavaPluginConvention> {
              setSourceCompatibility(1.7)
              setTargetCompatibility(1.7)
              
          if(project.hasProperty("env")){
                  var env = project.property("env");
                  sourceSets.getByName("main").resources.srcDirs("src/main/profile/" + env)
              }
          }

          configure<ProcessResources>("processResources") {
             
          if(project.hasProperty("env")){
                  exclude("src/main/profile/scripts")
             }
          }

          springBoot {
              mainClass = "Application"
          }

          inline fun <reified C> Project.configure(name: String, configuration: C.() -> Unit) {
               println(name + this.tasks.getByName(name));
              (this.tasks.getByName(name) as C).configuration()
          }

          /**
           * Retrieves or configures the [springBoot][org.springframework.boot.gradle.SpringBootPluginExtension] project extension.
           
          */
          fun Project.springBoot(configure: org.springframework.boot.gradle.SpringBootPluginExtension.() -> Unit = {}) =
          extensions.getByName<org.springframework.boot.gradle.SpringBootPluginExtension>("springBoot").apply { configure() }


          Version3
          //build a runnable jar with kotlin-dsl and spring boot gradle plugin, and It supports profiles,
          //any resource files related to different env can be placed in profile folder(env/stage/prod) 
          //and then run the command gradle.bat build -Penv=dev/stage/prod

          import org.springframework.boot.gradle.repackage.RepackageTask
          import org.gradle.language.jvm.tasks.ProcessResources

          buildscript {
              dependencies {
                  classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE")
              }
              
              repositories {
                  jcenter()
              }
          }

          plugins {
              java
              eclipse
              id("org.springframework.boot") version "1.5.6.RELEASE"
          }

          //java {
              
          //sourceCompatibility = JavaVersion.VERSION_1_7
              
          //targetCompatibility = JavaVersion.VERSION_1_7
          //}

          dependencies {
              testCompile("junit:junit:4.12")
          }

          repositories {
              jcenter()
          }

          configure<JavaPluginConvention> {
              setSourceCompatibility(1.7)
              setTargetCompatibility(1.7)
              
          if(project.hasProperty("env")){
                  var env = project.property("env");
                  sourceSets.getByName("main").resources.srcDirs("src/main/profile/" + env)
              }
          }

          (tasks.getByName("processResources") as ProcessResources).apply {
              
          //exclude the shell scripts  if user used command with env like "gradle.dat build -Penv=dev"
              if(project.hasProperty("env")){
                  exclude("src/main/profile/scripts")
              }
          }

          (tasks.getByName("bootRepackage") as RepackageTask).apply {
            mainClass = "demo.Application"
          }


          Reference
          https://github.com/gradle/kotlin-dsl/commit/b02916c7160ea45ad1bf7600d5a80c3bdaf6ceb9
          https://techdev.io/en/developer-blog/building-a-fat-jar-with-gradle-script-kotlin
          https://stackoverflow.com/questions/45399232/gradle-kotlin-is-it-possible-to-create-dynamic-on-fly-tasks
          https://stackoverflow.com/questions/39630897/how-to-configure-spring-boot-repackage-with-gradle-script-kotlin
          https://stackoverflow.com/questions/44741982/accessing-properties-of-a-task-in-gradle-kotlin
          https://stackoverflow.com/questions/40096007/how-to-configure-the-processresources-task-in-a-gradle-kotlin-build?rq=1
          https://github.com/kucharzyk/spring-kotlin-angular4/blob/master/build.gradle.kts
          https://github.com/gradle/kotlin-dsl/blob/master/build.gradle.kts
          https://stackoverflow.com/questions/41794914/how-to-create-the-fat-jar-with-gradle-kotlin-script?rq=1
          https://github.com/sdeleuze/spring-boot-kotlin-demo/blob/master/build.gradle.kts
          https://github.com/mixitconf/mixit/blob/master/build.gradle.kts
          https://github.com/gradle/kotlin-dsl/blob/master/build.gradle.kts
          https://github.com/mpecan/base_gradle_kts_newest/blob/master/build.gradle.kts
          https://github.com/gradle/gradle/blob/907a4e78989a1d7115020535ea0c0fc313d65b20/subprojects/plugins/src/main/java/org/gradle/api/plugins/JavaPluginConvention.java
          https://github.com/JLLeitschuh/springBootTesting/blob/da2d3be9bff22b50d6ba0b15bc0371eb6cc88b56/build.gradle.kts
          https://github.com/max-neverov/KMS/blob/a0239b9f8b82f72513b698a366209e8deb854893/build.gradle.kts
          https://github.com/kolyjjj/weed/blob/ce663f1a8606171e93e9f85092322191219abb97/build.gradle.kts
          https://github.com/gradle/kotlin-dsl/commit/b02916c7160ea45ad1bf7600d5a80c3bdaf6ceb9
          https://github.com/gradle/kotlin-dsl/blob/master/samples/copy/build.gradle.kts


          posted on 2017-08-29 10:05 Life is no respector of any genius. 閱讀(875) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 抚宁县| 札达县| 民县| 焉耆| 东山县| 建水县| 凭祥市| 阿城市| 大关县| 津南区| 齐齐哈尔市| 芷江| 池州市| 同德县| 武义县| 永修县| 竹溪县| 顺平县| 阿拉善右旗| 德阳市| 新乡县| 武城县| 射洪县| 扶沟县| 松原市| 台北市| 泸水县| 永川市| 望奎县| 福海县| 甘洛县| 乌拉特后旗| 秦皇岛市| 泸溪县| 英超| 禹城市| 双峰县| 威信县| 西城区| 建平县| 信丰县|