首页 文章

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

提问于
浏览
5

The issue.

Gradle依赖管理如下:

  • 没有简单的方法来检查依赖项更新的可用性(仅使用某些第三方插件,如ben-manes/gradle-versions-plugin)并下载更新替换旧版本;

  • dependencies工件从远程存储库下载,然后存储在gradle缓存中并在后续版本中重用;但是,成功编译项目不能依赖于连接到Internet,远程存储库的可用性以及这些存储库中特定版本的依赖项的存在 .

The goal.

  • 在VCS中下载并存储所有依赖项工件;

  • 手动检查这些依赖项的更新并下载它们 .

2 回答

  • 0

    我的解决方案适用于使用 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'])
    }
    
  • 5

    为了将下载的依赖项(库/ etc版本到硬编码版本)的版本锁定以使构建可重现,现在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 For 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插件提供 .

相关问题