一、gradle介绍
1、一个新的maven工具,与你使用什么语言开发并无关系,均是为了方便开发的项目自动化构建工具
2、maven旧版本存在jar包冲突,而gradle完美地解决了这个问题
3、gradler可以指定从maven等仓库中拉取jar包
4、gralde取消了maven的xml配置
5、支持动态的版本依赖,使用领域特定语言Groovy进行配置,大大简化了配置方式
Maven的一个依赖项有6种scope,分别是compile、provided、runtime、test、system、import,其中compile为默认。
gradle将其简化为4种,compile、runtime、testCompile、testRuntime。
如配置一个junit的依赖包gralde只需要 “testCompile ‘junit:junit:4.+'”,在Gradle中支持动态的版本依赖,在版本号后面使用+号可以实现动态的版本管理。
dependencies {
compile('org.springframework:spring-core:2.5.6')
compile('org.springframework:spring-beans:2.5.6')
compile('org.springframework:spring-context:2.5.6')
testCompile('junit:junit:4.+')
}
//引用依赖
6、多模块依赖
6、多模块依赖开发。在面向服务的架构中通常将一个项目会被分解为多个模块。在pom文件中我们通常使用module标签进行模块的引用和传递。build.gradle中可以使用allprojects和subprojects代码块分别定义应用于所有项目或子项目中的配置。对于子模块中的定义放置在settings.gradle文件中,每一个模块代表project的对象实例,在parent的build.gradle中通过allproject或subprojects对这些对象进行操作,相比Maven更显灵活
allprojects {
task nice << { task -> println "I'm $task.project.name" }
}
7、灵活的构建模式
在maven中我们引用插件需要通过plugin标签将goal在xml中进行配置,对于复杂的创建配置更是麻烦,缺少灵活性。maven的这一做法是为了应对初期软件开发界没有一套规范的流程而设计的。在使用约定优于配置的同时这套规范也限制了一定的灵活性。对于插件的使用只能绑定在某个阶段上(phase),在复杂场景下会形成阻碍。而在gadle中则只需要以task的形式,几行代码进行关联即可。
8、插件机制
gradle在maven的基础上脱离了xml的流程限制,更加灵活的语法规则,在Gradle中任何配置都可以作为代码被执行,通过指定的语法进行关联,使得项目的构建流程完全与gradle进行了解耦,更加方便了软件的开发。
总结:简单的配置,优秀的性能,灵活的依赖是gradle优于maven被广泛使用的特点。
二、项目结构与相关文件介绍
核心思路:初始化环境等信息-->读取配置文件加载个性化配置-->执行代码
分析:通常情况下,IDEA加载项目的步骤均是相似的流程,那么配置文件build和settings就成为了我们必须了解的文件。
settings.gradle
settings.gradles是每个子模块对应的个性化配置文件,它的作用多是为了配置子模块,
通常根目录下的setting.gradle脚本文件是针对module的全局配置,代表着整个项目通用的一些默认配置
对于每个模块而言,settings相当于每个模块需要进行初始化的环境信息
build.gradle
build.gradle文件包含项目构建所使用的脚本,定义了项目的语言,项目的版本信息,第三方依赖仓库的地址,各个阶段需要引用的依赖等,相当于maven项目中的pom文件。
通常如果存在多个模块,那么则每个模块都会有一个build.gradle文件
对于每个模块而言,build文件里面描述是模块需要的个性化配置信息
gradlew.bat
windows环境下的一段gradle wrapper的运行脚本
三、使用gradle搭建简单的java项目
选择基本的项目类型
构建项目包结构
项目基础环境
项目存放名称
目录结构
build.gradle文件参数
buildscript:声明gradle脚本自身需要使用的资源,可以声明的资源包括依赖项、第三方插件、maven仓库地址等,相当于父级环境
buildscript {
repositories {
maven { url "https://repo.spring.io/plugins-release" }
}
dependencies {
classpath("io.spring.gradle:propdeps-plugin:0.0.9.RELEASE")
classpath("org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.16")
}
}
repositories:项目需要使用的依赖和仓库和资源
dependencies {
compile(project(":spring-beans"))
compile(project(":spring-core"))
optional("org.aspectj:aspectjweaver:${aspectjVersion}")
optional("org.apache.commons:commons-pool2:2.6.0")
optional("com.jamonapi:jamon:2.81")
}
//用于为模块引入依赖包
description :文件描述
description = "Spring AOP" //常用于子模块中
pulgin: 引用插件
plugins {
id "io.spring.dependency-management" version "1.0.5.RELEASE" apply false
id "org.jetbrains.kotlin.jvm" version "1.3.20" apply false
id "org.jetbrains.dokka" version "0.9.17"
id "org.asciidoctor.convert" version "1.5.8"
}
分析:build.gradle文件中不仅仅只是pom文件,还可以入python文件一样写出可执行函数方法。
exe:map为一个ExtentionAware类型的容器,本质是一个Map类型的变量。ExtentionAware接口的实现类为Project, Settings, Task, SourceSet等,ExtentionAware可以在运行时扩充属性,
而这里的ext,就是里面的一个特殊的属性而已,常用于加载外部的常量和参数信息
ext {
//容器
myMap= [
version: 27,
name : "我们自己定义了一个Map",
]
//字符串
linkHomepage = "https://projects.spring.io/spring-framework"
linkCi = "https://build.spring.io/browse/SPR"
linkIssue = "https://github.com/spring-projects/spring-framework/issues"
linkScmUrl = "https://github.com/spring-projects/spring-framework"
//依赖包名称
dependencies = [
appcompatV7': 'com.android.support:appcompat-v7:23.2.1',
design : 'com.android.support:design:23.2.1'
]
}
//应用
1、在需要使用的build.gradle中引入拥有ext的脚本【apply from: "xxxx.gradle"】
2、直接使用对象信息
myMap{
myversion rootProject.ext.myMap.version
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile rootProject.ext.dependencies.appcompatV7
compile rootProject.ext.dependencies.design
}
常见的build.gradle版本
allprojects {
group 'com.gweb'
version '1.0-SNAPSHOT'
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
}
}
buildscript {
ext {
springIOVersion = '1.0.0.RELEASE'
springBootVersion = '1.5.9.RELEASE'
}
repositories {
jcenter()
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/milestone" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:${springIOVersion}"
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 1.8
targetCompatibility = 1.8
jar {
baseName = 'gweb'
version = '0.0.1'
manifest {
attributes "Manifest-Version": 1.0, 'Main-Class': 'com.gweb.GwebApplication'
}
}
repositories {
jcenter()
mavenLocal()
mavenCentral()
}
dependencyManagement {
imports {
mavenBom 'io.spring.platform:platform-bom:Brussels-SR6'
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Brixton.SR4'
}
}
ext {
junitVersion = '4.12'
}
dependencies {
compile 'org.springframework:spring-core'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-autoconfigure'
compile 'org.springframework.boot:spring-boot-starter-tomcat'
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile "junit:junit:${junitVersion}"
}
说明:此处的文件只能用于参考,具体如何配置见官方文档,根据个人需要进行配置。
四、使用gradle搭建简单的sb项目
构建目录结构
配置项目基本信息
项目名称
构建如下目录结构
配置build.gradle文件,根据个人需要配置settings.gardle,如果没有自己创建文件,以下为模板
allprojects {
group 'com.gweb'
version '1.0-SNAPSHOT'
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
}
}
buildscript {
ext {
springIOVersion = '1.0.0.RELEASE'
springBootVersion = '1.5.9.RELEASE'
}
repositories {
jcenter()
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/milestone" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:${springIOVersion}"
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 1.8
targetCompatibility = 1.8
jar {
baseName = 'gweb'
version = '0.0.1'
manifest {
attributes "Manifest-Version": 1.0, 'Main-Class': 'com.gweb.GwebApplication'
}
}
repositories {
jcenter()
mavenLocal()
mavenCentral()
}
dependencyManagement {
imports {
mavenBom 'io.spring.platform:platform-bom:Brussels-SR6'
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Brixton.SR4'
}
}
ext {
junitVersion = '4.12'
}
dependencies {
compile 'org.springframework:spring-core'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-autoconfigure'
compile 'org.springframework.boot:spring-boot-starter-tomcat'
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile "junit:junit:${junitVersion}"
}
启动项目
方式1:传统模式通过run命令启动
方式2:通过【boot run】启动
————————————————
版权声明:本文为CSDN博主「琼歌」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36505948/article/details/111879435