1. 程式人生 > 其它 >gradle 打包

gradle 打包

技術標籤:gradlejava

gradle 打包

打包 spring boot 單模組

// build.gradle

//... ... ... ...

dependencies {
    compile project(':common')
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok' //這個可以處理Gradle 找不到 lombok 符號
}

// 使用閉包定義任務
task copyJar(type: Copy) {
    delete "$buildDir\\libs\\lib"
    from configurations.compileClasspath
    into "$buildDir\\libs\\lib"
}
task copyConfigFile(type: Copy)  {
    delete "$buildDir\\libs\\config"
    from('src/main/resources')
    into 'build/libs/config'
}
task copyBinFile(type: Copy)  {
    delete "$buildDir\\libs\\bin"
    from('src/main/bin')
    into 'build/libs/bin'
}
task copyLogFile(type: Copy)  {
    delete "$buildDir\\libs\\log"
    from '../log/boot-server-log'
    into 'build/libs/log/boot-server-log'
}
task copyOtherFile(type: Copy)  {
    delete "$buildDir\\libs"
    from 'README.md'
    into 'build/libs'
}
//使用歸檔任務可以建立Zip、Tar、Jar、War、Ear等歸檔檔案
task dist(type: Zip) {
    from "$buildDir/libs"
    into 'serv'
    destinationDir file("$buildDir/dist")
    doLast {
        delete "$buildDir/libs"
    }
}

bootJar {
    archivesBaseName = 'boot-server'
    project.version =  '0.0.1' //artifactId + gitHashAndBuildTime()
    excludes = ["*.jar"]
    manifest {
        attributes "Manifest-Version": 1.0,
                //"Main-Class": "App",
                'Class-Path': configurations.compileClasspath.files.collect { "lib/${it.name}" }.join(' ')
    }
}
// 最後執行這個
task release(type: Copy, dependsOn: [build, copyJar,copyConfigFile,copyBinFile,copyLogFile,copyOtherFile,dist])