1

通用

统一定义子项目的通用部分

在根目录的build.gradle中定义:

subprojects{
    repositories {
        jcenter()
    }
}

War

war 依赖 war

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

打包war后,自动创建exploded war

添加一个任务

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

在war中定义finalizedBy

war {
    finalizedBy "explodedWar"
}

执行war任务的结果

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(' ')总是报错


MichaelZ
374 声望13 粉丝

web全栈工程师