Gradle 笔记 2
创始人
2024-01-21 05:49:14
0

Scope

[java]
compileOnly
runtimeOnly
implementation
testCompileOnly
testRuntimeOnly
testImplementation

[java-library]
api
compileOnlyApi

[war]
providedCompile

[exclude]
implementation(‘org.xx:xx:xx’) {
exclude group: ‘org.xx’
exclude module: ‘slf4j-api’ // or
exclude group: ‘org.xx’, module: ‘slf4j-api’
}

[transitive]
implementation(‘org.hibernate:hibernate-core:3.6.3.Final’) {
transitive(false) // no allow transitive
}

[enfore]
implementation(‘org.slf4j:slf4j-api:1.4.0!!’)

fast-fail

configurations.all() {
Configuration config -> config.resolutionStrategy.faillOnVersionConflict()
}

gradle command

gradle tasks
gradle build
gradle run
gradle tasks --all
gradle dependencies
gradle properties

commandline args

–max-workers --parallel --no-parallel -Dorg.gradle.logging.level=quiet
-q/quiet -w/warn -i/info -d/debug -x/exclude-task --rerun-tasks # ignore up-to-date
–continue # generate build report

e.g. gradle init --type pom
e.g. gradle myTask is equal to gradle mT

dynamic invoke

new A().printA()
// equivalent
def a = new A()
invokeVirtual(a, 'printA')

Domain-Sepcific Languages

// equivalent to: turn(left).then(right)
turn left then right// equivalent to: take(2.pills).of(chloroquinine).after(6.hours)
take 2.pills of chloroquinine after 6.hours// equivalent to: paint(wall).with(red, green).and(yellow)
paint wall with red, green and yellow// with named parameters too
// equivalent to: check(that: margarita).tastes(good)
check that: margarita tastes good// with closures as parameters
// equivalent to: given({}).when({}).then({})
given { } when { } then { }// equivalent to: select(all).unique().from(names)
select all unique() from names// equivalent to: take(3).cookies
// and also this: take(3).getCookies()
take 3 cookies

Delegate & Closure

Closure closure = {sayHello()
}
class Foo {void sayHello() {println("hello")}
}
def foo = new Foo()
closure.delegate = foo
closure() // invoke

equivalent to internal Class

Task

task A {}
task(map, 'B')
tasks.create('C') {}
tasks.register('D') {} // Lazy loading// use exist Type
tasks.register('myClean', Delete) { delete buildDir
}task sourcesJar(type: Jar, dependsOn: classes) {classifier = 'sources'from sourceSets.main.allSource
}task javadocJar(type: Jar, dependsOn: javadoc) {classifier = 'javadoc'from javadoc.destinationDir
}4.times {tasks.register("task$it") {doLast {println "I'm task number $it"}}
}

method: tasks.findByName / getByName / findByPath / getByPath(“:project-a”)
addRule(“description”) { taskName -> task(taskName) { doLast { print $taskName } } }

taskName.onlyIf { !project.hasProperty(‘a’) } // return if excute
defaultTasks ‘taskA’, ‘taskB’ // project default execute Task

Daemon

jvm start and shutdown is too heavy for system, better than keep running

gradle connect daemon throught socket

Core Object

  1. Project

  2. Task

  3. Gradle

  4. Gradle 初始化构建, 全局单例

  5. Project pom.xml 模块唯一对应

  6. Settings 项目唯一, 一般只用到 include 方法

  7. Task 前面的有向无环图基于 Task 对象

Life Cycle

Initialzation
settings.gradle Gradle#settingsEvaluated
build.gradle Gradle#projectsLoaded
Configuration
allprojects: Gradle#beforeProject and Project#beforeEvaluate
subprojects: Gradle#beforeProject and Project#beforeEvaluate
SubProjectA: Gradle#beforeProject and Project#beforeEvaluate
EXECUTE build.gradle 配置段脚本
SubProjectA: Gradle#afterProject and Project#afterEvaluate
ALL READY:
Gradle#projectsEvaluated
Gradle#taskGraph#whenReady
Execution
TaskExecutionGraph#beforeTask
EXECUTE Task Action
TaskExecutionGraph#afterTask
ALL EXECUTED: Gradle#buildFinish

buildscript

plugins {id 'java'id 'org.springframework.boot' version '2.6.3'id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}group = 'com.xxx.xx'
version = '1.0-SNAPSHOT'repositories {mavenCentral()
}dependencies {implementation 'org.springframework.boot:spring-boot-starter-web'testImplementation 'org.springframework.boot:spring-boot-starter-test'
}tasks.named('test') {// useJUnit() for junit fouruseJUnitPlatform() // for junit jupiter
}// or
buildscript {repositories {maven { url 'https://maven.aliyun.com/repository/public' }}dependencies {classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.4.1'}
}apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

相关内容

热门资讯

保存时出现了1个错误,导致这篇... 当保存文章时出现错误时,可以通过以下步骤解决问题:查看错误信息:查看错误提示信息可以帮助我们了解具体...
汇川伺服电机位置控制模式参数配... 1. 基本控制参数设置 1)设置位置控制模式   2)绝对值位置线性模...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
表格中数据未显示 当表格中的数据未显示时,可能是由于以下几个原因导致的:HTML代码问题:检查表格的HTML代码是否正...
本地主机上的图像未显示 问题描述:在本地主机上显示图像时,图像未能正常显示。解决方法:以下是一些可能的解决方法,具体取决于问...
表格列调整大小出现问题 问题描述:表格列调整大小出现问题,无法正常调整列宽。解决方法:检查表格的布局方式是否正确。确保表格使...
不一致的条件格式 要解决不一致的条件格式问题,可以按照以下步骤进行:确定条件格式的规则:首先,需要明确条件格式的规则是...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...