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. 阅读(858) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: