侧边栏壁纸
  • 累计撰写 251 篇文章
  • 累计创建 138 个标签
  • 累计收到 16 条评论

目 录CONTENT

文章目录

Gradle-Springboot 项目打包

Sherlock
2018-06-27 / 0 评论 / 0 点赞 / 2577 阅读 / 5285 字 / 编辑
温馨提示:
本文最后更新于 2023-10-09,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

运行 gradle bootRepackage 命令即可打包,输出到 build/libs
添加apply plugin: 'application'后可以打包输出到 build/distributions 目录

buildscript {  
    ext {
        springBootVersion = '1.5.14.RELEASE'
        springIOVersion = '1.0.0.RELEASE'
    }

    // gradle 脚本自身需要使用的资源
    repositories {
        //mavenCentral()
        maven {
            url "http://maven.aliyun.com/nexus/content/groups/public/"
        }        
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        // 热部署
        classpath 'org.springframework:springloaded:1.2.8.RELEASE'
        classpath "io.spring.gradle:dependency-management-plugin:${springIOVersion}"
    }
}

def appMainClass = 'com.sherlocky.job.Application'

// apply plugin: 'java' // 已被 java-library 取代
apply plugin: 'java-library'  
apply plugin: 'org.springframework.boot'  
// apply plugin: 'war'
// IDE 文件生成
apply plugin: 'eclipse'  
apply plugin: 'idea'

// 生成启动脚本并打包tar/zip
apply plugin: 'application'  
mainClassName = appMainClass

group 'com.sherlocky'  
version '0.0.1-SNAPSHOT'

[compileJava, compileTestJava, javadoc]*.options*.encoding = 'utf-8'
//设置jdk的版本
sourceCompatibility = 1.8  
targetCompatibility = 1.8

// 依赖配置 compile, runtime, testCompile, testRuntime(默认的,包含前面三个依赖)
configurations {  
    testRuntime
}

dependencies {  
    // compile gradle 3.0+ 已被标为过时方法,而新增了两个依赖指令,一个是implement 和api,这两个都可以进行依赖添加
    // api 指令: 完全等同于compile指令,没区别,你将所有的compile改成api,完全没问题
    api ('org.springframework.boot:spring-boot-starter-web')
    api 'com.xuxueli:xxl-job-core:1.9.1'
    // implement 指令: 对于使用了该命令编译的依赖,对该项目有依赖的项目将无法访问到使用该命令编译的依赖中的任何程序,
    // 也就是将该依赖隐藏在内部,而不对外部公开 
    // Google IO 相关话题的中提到了一个建议,就是依赖首先应该设置为implement的,如果没有错,那就用implement,如果有错,那么使用api指令,这样会使编译速度有所增快
    implementation 'com.google.guava:guava:23.0'

    testCompile('org.springframework.boot:spring-boot-starter-test')

    runtime group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat'//, version: 'xxx'
}

// 项目自身需要的资源
repositories {  
    // mavenCentral()
    maven {
        url "http://maven.aliyun.com/nexus/content/groups/public/"
    }
}
// 对于 springboot 的 gradle 项目,可以直接运行 bootRepackage 打可执行jar包,再结合 plugin: 'application' 可以打包成带执行脚本的格式

gradle dependencies

https://docs.gradle.org/4.10.2/userguide/buildingjavaprojects.html

  • api -
  • compileOnly — for dependencies that are necessary to compile your production code but shouldn’t be part of the runtime classpath
  • implementation (supersedes compile) — used for compilation and runtime
  • runtimeOnly (supersedes runtime) — only used at runtime, not for compilation
  • testCompileOnly — same as compileOnly except it’s for the tests
  • testImplementation — test equivalent of implementation
  • testRuntimeOnly — test equivalent of runtimeOnly

看一个图,比较清楚:

新配置 已弃用配置 行为
implementation compile 依赖项在编译时对模块可用,并且仅在运行时对模块的消费者可用。 对于大型多项目构建,使用 implementation 而不是 api/compile 可以显著缩短构建时间,因为它可以减少构建系统需要重新编译的项目量。 大多数应用和测试模块都应使用此配置。
api compile 依赖项在编译时对模块可用,并且在编译时和运行时还对模块的消费者可用。 此配置的行为类似于 compile(现在已弃用),一般情况下,您应当仅在库模块中使用它。 应用模块应使用 implementation,除非您想要将其 API 公开给单独的测试模块。
compileOnly provided 依赖项仅在编译时对模块可用,并且在编译或运行时对其消费者不可用。 此配置的行为类似于 provided(现在已弃用)。
runtimeOnly apk 依赖项仅在运行时对模块及其消费者可用。 此配置的行为类似于 apk(现在已弃用)。

implementationapi是取代之前的compile的,其中apicompile是一样的效果,implementation有所不同,通过implementation依赖的库只能自己库本身访问,举个例子,A依赖B,B依赖C,如果B依赖C是使用的implementation依赖,那么在A中是访问不到C中的方法的,如果需要访问,请使用api依赖。

compileOnlyprovided效果是一样的,只在编译的时候有效, 不参与打包。

runtimeOnlyapk效果一样,只在打包的时候有效,编译不参与。

testImplementationtestCompile效果一样,在单元测试和打包测试apk的时候有效。

debugImplementationdebugCompile效果相同, 在debug模式下有效。

releaseImplementationreleaseCompile效果相同,只在release模式和打包release包情况下有效。

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区