问题
Gradle dependencies management made so:
- 没有简单的方法来检查依赖项更新的可用性(只能使用一些第三方插件,如ben-manes/gradle-versions-plugin)和下载更新替换旧版本;
- 依赖项构件从远程存储库下载,然后存储在gradle缓存中,并在后续构建中重用;但是,项目的成功编译必须不依赖于是否连接到Internet,远程存储库的可用性以及这些存储库中依赖项的特定版本的存在。
目标。
- 下载并存储VCS中的所有依赖项;
- 手动检查这些依赖项的更新并下载它们。
我的解决方案适用于使用java
或android
插件的Gradle配置。
java
插件定义compile
和testCompile
配置。compile
用于编译项目的生产源所需的依赖项。testCompile
用于编译项目的测试源所需的依赖项。
让我们在build.gradle
中定义自己的配置:
configurations {
download
testDownload
}
下面创建目录:
-
libs/compile/downloaded
将存储download
依赖项; -
libs/testCompile/downloaded
是testDownload
依赖项存储的地方。
接下来我们定义几个任务。
删除download
配置中的依赖项:
task cleanDownloadedDependencies(type: Delete) {
delete fileTree('libs/compile/downloaded')
}
删除testDownload
配置中的依赖项:
task cleanDownloadedTestDependencies(type: Delete) {
delete fileTree('libs/testCompile/downloaded')
}
从download
配置中下载依赖项:
task downloadDependencies(type: Copy) {
from configurations.download
into "libs/compile/downloaded/"
}
从testDownload
配置中下载依赖项:
task downloadTestDependencies(type: Copy) {
from configurations.testDownload
into "libs/testCompile/downloaded/"
}
执行以上所有任务来更新依赖项:
task updateDependencies {
dependsOn cleanDownloadedDependencies, cleanDownloadedTestDependencies, downloadDependencies, downloadTestDependencies
}
接下来定义依赖项:
dependencies {
download(
'com.google.code.gson:gson:+',
'joda-time:joda-time:+',
)
testDownload(
'junit:junit:+'
)
然后我们告诉compile
和testCompile
配置应该在编译时使用依赖项。
compile fileTree(dir: 'libs/compile', include: '**/*.jar')
testCompile fileTree(dir: 'libs/testCompile', include: '**/*.jar')
}
现在你可以下载或更新已经下载的依赖项:
./gradlew updateDependencies
如果你正在使用android
插件,那么你也可以添加androidTestDownload
配置,用于在Android设备上编译和运行测试所需的依赖项。还有一些依赖项可以作为aar
工件提供。
下面是使用android
插件的Gradle配置示例:
...
repositories {
...
flatDir {
dirs 'libs/compile', 'libs/compile/downloaded',
'libs/testCompile', 'libs/testCompileDownloaded',
'libs/androidTestCompile', 'libs/androidTestCompile/downloaded'
}
}
configurations {
download
testDownload
androidTestDownload
}
android {
...
}
dependencies {
download(
'com.android.support:support-v4:+',
'com.android.support:appcompat-v7:+',
'com.google.android.gms:play-services-location:+',
'com.facebook.android:facebook-android-sdk:+',
'com.vk:androidsdk:+',
'com.crashlytics.sdk.android:crashlytics:+',
'oauth.signpost:signpost-core:+',
'oauth.signpost:signpost-commonshttp4:+',
'org.twitter4j:twitter4j-core:+',
'commons-io:commons-io:+',
'com.google.code.gson:gson:+',
'org.jdeferred:jdeferred-android-aar:+'
)
compile fileTree(dir: 'libs/compile', include: '**/*.jar')
testCompile fileTree(dir: 'libs/testCompile', include: '**/*.jar')
androidTestCompile fileTree(dir: 'libs/androidTestCompile', include: '**/*.jar')
}
task cleanDownloadedDependencies(type: Delete) {
delete fileTree('libs/compile/downloaded')
}
task cleanDownloadedTestDependencies(type: Delete) {
delete fileTree('libs/testCompile/downloaded')
}
task cleanDownloadedAndroidTestDependencies(type: Delete) {
delete fileTree('libs/androidTestCompile/downloaded')
}
task downloadDependencies(type: Copy) {
from configurations.download
into 'libs/compile/downloaded/'
}
task downloadTestDependencies(type: Copy) {
from configurations.testDownload
into 'libs/testCompile/downloaded/'
}
task downloadAndroidTestDependencies(type: Copy) {
from configurations.androidTestDownload
into 'libs/androidTestCompile/downloaded/'
}
task updateDependencies {
dependsOn cleanDownloadedDependencies, cleanDownloadedTestDependencies, cleanDownloadedAndroidTestDependencies, downloadDependencies, downloadTestDependencies, downloadAndroidTestDependencies
}
fileTree(dir: 'libs/compile', include: '**/*.aar')
.each { File file ->
dependencies.add("compile",
[name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }, ext: 'aar'])
}
fileTree(dir: 'libs/testCompile', include: '**/*.aar')
.each { File file ->
dependencies.add("testCompile",
[name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }, ext: 'aar'])
}
fileTree(dir: 'libs/androidTestCompile', include: '**/*.aar')
.each { File file ->
dependencies.add("androidTestCompile",
[name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }, ext: 'aar'])
}
为了将下载的依赖版本(库/等版本)锁定到硬编码版本,以使构建可复制,现在的Gradle 4.8和更高版本,我们将内置"依赖锁"支持。如果有人使用动态版本(M.m.p/i),这将极大地有助于构建的可重复性。patch/interimBranch等(例如:4;+或3.1.+)或从二进制存储库工具(例如:Artifactory/Nexus)中提取工件的版本范围。
任何使用Gradle 4.8+版本的Gradle用户都应该开始使用这个新功能。https://docs.gradle.org/4.8/userguide/dependency_locking.htmlGradle 4.8版本说明:https://docs.gradle.org/4.8/release-notes.html
过去,这个依赖锁特性是提供给Gradle社区的,并通过一个FOSS插件提供给Gradle,该插件可从Netflix Nebula的https://github.com/nebula-plugins/gradle-dependency-lock-plugin和https://plugins.gradle.org/plugin/nebula.dependency-lock获得