【问题标题】:Gradle - download dependencies, lock versions and update dependencies manuallyGradle - 手动下载依赖、锁定版本和更新依赖
【发布时间】:2015-10-18 05:03:11
【问题描述】:

问题。

Gradle 依赖管理是这样制作的:

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

目标。

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

【问题讨论】:

    标签: java android gradle dependency-management


    【解决方案1】:

    我的解决方案适用于使用 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 插件,那么您还可以为在Android 设备上编译和运行测试所需的依赖项添加androidTestDownload 配置。还可以将一些依赖项作为 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'])
    }
    

    【讨论】:

    • 看起来不错,你可以为所有任务编写一个插件来自动处理大部分任务。尽管我了解目标,但我不喜欢在 VCS 中保留依赖项。在 iOS 开发(使用 Pods)中,仍然存在一场关于它的战争 - 是否将依赖项保持在 VCS 下;)+1!
    • 关于插件的好主意!我会试试的。
    • 有什么问题就问吧。
    • @Opal 关于在 VCS 下保持依赖关系。每个项目都有一些成功构建的先决条件和要求——操作系统、工具(例如 Java、Gradle、Android SDK)、互联网连接、远程存储库等。我总是将这些要求的数量减少到合理的最小绝对必需工具集(操作系统、Java、Gradle、Android SDK)。如果我不在 VCS 中包含依赖项,那么我将它们作为 git 模块包含在项目中,这些存储库与原始项目的存储库位于同一主机上。
    • 这可能看起来像偏执狂,但如果它们将从 maven 存储库或 cocoapods 中删除,总比缺少必需的依赖项要好。
    【解决方案2】:

    对于锁定已下载依赖项的版本(库/等版本到硬编码版本)以使构建可重现,现在 Gradle 4.8 及更高版本,我们将内置“依赖项锁定”支持。如果有人使用动态版本 (Mmp/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 的 https://github.com/nebula-plugins/gradle-dependency-lock-pluginhttps://plugins.gradle.org/plugin/nebula.dependency-lock 提供给 Gradle 的 FOSS 插件提供

    【讨论】:

    • 很高兴我们将它作为 gradle 的内部内置部分来更新锁,但是......以其他方式修改锁状态怎么样? (删除、开发时可以忽略等)
    猜你喜欢
    • 2017-12-26
    • 2017-08-21
    • 2014-09-17
    • 2016-09-13
    • 1970-01-01
    • 2019-12-25
    • 2018-02-20
    • 2018-06-02
    • 1970-01-01
    相关资源
    最近更新 更多