简体   繁体   English

Gradle:查找使用+导入的依赖项的已解析版本

[英]Gradle: find resolved version of a dependency imported with +

I want to print the last version of a dependency in gradle. 我想在gradle中打印dependency的最后一个版本。

I added my dependency in this way : 我以这种方式添加了我的依赖:

compile 'test:test:+'

now I want to print the version of my dependency, because I want to know which version I'm using. 现在我想打印我的依赖版本,因为我想知道我正在使用哪个版本。

I'm using it in this way : 我正在以这种方式使用它:

gradle dependencyInsight --configuration compile --dependency test:test

But my output is this : 但我的输出是这样的:

+--- test:test:+ -> project : (*)

Is there anyway I can get the real version of my dependency and not the + ? 无论如何我可以得到我的依赖的真实版本而不是+

Within app module's build.gradle I've imported Square's Moshi library as follows: app模块的build.gradle我导入了Square的Moshi库,如下所示:


    dependencies {
        compile 'com.squareup.moshi:moshi:+'
    }

Then I executed following command in terminal: 然后我在终端执行了以下命令:


./gradlew app:dependencyInsight --configuration compile --dependency com.squareup.moshi:moshi

Here's the output that I've received: 这是我收到的输出:

在此输入图像描述

All easy, open hierarchy of view Project and see External Libraries 所有简单,开放的视图层次结构Project和参见外部库 在此输入图像描述

If you want to check the overview for all your dependencies, you can check with this command - 如果要检查所有依赖项的概述,可以使用此命令检查 -

Solution 1- 方案1-

./gradlew app:dependencies

Or 要么

Solution 2- 溶液2-

If you want to check for any specific dependency.you can use gradles' build-in 'dependencyInsight : - 如果你想检查任何特定的依赖项。你可以使用gradles'内置'dependencyInsight: -

gradle dependencyInsight --configuration compile --dependency compile 'test:test:+'

or 要么

Solution 3- 方案3-

You can check your project .idea folder 您可以检查您的项目.idea文件夹

inside your project -> .idea/libraries 在你的项目中 - > .idea / libraries

there also you can see the final version of dependencies used. 您还可以看到所使用的依赖项的最终版本。

Once you have added your dependency as "compile 'test:test:+'" build the project. 将依赖项添加为“compile”test:test:+'“构建项目后。

Then within the " Project " folder structure hierarchy find that dependency within " External Libraries " at the bottommost of folder structure , it will along with its version there. 然后在“ Project ”文件夹结构层次结构中,在文件夹结构的最底部的“ 外部库 ”中找到该依赖项,它将与其版本一起。 Use that version with your dependency and re-sync/build project again. 将该版本与您的依赖项一起使用,然后重新同步/构建项目。

You can do the following: 您可以执行以下操作:

  • Use the configuration that contains your jar file 使用包含jar文件的配置
  • Filter for the the jar file's name 过滤jar文件的名称
  • Print the results 打印结果

This will print the full path as well as the version. 这将打印完整路径以及版本。 You can extract just the jar name if needed. 如果需要,您可以只提取jar名称。

 task printPmdVersion << {
    FileTree pmdJar = zipTree(configurations.pmd.filter {
        dep -> dep.name.contains("pmd-core")
    }.singleFile)
    println pmdJar
}

Example of output: 输出示例:

ZIP '/home/user/java/gradle_user_home/caches/modules-2/files-2.1/net.sourceforge.pmd/pmd-core/5.4.1/28715c2f768b58759bb5b373365997c30ac35899/pmd-core-5.4.1.jar' ZIP'/home/user/java/gradle_user_home/caches/modules-2/files-2.1/net.sourceforge.pmd/pmd-core/5.4.1/28715c2f768b58759bb5b373365997c30ac35899/pmd-core-5.4.1.jar'

It's not a best practice use the '+' sign to always use the latest library version because you could not be able to have a repeatable build if you need one. 使用“+”符号始终使用最新的库版本并不是最佳做法,因为如果需要,您将无法拥有可重复的版本。

I mean, if you have to checkout your previous version of your APK from your Source Control Management system (eg Git) that you know it works fine, if you compile today (new library version could have been release)... maybe your old friend APK that was working fine... now it doesn't work fine like your latest one. 我的意思是,如果你必须从你的源代码管理系统(例如Git)检查你以前的APK版本,你知道它工作正常,如果你今天编译(新的库版本可能已经发布)...也许你的旧版本朋友APK工作正常...现在它不像你最新的那样好。

That said I suggest you using a gradle plugin like that: 那就是说我建议你使用这样的gradle插件:

https://github.com/ben-manes/gradle-versions-plugin https://github.com/ben-manes/gradle-versions-plugin

You will install in your build.gradle at project level like that: 您将在项目级别的build.gradle中安装,如下所示:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply plugin: 'com.github.ben-manes.versions'

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.github.ben-manes:gradle-versions-plugin:0.17.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And you'll find a new gradle task named dependencyUpdate that if you lunch it it will report you all your library versions compared with the latest ones: 你会发现一个名为dependencyUpdate的新gradle任务,如果你在午餐时它会报告你所有的库版本与最新版本相比:

------------------------------------------------------------
: Project Dependency Updates (report to plain text file)
------------------------------------------------------------

The following dependencies are using the latest milestone version:
 - com.github.ben-manes:gradle-versions-plugin:0.17.0
 - junit:junit:4.12

The following dependencies have later milestone versions:
 - com.android.support:appcompat-v7 [26.1.0 -> 27.0.2]
 - com.android.support.constraint:constraint-layout [1.0.2 -> 1.1.0-beta5]
 - com.android.support.test.espresso:espresso-core [3.0.1 -> 3.0.2-alpha1]
 - com.android.tools.build:gradle [3.0.1 -> 3.2.0-alpha03]
 - org.jacoco:org.jacoco.agent [0.7.4.201502262128 -> 0.8.0]
 - org.jacoco:org.jacoco.ant [0.7.4.201502262128 -> 0.8.0]
 - com.android.support.test:runner [1.0.1 -> 1.0.2-alpha1]

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM