问题描述
的问题。
摇篮依赖管理层做出这样:
Gradle dependency management made so:
- 有没有简单的方法来检查可用性的依赖性更新(仅在使用某些第三方插件如的),并下载更新替换旧版本;
- 依赖工件被从远程仓库下载,然后存储在摇篮缓存和在随后的构建重复使用;但是你的项目成功编译不能依赖于具有远程仓库和存在的这些存储库的依赖特定版本的互联网连接,可用性。
- 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;
- 手动检查这些依赖的更新和下载。
推荐答案
使用我的解决方案适用于摇篮配置的Java
或机器人
插件。
My solution works for Gradle configuration using java
or android
plugins.
的Java
插件定义编译
和 testCompile
配置。编译
是所需编译该项目的生产源头的依赖。 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:
-
库/编译/下载
是下载
相关性将被保存; -
库/ testCompile /下载
是testDownload
相关性将被保存。
libs/compile/downloaded
is wheredownload
dependencies will be stored;libs/testCompile/downloaded
is wheretestDownload
dependencies will be stored.
接下来,我们定义几个任务。
Next we define several tasks.
从删除的依赖下载
配置:
task cleanDownloadedDependencies(type: Delete) {
delete fileTree('libs/compile/downloaded')
}
删除 testDownload
相关性配置:
task cleanDownloadedTestDependencies(type: Delete) {
delete fileTree('libs/testCompile/downloaded')
}
下载从依赖下载
配置:
task downloadDependencies(type: Copy) {
from configurations.download
into "libs/compile/downloaded/"
}
下载从 testDownload
配置的依赖关系:
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:+'
)
然后我们告诉其中编译
和 testCompile
配置应该用于编译依赖。
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
如果您使用的是机器人
插件,那么你也可以添加 androidTestDownload
配置所必需的编译依赖和运行Android设备的测试。也有一些相关性可以被设置为 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.
这是一个使用示例摇篮配置机器人
插件:
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:3.+',
'com.vk:androidsdk:1.3.9',
'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'])
}
这篇关于摇篮 - 下载的依赖,锁版本和更新手动依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!