gradle打包

1. 默认打包

在build.gradle中配置

jar {
    manifest {
        attributes('Main-Class': 'org.x3.server.Server')
    }

}

说明:

manifest:是配置META-INF/MANIFEST.MF文件
    
主要配置:
    Main-Class:主运行类
    
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }

作用:收集compile类依赖

打包命令:gradle jar
jar包位置:build/libs

2. 打可执行包(jar)

jar {
    manifest {
        attributes('Main-Class': 'org.x3.server.Server')
    }

}

from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }

说明:

from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }

打包时,将compile类型依赖打入jar包

打包命令:gradle jar
jar包位置:build/libs

3. 打可执行包(shadow)

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "com.github.jengelman.gradle.plugins:shadow:2.0.1"
    }
}

apply plugin: "com.github.johnrengelman.shadow"

jar {
    manifest {
        attributes('Main-Class': 'org.x3.cloud.PaasServer')
    }
}

说明:

1. 先引入shadow插件
2. 执行运行主类

打包命令:gradlew shadow
jar包位置:build/libs

4. 打可执行包(shadow)-解决spring spring.schemas问题

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "com.github.jengelman.gradle.plugins:shadow:2.0.1"
    }
}

apply plugin: "com.github.johnrengelman.shadow"

shadowJar {
    println("shadowJar run....")
    zip64 true
    manifest {
        attributes("Main-Class": "PAAS.EPRIA.Common.Log.server.Server")
    }

    from("src/main/java") {
        include 'META-INF/**'
    }
    
    append("META-INF/spring.schemas")
    append("META-INF/spring.handlers")
}

说明:

spring每个模块都有一个spring.schemas和一个spring.handlers文件
打包时如果不尽兴
    原文作者:雨中星辰0
    原文地址: https://www.jianshu.com/p/f7ef02295757
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞