java – Gradle – 手动下载依赖项,锁定版本和更新依赖项
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Gradle – 手动下载依赖项,锁定版本和更新依赖项,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4490字,纯文字阅读大概需要7分钟。
内容图文
问题.
Gradle依赖管理如下:
>没有简单的方法来检查依赖项更新的可用性(仅使用某些第三方插件,如ben-manes/gradle-versions-plugin)并下载更新替换旧版本;
>依赖项工件从远程存储库下载,然后存储在gradle缓存中并在后续构建中重用;但是,成功编译项目不能依赖于连接到Internet,远程存储库的可用性以及这些存储库中特定版本的依赖项的存在.
目标.
>在VCS中下载并存储所有依赖项工件;
>手动检查这些依赖项的更新并下载它们.
解决方法:
我的解决方案适用于使用java或android插件的Gradle配置.
java插件定义了compile和testCompile配置.
compile用于编译项目生产源所需的依赖项. testCompile用于编译项目测试源所需的依赖项.
让我们在build.gradle中定义我们自己的配置:
configurations {
download
testDownload
}
接下来让我们创建目录:
> libs / compile / downloaded是存储下载依赖项的地方;
> libs / testCompile / downloaded是存储testDownload依赖项的地方.
接下来我们定义几个任务.
从下载配置中删除依赖项:
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/"
}
执行以上所有任务以更新依赖项:
task updateDependencies {
dependsOn cleanDownloadedDependencies, cleanDownloadedTestDependencies, downloadDependencies, downloadTestDependencies
}
接下来我们定义依赖项:
dependencies {
download(
'com.google.code.gson:gson:+',
'joda-time:joda-time:+',
)
testDownload(
'junit:junit:+'
)
然后我们告诉compile和testCompile配置应该在哪里使用用于编译的依赖项.
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'])
}
内容总结
以上是互联网集市为您收集整理的java – Gradle – 手动下载依赖项,锁定版本和更新依赖项全部内容,希望文章能够帮你解决java – Gradle – 手动下载依赖项,锁定版本和更新依赖项所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。