Gradle - 下载依赖项,锁定版本并手动更新依赖项

9

问题。

Gradle 依赖管理的做法如下:

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

目标。

  • 下载并将所有依赖项工件存储在版本控制系统中;
  • 手动检查这些依赖项的更新并下载它们。
2个回答

8
我的解决方案适用于使用javaandroid插件的Gradle配置。 java插件定义了compiletestCompile配置。其中,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:+'
    )

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

    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'])
}

看起来还不错,你可以为所有任务编写一个插件,它将自动处理大部分任务。尽管我理解目标,但我不喜欢在版本控制系统中保留依赖项。在iOS开发中(使用Pods),仍然存在关于此问题的争论 - 是否将依赖项保留在版本控制系统中;) +1! - Opal
好主意关于插件!我会尝试。 - mixel
如有任何问题,请随时询问。 - Opal
@Opal 关于将依赖项保留在版本控制系统下。每个项目都有一些先决条件和要求,以成功构建 - 操作系统、工具(例如Java、Gradle、Android SDK)、互联网连接、远程存储库等。我总是将这些要求的数量减少到合理的最小工具集(操作系统、Java、Gradle、Android SDK)。如果我不将依赖项包含在版本控制系统中,则将它们作为git模块包含在项目中,这些存储库位于原始项目的同一主机上。 - mixel
这可能看起来像是偏执狂,但比起如果它们将从Maven仓库或CocoaPods中删除而错过必需的依赖项,这种方式更好。 - mixel

2
为了使构建过程可重复,需要将下载的依赖项(库/等版本)锁定到硬编码的版本。现在Gradle 4.8及更高版本中,我们将拥有内置的“依赖项锁定”支持。如果有人使用动态版本(M.m.p/i)Major.minor.patch/interimBranch等(例如:4.+或3.1.+)或版本范围从二进制存储库工具(例如:Artifactory / Nexus)中获取构件,则这将极大地帮助使构建过程可重复。
任何使用Gradle 4.8+版本的Gradle用户都应该开始使用此新功能。 https://docs.gradle.org/4.8/userguide/dependency_locking.html 有关Gradle 4.8版本说明,请参见:https://docs.gradle.org/4.8/release-notes.html 过去,此依赖项锁定功能是提供给Gradle社区的,并通过Netflix Nebula的FOSS插件提供给Gradle,包括https://github.com/nebula-plugins/gradle-dependency-lock-pluginhttps://plugins.gradle.org/plugin/nebula.dependency-lock

很好,我们在Gradle中有内置的部分来更新锁定状态,但是......那如果要以其他方式修改该锁定状态呢?(例如删除,在开发时能够忽略它等) - dtc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接