Gradle管理多子项目

当项目达到一定规模时,我们一般会将项目拆分成各个小的项目,以便进行管理。本篇主要介绍Gradle如何便利地管理多子项目。

项目结构

拆分项目时,可能会拆分成如下结构:

└── gradle-subprojects-example
    └── service-api
    └── service-impl
    └── web

其中,service-impl依赖于service-api,而web依赖于前者两个。那么此时,如何使用Gradle来管理这些项目呢?

settings文件

settings文件声明了项目的层次结构。在默认情况下,这个文件被命名为settings.gradle,并且和根项目的build.gradle文件放在一起。

gradle-subprojects-example/settings.gradle:

rootProject.name = 'gradle-subprojects-example'
include 'service-api'
include 'service-impl'
include 'web'

如果你的项目结构层次比较深,比如你想要映射api/service/user-service-api目录的工程,可以使用include 'api:service:user-service-api'方式来添加子项目。

集中式配置子项目

集中式配置就是将所有项目的依赖、任务都集中在根项目中进行配置管理。

gradle-subprojects-example/build.gradle:

group = 'cn.yerl'
version = '1.0.0-SNAPSHOT'

project(':service-api') {
    group = 'cn.yerl'
    version = '1.0.0-SNAPSHOT'
    apply plugin: 'java'
}
project(':service-impl') {
    group = 'cn.yerl'
    version = '1.0.0-SNAPSHOT'
    apply plugin: 'java'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.11'
        compile project(':service-api')
    }
}
project(':service-api') {
    group = 'cn.yerl'
    version = '1.0.0-SNAPSHOT'
    apply plugin: 'war'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.11'
        compileOnly 'javax.servlet:servlet-api:2.5'
        compile 'org.springframework:spring-webmvc:4.3.3.RELEASE'
    }
}

上面可以发现,重复写了好几次group、version、repositories等等,这些在本文后面有提到如何进行优化。

以上便是集中式项目配置管理的例子,将所有子项目相关的配置信息都存放于根项目的build.gradle中,便于管理。

分布式配置子项目

当项目达到一定规模时,子项目越来越多,自定义任务也越来越多的时候,集中式项目配置的缺陷就暴露出来了。此时可以使用分布式配置子项目。

分布式配置子项目的意思就是,将build.gradle中的内部拆分到每个子项目的build.gradle文件中。拆分后,文件内容如下:

gradle-subprojects-example/build.gradle

group = 'cn.yerl'
version = '1.0.0-SNAPSHOT'

gradle-subprojects-example/service-api/build.gradle

group = 'cn.yerl'
version = '1.0.0-SNAPSHOT'
apply plugin: 'java'

sourceCompatibility = 1.6

gradle-subprojects-example/service-impl/build.gradle

group = 'cn.yerl'
version = '1.0.0-SNAPSHOT'
apply plugin: 'java'

sourceCompatibility = 1.6

repositories {
    mavenCentral()
}
    
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile project(':service-api')
}

gradle-subprojects-example/web/build.gradle

group = 'cn.yerl'
version = '1.0.0-SNAPSHOT'
apply plugin: 'war'

sourceCompatibility = 1.6

repositories {
    mavenCentral()
}
    
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compileOnly 'javax.servlet:servlet-api:2.5'
    compile 'org.springframework:spring-webmvc:4.3.3.RELEASE'
}

定义公共行为

在上面的代码清单中,需要为每个项目定议group、version等属性,需要将java插件分别应用于每个子项目,需要为每个项目都配置一次repositories等。在相当小的项目中,这似乎不是什么大的问题,但是如果子项目有十数个时,就会变成体力劳动,相当无趣。

如何定义公共行为呢,这里用到了allprojectssubprojects方法来改进现有的代码。

  • allprojects: 包含根项目的所有项目
  • subprojects: 不包含根项目的所有子项目

gradle-subprojects-example/build.gradle

allprojects {
    group = 'cn.yerl'
    version = '1.0.0-SNAPSHOT'
}

subprojects {
    apply plugin: 'java'
    
    jar {
        sourceCompatibility = 1.6
        targetCompatibility = 1.6
    }
    
    repositories {
        mavenLocal()
        mavenCentral()
    }
    
    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.11'
    }
}

gradle-subprojects-example/service-api/build.gradle


gradle-subprojects-example/service-impl/build.gradle

dependencies {
    compile project(':service-api')
}

gradle-subprojects-example/web/build.gradle

dependencies {
    compileOnly 'javax.servlet:servlet-api:2.5'
    compile 'org.springframework:spring-webmvc:4.3.3.RELEASE'
}

可以看到,通过定义公共行为,子项目仅需关注自己与别的项目的不同的行为即可,这样可以大大减少代码的冗余量。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,288评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,866评论 25 709
  • 去年有段时间得空,就把谷歌GAE的API权威指南看了一遍,收获颇丰,特别是在自己几乎独立开发了公司的云数据中心之后...
    骑单车的勋爵阅读 20,814评论 0 41
  • 无论是SaaS服务,还是单独开发APP,我们都需要回答这个问题:园区,或者说租户,需要一个APP吗?如果这个问题直...
    上班的家阅读 535评论 0 0
  • “手上提个破旧葫芦,还来冒充什么大户人家,你个要饭的书生,给老子滚开!”,一个满脸横肉的大汉恶狠狠的对一个穿...
    树下的老石头阅读 409评论 0 2