1. 配置 aliyun 仓库
1.1. 1. 对当前项目起作用
在 build.gradle 中添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| buildscript { repositories { mavenLocal() maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' } mavenCentral() } }
allprojects { repositories { mavenLocal() maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' } mavenCentral() } }
|
1.2. 2. 对所有项目起作用
$HOME/.gradle/init.d 下创建 init.gradle 文件
(初始化文件相关见: https://docs.gradle.org/current/userguide/init_scripts.html)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| allprojects{ repositories { def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public' def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter' all { ArtifactRepository repo -> if(repo instanceof MavenArtifactRepository){ def url = repo.url.toString() if (url.startsWith('https://repo1.maven.org/maven2')) { project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL." remove repo } if (url.startsWith('https://jcenter.bintray.com/')) { project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL." remove repo } } } maven { url ALIYUN_REPOSITORY_URL url ALIYUN_JCENTER_URL } } }
|
2. buildScript 块的作用
buildScript 中配置的是给 build 脚本使用, 比如 task 中可以引用第三方的包; 而它之外的配置是给项目使用的.
buildScript 中也可以配置 repositories, plugin, dependency
gradle 执行脚本时, 优先执行 buildScript 中的内容, 然后才会执行剩余的脚本内容.
3. dependencies
api
implementation
compile
compileOnly
annotationProcessor
testCompile
testImplementation
testRuntimeOnly
testAnnotationProcessor
runtime
implementation(platform(“mavenBom”))
implementation(project(":module"))
4. 使用 Gradle 测试, 而不是 Junit 或 testNG
Settings / Build, Execution, Deployment / Build Tool / Gradle
Running test using -> Gradle
5. 命令
1 2 3 4 5 6 7
| # 查看命令帮助文档 gradle -q help --task $task_name
# 将 maven 项目转化为 gradle # --dsl=kotlin 表示转化成 kotlin-script 语法文件, 但是目前 **不支持** gradle init --type pom --dsl=kotlin
|
6. 应用脚本插件
//脚本插件
apply from: ‘other.gradle’
7. 常见问题
7.1. 1. 下载依赖源码
https://stackoverflow.com/questions/11474729/how-to-build-sources-jar-with-gradle
https://docs.gradle.org/6.0/userguide/plugin_reference.html
7.2. 2. Deprecated Gradle features were used in this build, making it incompatible with Gradle
本地 gradle 版本过低, 调高默认版本
在 gradle/wrapper/gradle-wrapper.properties 设置 gradle 版本
1 2 3 4 5
| distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists
|
7.3. 2. 脚本插件报找不到 sourceSets
https://stackoverflow.com/questions/52975515/unresolved-reference-sourcesets-for-gradle-kotlin-dsl
用 project.the() 替代脚本插件中的 sourceSets
8. Resource
https://docs.gradle.org/current/userguide/what_is_gradle.html#five_things
https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_separation
Gradle 编译环境参数
Gradle 核心插件
插件查询
Gradle **插件使用**
Gradle 插件介绍
Gradle 中文文档
Maven Publish
Maven Publish 使用
kotlin-dsl-samples
kotlin-examples
Gradle 中文文档