gpt4 book ai didi

java - Gradle - 手动下载依赖、锁定版本和更新依赖

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:49:56 24 4
gpt4 key购买 nike

问题。

Gradle 依赖管理是这样的:

  • 没有简单的方法来检查依赖项更新的可用性(只能使用一些第三方插件,如 ben-manes/gradle-versions-plugin )和下载更新以替换旧版本;
  • 依赖项 Artifact 从远程存储库下载,然后存储在 gradle 缓存中,并在后续构建中重复使用;但项目的成功编译不能依赖于与 Internet 的连接、远程存储库的可用性以及这些存储库中依赖项的特定版本的存在。

目标。

  • 下载所有依赖项并将其存储在 VCS 中;
  • 手动检查这些依赖项的更新并下载它们。

最佳答案

我的解决方案适用于使用 javaandroid 插件的 Gradle 配置。

java 插件定义了compiletestCompile 配置。compile 用于编译项目的生产源代码所需的依赖项。 testCompile 用于编译项目测试源所需的依赖。

让我们在build.gradle中定义我们自己的配置:

configurations {
download
testDownload
}

接下来让我们创建目录:

  • libs/compile/downloaded 是存放download依赖的地方;
  • libs/testCompile/downloaded 是存储 testDownload 依赖项的地方。

接下来我们定义几个任务。

下载配置中删除依赖:

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:+'
)

然后我们告诉 compiletestCompile 配置应该在哪里获取用于编译的依赖项。

    compile fileTree(dir: 'libs/compile', include: '**/*.jar')
testCompile fileTree(dir: 'libs/testCompile', include: '**/*.jar')
}

现在您可以下载或更新已下载的依赖项:

./gradlew updateDependencies

如果您使用的是 android 插件,那么您还可以为在 Android 设备上编译和运行测试所需的依赖项添加 androidTestDownload 配置。此外,一些依赖项可以作为 aar Artifact 提供。

这是使用 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'])
}

关于java - Gradle - 手动下载依赖、锁定版本和更新依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31662781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com