Skip to content

Instantly share code, notes, and snippets.

@qrtt1
Last active August 29, 2015 13:57
Show Gist options
  • Save qrtt1/9799568 to your computer and use it in GitHub Desktop.
Save qrtt1/9799568 to your computer and use it in GitHub Desktop.
Gradle Notes

在 gradle 內,執行特定 task 時,需要不同的設定時,可以透 taskGraph 來判斷。下面這例子,很典型地是在 publish 新的 library 時,不含設定檔:

gradle.taskGraph.whenReady { graph ->
    if (graph.hasTask(uploadArchives)) {
        jar {
            exclude '**/*.properties'
        }
    }
}

有些 project 同時具有 application 與 library 的功能,當 application 正常 build 出來時,會需要含設定檔。作為 library 被編出來時,不需要設定檔,放上去還有可能在 classpath 內 shadowing 其他同名的設定檔。

gradle build 時需要 embedded tomcat,跑 gradle war 時就不需要它了。

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

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'spring-boot'

jar {
    baseName = 'gs-rest-service'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.7
targetCompatibility = 1.7

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("junit:junit")
}

gradle.taskGraph.whenReady { graph ->
    if (graph.hasTask(war)) {
        dependencies {
            providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
        }
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment