如何使用gradle脚本kotlin和spring boot gradle插件构建一个可运行的jar

时间:2017-08-25 03:15:21

标签: gradle-kotlin-dsl

我有一个gradle构建脚本,用于构建一个可运行的jar(dependnencies也是jars),带有spring boot gradle插件,工作正常,我想知道如何将它转换为gradle脚本kotlin。我不知道如何处理bootRepackage,profile以及kotlin中的processResources。以及是否支持使用spring boot gradle插件?感谢。

apply plugin: 'java'
apply plugin: 'eclipse'


buildscript {
    repositories {
        maven { url 'https://repo.spring.io/libs-milestone' }
   }

    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE'
    }
 }

apply plugin: 'org.springframework.boot'

sourceCompatibility = 1.6
targetCompatibility = 1.6
version = "1.0.0" 

tasks.withType(JavaCompile) {
    options.bootClasspath = "$JDK6_HOME/jre/lib/rt.jar"
}

if (project.hasProperty('env')) {
    println "Target environment: $env"
    sourceSets.main.resources.srcDir "src/main/profile/$env"
}

processResources{
    exclude 'TagNames.properties'
}

repositories {
    jcenter()
    maven { url "http://clojars.org/repo" }
}

dependencies {
    compile files('lib/jlibs-core.jar')
    compile files('lib/jlibs-xml.jar')
    compile files('lib/autonomyPiB.jar')
}

bootRepackage {
    mainClass = 'com.test.TestA'
}

1 个答案:

答案 0 :(得分:1)

我指的是github和stackoverflow资源,似乎以下方式有效,任何问题请告诉我更新:

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"
}

repositories {
    maven {
        url=uri("http://clojars.org/repo") 
    }
}

dependencies {
    compile ("commons-net:commons-net:2.0")
    compile ("log4j:log4j:1.2.17")
    compile ("javax.servlet:servlet-api:2.4")
}

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")
    }
}

//apply<SpringBootPlugin>()
configure<SpringBootPluginExtension> {
    mainClass = "sample.HelloWolrd"
}

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

另一种工作方式:

import org.springframework.boot.gradle.SpringBootPluginExtension

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) {
    (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() }