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'

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...