How to bundle dependencies in Gradle for multiple applications

时间:2015-07-28 16:56:48

标签: gradle

I'd like to somehow enable shared dependency management between different applications using Gradle and have created a hierarchy of 'bundles' essentially packaging Java, Spring, Groovy and various test artifacts.

The root of the 'bundling' consists of config as below:

ext {

    // Spring-specific
    springVersion           = '4.1.7.RELEASE'
    springCore              = "org.springframework:spring-core:${springVersion}"
    springContext           = "org.springframework:spring-context:${springVersion}"
    springBeans             = "org.springframework:spring-beans:${springVersion}"
    springBase              = [springCore, springContext, springBeans]

    ... and more Spring

    // Miscellaneous
    sourceCompatibility = 1.8
    groovy = 'org.codehaus.groovy:groovy-all:2.4.4'
    servlet = 'javax.servlet:javax.servlet-api:3.1.0'
    ... and more
}

subprojects {

    // Enabling Groovy, Wrapper

}

With a 'Spring bundle' configured as follows:

dependencies {
    compile(
        groovy,
        springBase,
    )
}

JAR artifacts like my common-spring then depend on the above 'bundle' - and other projects depend on common-spring.

Building locally using Gradle's Maven plugin, this works when I also install the 'bundles' and 'common' to my local Maven repository. However, I am unsure if this is the way I'm 'supposed' to do it in Gradle.

An alternative (and perhaps more correct?) approach could be publishing to Artifactory and resolving artifacts from there, thus (hopefully?) not using any Maven-y stuff at all.

Or is there a completely different and simpler way I could centralize which versions I use of various third-party artifacts?

1 个答案:

答案 0 :(得分:0)

是的,这是一种分享依赖关系的好方法,实际上Gradle的构建使用了相同的技术。

一个区别是他们创建了一个地图来存储所有的包,并将它们全部包装在一个列表中。

ext.libs = [:]
libs.springCore = ["org.springframework:spring-core:${springVersion}",]
...