Java笔记··By/蜜汁炒酸奶

边学边用Gradle:Gradle的脚本结构

前言

一个简单的Gralde脚本,主要包含如下内容,其中标明可选的都是可以删掉的部分:

  • 插件引入:声明你所需的插件—如 apply plugin: ‘java’
  • 属性定义(可选):定义扩展属性— 如 ext
  • 局部变量(可选):定义局部变量— 如 def
  • 属性修改(可选):指定project自带属性—如: group ‘com.windcoder’
  • 仓库定义:指明要从哪个仓库下载jar包—如:repositories
  • 依赖声明:声明项目中需要哪些依赖—如:dependencies
  • 自定义任务(可选):自定义一些任务–如:task printWindCoder
//定义扩展属性(给脚本用的脚本)
buildScript {
    repositories {
         mavenCentral()
    }
}
//应用插件,这里引入了Gradle的Java插件,此插件提供了Java构建和测试所需的一切。
apply plugin: 'java'
//定义扩展属性(可选)
ext {
    windcoder="windcoder"
}
//定义局部变量(可选)
def com="com"

//修改项目属性(可选)
group 'com.windcoder'
version '1.0-SNAPSHOT'

//定义仓库,当然gradle也可以使用各maven库 ivy库 私服 本地文件等
repositories {
    maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}

//定义依赖,此处定义了项目依赖
dependencies {
     compile project(":qycms-core:system-console")
}

//自定义任务(可选)
task printWindCoder {
    println "${windcoder}__${com}"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

参考:跟我学Gradle-3.2:快速入门,Gradle的脚本结构 具体的一些内容可见下面。

buildscript

官方解释为:

Configures the build script classpath for this project. The given closure is executed against this project’s ScriptHandler. The ScriptHandler is passed to the closure as the closure’s delegate. 来源:buildscript { }

配置此项目的构建脚本类路径。可声明用于编译和执行构建脚本的类路径。该类路径也用于加载构建脚本使用的插件。 简单说即设置脚本的运行环境buildscript中的声明是gradle脚本自身需要使用的资源。可以声明的资源包括依赖项、第三方插件、maven仓库地址等。 而在build.gradle文件中直接声明的依赖项、仓库地址等信息是项目自身需要的资源。 例:

buildscript {
    ext {
        springBootVersion = "1.5.8.RELEASE"
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/plugins-release" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
apply plugin: 'org.springframework.boot'
1
2
3
4
5
6
7
8
9
10
11
12
13

ext

额外的属性扩展允许将新属性添加到现有的域对象。即用于配置额外的属性。 详情:ExtraPropertiesExtension

repositories

配置该项目的存储库。支持java 依赖库管理(maven/ivy),用于项目的依赖。 Gradle需要你指定至少一个仓库作为依赖下载的地方,比如mavenCentral

repositories {
    mavenCentral()
}
1
2
3
| 仓库                                                                                             | 含义        |
| ---------------------------------------------------------------------------------------------- | --------- |
| mavenLocal()                                                                                   | 本地仓库      |
| mavenCentral()                                                                                 | 远程maven仓库 |
| maven \{name 'Custom Maven Repository',url 'http://repository.forge.cloudbees.com/release/')\} | 自定义仓库     |

自定义仓库其他写法

maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'}
1

dependencies

配置此项目的依赖关系。依赖包的定义。支持maven/ivy,远程,本地库,也支持单文件,如果前面定义了repositories{}maven 库,使用maven的依赖(我没接触过ivy。。)的时候只需要按照用类似于com.android.tools.build:gradle:0.4,gradle 就会自动的往远程库下载相应的依赖。

写法:

1、依赖通过group标识,name和version来确定,比如下面这个:

dependencies {
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.1'
}
1
2
3

2、简写

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
}
1
2
3

3、添加libs的所有jar包为依赖

dependencies {
        compile fileTree(dir: 'lib', exclude:'', include: '*.jar')
    }
1
2
3

将libs目录下所有jar文件进行编译并打包。 4、引入另一个模块

dependencies {
        compile project(":windcoder-com:test")
    }
1
2
3

即是将另一个module(等同eclipse中的library)进行编译并打包 5、buildscript代码块独有

dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
1
2
3

buildscript代码块中你可以对dependencies使用classpath声明。该classpath声明说明了在执行其余的build脚本时,class loader可以使用这些你提供的依赖项。这也正是我们使用buildscript代码块的目的。 dependencies {} 用Gradle 构建你的android程序 compile、provided、compile files、compile project四者的区别

allprojects{}

配置此项目及其每个子项目。

subprojects{}

配置该项目的子项目。

configure(rootProject){}

配置一个对象,如此处配置根项目。 Object configure(Objectobject,ClosureconfigureClosure)

bootRepackage

SpringBoot构建插件(spring-boot-gradle-plugin),有一个bootRepackage任务,它的作用是重新打包jar为可执行的jar。 所以他需要指定一个MainClass, 解决办法: 关掉bootRepackage任务

bootRepackage.enabled = false
1

或配置mainclass

springBoot {
        mainClass = "com.windcoder.nigthbook.BookApplication";
        buildInfo()
    }
1
2
3
4

spring-boot填坑 注:bootRepackage 任务已经被替换成 bootJar 和 bootWar 任务,分别用于构建可执行的 jar 包和 war 包。 【官方文档】Spring Boot 2.0 迁移指南

预览
Loading comments...
0 条评论

暂无数据

example
预览