2012-03-03 24 views

回答

146

你可以声明在一个父脚本公共依赖:

ext.libraries = [ // Groovy map literal 
    spring_core: "org.springframework:spring-core:3.1", 
    junit: "junit:junit:4.10" 
] 

从一个孩子的脚本,然后你可以使用依赖声明,如下所示:

dependencies { 
    compile libraries.spring_core 
    testCompile libraries.junit 
} 

一起分享依赖声明高级配置选项,你可以使用DependencyHandler.create

libraries = [ 
    spring_core: dependencies.create("org.springframework:spring-core:3.1") { 
     exclude module: "commons-logging" 
     force = true 
    } 
] 

多的依赖可以根据同名共享:然后

libraries = [ 
    spring: [ // Groovy list literal 
     "org.springframework:spring-core:3.1", 
     "org.springframework:spring-jdbc:3.1" 
    ] 
] 

dependencies { compile libraries.spring }将同时添加这两种依赖关系。

的一条,你不能以这种方式分享信息是什么样的配置(范围在Maven的术语)的依赖应该被分配到。但是,从我的经验来看,最好还是要明确这一点。

+3

谢谢,这解决了我的问题,但仍然有一个问题,但..在Maven中,我们可以离开版本为空,如果这是一个lib,它很方便,因为您可以在我们的应用程序中使用它,并使dependencyManagement定义它应该采用的lib版本。你如何对Gradle做同样的事情? – 2012-03-05 13:04:37

+0

我不明白这个问题。请举一个例子。 – 2012-03-05 13:56:23

+4

Peter,ctapobep说的是,在maven中,你可以在dependencyManagement部分的父级(或聚合器)pom中声明与版本(和范围)的依赖关系。然后在“具体”pom中,你不需要重新声明版本;只是神器和groupId。基本上它告诉maven“我需要X:Y,但使用父配置的任何版本。” – 2013-04-19 23:14:43

6

这是一个很晚的回复,但你可能也想看看:http://plugins.gradle.org/plugin/io.spring.dependency-management 它提供了导入maven“bom”的可能性,并重用了'bom'中定义的定义。 当从Maven逐渐迁移到Gradle时,这当然是一个很好的帮助!现在享受它。

+0

当您想要跨多个(多个)项目共享相同的依赖关系时,它甚至是必备的。 – roomsg 2014-11-21 20:53:43

+4

虽然方便,但这个插件可能会有明显的性能影响。对于具有200个以上依赖项的30个子项目,它会将依赖性解决阶段的时间增加1分钟。对于小项目来说,它的作用就像魅力一样,尽管 – Jk1 2015-06-06 23:33:43

1

io.spring.gradle:dependency-management-plugin插件有新的摇篮3.x系列,但稳定2.X系列问题。仅供参考看看bug报告Drop support for Gradle 3 #115

在春季(main promoter of BOM usage)的情况下,你可能最终有:

buildscript { 
    repositories { 
     mavenLocal() 
     jcenter() 
    } 
    dependencies { 
     classpath 'io.spring.gradle:dependency-management-plugin:1.0.0.RELEASE' 
    } 
} 

repositories { 
    mavenLocal() 
    jcenter() 
} 

apply plugin: 'java' 
apply plugin: 'io.spring.dependency-management' 

dependencyManagement { 
    imports { 
     mavenBom 'io.spring.platform:platform-bom:Athens-SR3' 
    } 
} 

dependencies { 
    compile 'org.springframework.boot:spring-boot-starter-web' 

    testCompile 'org.springframework.boot:spring-boot-starter-test' 
} 

注意io.spring.platform:platform-bomorg.springframework.boot:spring-boot-starter-parent父所以它是compatable与Spring引导

你可以通过以下方式验证实际的相关性分辨率:

$ gradle dependencies 
$ gradle dependencies --configuration compile 
$ gradle dependencies -p $SUBPROJ 

$ gradle buildEnvironment 
$ gradle buildEnvironment -p $SUBPROJ 

或带任务:

task showMeCache { 
    configurations.compile.each { println it } 
} 

阅读官方Soring博客文章Better dependency management for Gradle了解引入io.spring.gradle:dependency-management-plugin的原因。

0

可以使用下面的代码集中的依赖关系:

gradle.properties

COMPILE_SDK_VERSION=26 
BUILD_TOOLS_VERSION=26.0.1 
TARGET_SDK_VERSION=26 
MIN_SDK_VERSION=14 

ANDROID_SUPPORT_VERSION=26.0.2 

在每一个模块添加到build.gradle

android { 
    compileSdkVersion COMPILE_SDK_VERSION as int 
    buildToolsVersion BUILD_TOOLS_VERSION as String 

    defaultConfig { 
     minSdkVersion MIN_SDK_VERSION as int 
     targetSdkVersion TARGET_SDK_VERSION as int 
     versionCode 1 
     versionName "1.0" 

    } 

} 

dependencies { 
compile "com.android.support:appcompat-v7:${ANDROID_SUPPORT_VERSION}" 
compile "com.android.support:support-v4:${ANDROID_SUPPORT_VERSION}" 
compile "com.android.support:support-annotations:${ANDROID_SUPPORT_VERSION}" 
compile "com.android.support:support-vector-drawable:${ANDROID_SUPPORT_VERSION}" 
compile "com.android.support:design:${ANDROID_SUPPORT_VERSION}" 
} 
1

本博客文章sugge st管理依赖和组作为配置: https://www.javacodegeeks.com/2016/05/manage-dependencies-gradle-multi-project-build.html

我还没有尝试过自己,但它看起来很有趣。

根项目的build.gradle

subprojects { 
    configurations { 
    commonsIo 
    } 

    dependencies { 
    commonsIo 'commons-io:commons-io:2.5' 
    } 
} 

子项目的build.gradle

configurations { 
    compile.extendsFrom commonsIo 
} 
+0

我已经尝试过了,它很有用。 – 2018-01-04 06:34:24