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'

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...