Gradle - 手动下载依赖项、锁定版本和更新依赖项 [英] Gradle - download dependencies, lock versions and update dependencies manually

查看:52
本文介绍了Gradle - 手动下载依赖项、锁定版本和更新依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题.

Gradle 依赖管理是这样的:

Gradle dependency management made so:

  • 没有简单的方法可以检查依赖项更新的可用性(仅使用一些第三方插件,例如 ben-manes/gradle-versions-plugin) 并下载更新替换旧版本;
  • 从远程存储库下载依赖项工件,然后存储在 gradle 缓存中并在后续构建中重用;但项目的成功编译不得依赖于是否连接到 Internet、远程存储库的可用性以及这些存储库中是否存在特定版本的依赖项.
  • there is no easy way to check availability of dependencies updates (only using some third-party plugins like ben-manes/gradle-versions-plugin) and download updates replacing old versions;
  • dependencies artifacts are downloaded from remote repositories and then stored in gradle cache and reused in succeeding builds; but successful compilation of your project must not depend on having a connection to Internet, availability of remote repositories and existence of specific versions of dependencies in these repositories.

目标.

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

推荐答案

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

My solution works for Gradle configuration using java or android plugins.

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

java plugin defines compile and testCompile configurations. compile is for dependencies that are required to compile the production source of the project. testCompile is for dependencies that are required to compile the test source of the project.

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

Let's define our own configurations in build.gradle:

configurations {
    download
    testDownload
}

接下来让我们创建目录:

Next let's create directories:

  • libs/compile/downloaded 是存储 download 依赖项的地方;
  • libs/testCompile/downloaded 是存储 testDownload 依赖项的地方.
  • libs/compile/downloaded is where download dependencies will be stored;
  • libs/testCompile/downloaded is where testDownload dependencies will be stored.

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

Next we define several tasks.

download 配置中删除依赖项:

Delete dependencies from download configuration:

task cleanDownloadedDependencies(type: Delete) {
    delete fileTree('libs/compile/downloaded')
}

testDownload 配置中删除依赖项:

Delete dependencies from testDownload configuration:

task cleanDownloadedTestDependencies(type: Delete) {
    delete fileTree('libs/testCompile/downloaded')
}

download配置下载依赖:

task downloadDependencies(type: Copy) {
    from configurations.download
    into "libs/compile/downloaded/"
}

testDownload 配置下载依赖项:

Download dependencies from testDownload configuration:

task downloadTestDependencies(type: Copy) {
    from configurations.testDownload
    into "libs/testCompile/downloaded/"
}

执行以上所有任务更新依赖:

Execute all above tasks to update dependencies:

task updateDependencies {
    dependsOn cleanDownloadedDependencies, cleanDownloadedTestDependencies, downloadDependencies, downloadTestDependencies
}

接下来我们定义我们的依赖项:

Next we define our dependencies:

dependencies {
    download(
            'com.google.code.gson:gson:+',
            'joda-time:joda-time:+',
    )
    testDownload(
            'junit:junit:+'
    )

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

And then we tell where compile and testCompile configurations should take dependencies used for compilation.

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

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

Now you can download or update already downloaded dependencies:

./gradlew updateDependencies

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

If you are using android plugin then you can also add androidTestDownload configuration for dependencies that are required for compilation and running tests on Android device. Also some dependencies can be provided as aar artifacts.

这是使用 android 插件的 Gradle 配置示例:

This is the example for Gradle configuration using android plugin:

...

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 - 手动下载依赖项、锁定版本和更新依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆