Gradle使用筆記

通用

統必定義子項目的通用部分

在根目錄的build.gradle中定義:java

subprojects{
    repositories {
        jcenter()
    }
}

War

war 依賴 war

war {
    into("/") {
        exclude 'META-INF/MANIFEST.MF' // or whatever
        with project(':sub-project-name').war
    }
}

打包war後,自動建立exploded war

添加一個任務web

task explodedWar(type: Sync) {
    println 'exploded war start'
    into "${buildDir}/exploded"
    with war
    println 'exploded war end'
}

在war中定義finalizedByapp

war {
    finalizedBy "explodedWar"
}

執行war任務的結果gradle

15:08:28: Executing external task 'war'...
exploded war start
exploded war end
:wbms-lib:compileJava UP-TO-DATE
:wbms-lib:processResources UP-TO-DATE
:wbms-lib:classes UP-TO-DATE
:wbms-lib:jar UP-TO-DATE
:wbms-web:compileJava UP-TO-DATE
:wbms-web:processResources UP-TO-DATE
:wbms-web:classes UP-TO-DATE
:wbms-web-jetty-embed:compileJava UP-TO-DATE
:wbms-web-jetty-embed:processResources UP-TO-DATE
:wbms-web-jetty-embed:classes UP-TO-DATE
:wbms-web-jetty-embed:war
:wbms-web-jetty-embed:explodedWar

BUILD SUCCESSFUL

Total time: 8.522 secs
15:08:37: External task execution finished 'war'.

Jar

打包時,自動加入在manifest中dependencies中jar包

apply plugin: 'java'
sourceCompatibility = 1.8
dependencies{
    ...
}
jar {
    manifest {
        attributes(
                "Main-Class": "com.cnvp.wbms.application.startup",
                "Implementation-Title": "Gradle",
                "Implementation-Version": version,
                "Class-Path": configurations.compile.collect {it.getName()}.join(' ')
        )
    }
}

須要注意的是:dependencies必須在jar上面。我以前是顛倒的configurations.compile.collect {it.getName()}.join(' ')老是報錯ui

相關文章
相關標籤/搜索