您现在的位置是:首页 > 文章详情

创建一个基于Spring IO Platform的多模块Gradle项目

日期:2018-09-14点击:430

最近有同学问我,能不能让我弄一个基于Spring-Platform的多模块Gradle项目的例子,最近终于试着弄了一下,所以写篇文章介绍一下。

首先介绍一下Spring IO Platform,它主要为了帮助我们管理项目中庞大的类库的版本。做过项目的同学都肯定被大量类库的版本号所困扰,如果要进行项目升级的话一堆类库要不要进行升级,升级会不会出现不兼容情况?如果使用Spring IO Platform的话,它会帮我们统一管理这些版本,Spring IO Platform一升级,所有相关类库版本都会升级,而且Spring IO Platform会保证它们之间的兼容性。我之前也写过一篇文章《Spring IO Platform 简介》介绍Spring IO Platform。

Gradle的话大家可能不太熟悉,但是我是非常喜欢使用Gradle,因为确实比Maven好用多了,不过之前都是单个的简单Gradle项目,正好我也没试过多模块互相依赖的Gradle,正好也来研究一下。

创建项目

创建父项目

首先打开Idea,创建一个空的Gradle项目。父项目是之后两个子项目的容器,父项目本身没有任何代码,所以这里什么也不需要选。

img_d8abf34edc003624588dcbc4d5d1dfdc.png
创建父项目

创建完成后在build.gradle中添加以下代码:

buildscript { repositories { jcenter() } dependencies { classpath 'io.spring.gradle:dependency-management-plugin:1.0.0.RELEASE' } } apply plugin: 'io.spring.dependency-management' repositories { mavenCentral() } dependencyManagement { imports { mavenBom 'io.spring.platform:platform-bom:Cairo-SR4' } } 

这些代码导入了Spring IO Platform插件,让Spring IO Platform来管理项目依赖。有了这些代码,之后的依赖项就不需要手动指定版本好了。

创建booklist子模块

然后来创建两个子项目。首先在Idea中选中项目窗格中选中父项目,然后点击File->New-Module...,来创建一个名为booklist的子项目,类库那里只选择Java。如果前面不选择父项目,那么Idea只会创建一个独立的模块,而非我们想要的Gradle子模块。

编辑一下build.gradle文件,将版本号删掉。

plugins { id 'java' } group 'yitian.study' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit' } 

然后在booklist模块中,我们随便写点东西,比方说我就写了这么两个文件:

// IBookList接口 package yitian.study.booklist; import java.util.List; public interface IBookList { List<String> getBookList(); } // MyBookList实现类 package yitian.study.booklist; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class MyBookList implements IBookList { @Override public List<String> getBookList() { return Stream.of("金瓶梅", "少年阿宾", "少妇白洁") .collect(Collectors.toList()); } } 

创建webapp子模块

这个模块是一个基于Spring Boot的webapp项目,用来显示刚才的booklist。按照同样的方法创建子模块webapp,然后在build.gradle中添加以下内容:

plugins { id 'org.springframework.boot' version '2.0.5.RELEASE' id 'java' } jar { baseName = 'mywebapp' version = '0.0.1-SNAPSHOT' } repositories { jcenter() } dependencies { compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-starter-thymeleaf") compile("org.springframework.boot:spring-boot-devtools") testCompile("org.springframework.boot:spring-boot-starter-test") } 

创建完毕后,查看一下父项目的settings.gradle文件,应该会看到类似下面的内容,被包括的正是两个子模块。如果没有包括类似的内容, 说明创建子模块的时候没选中父项目,导致Idea不能自动生成这些gradle配置。

rootProject.name = 'multi-module-spring-platform-webapp-sample' include 'booklist' include 'webapp' 

配置依赖关系

这时候,打开Idea的Gradle窗格,应该会看到一片红色:


img_b8800d20475ef25fe7708c580e69b232.png
未处理好的依赖关系

如果全部展开的话,会发现所有类库都是解析失败的状态(Unable to resolve),这说明我们前面配置的有问题。其实,这和Gradle多模块的依赖实现有关系。默认情况下,每个模块的build.gradle的作用域只有本模块自身,也就是说,我们前面配置的Spring IO Platform父项目只能管理自己的依赖,这些设置无法作用到子模块上。

配置全局依赖

如果要让所有子模块共享依赖,需要在父项目中配置allprojects部分,这一部分会在所有子模块中共享。编辑父项目的build.gradle文件,改成下面这样的。这样一来,Spring IO Platform就可以管理所有子项目的依赖了。

group 'yitian.study' version '1.0-SNAPSHOT' buildscript { repositories { jcenter() } dependencies { classpath 'io.spring.gradle:dependency-management-plugin:1.0.0.RELEASE' } } allprojects { apply plugin: 'io.spring.dependency-management' repositories { mavenCentral() } dependencyManagement { imports { mavenBom 'io.spring.platform:platform-bom:Cairo-SR4' } } } 

如果再观察一下Idea的Gradle窗格,会发现这时候就没有那些红色波浪线了。

配置模块间依赖

子模块之间也可能存在依赖关系,比方说前面创建的webapp模块要显示booklist内容,那么webapp肯定要依赖booklist。所以需要在webapp的build.gradle中进行一些修改:

dependencies { // 引用另一个模块booklist compile project(':booklist') compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-starter-thymeleaf") compile("org.springframework.boot:spring-boot-devtools") testCompile("org.springframework.boot:spring-boot-starter-test") } 

这样一来,在webapp子模块中,也可以访问booklist子模块中的booklist包以及其中的代码了。

// WebApplication.java package yitian.study; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import yitian.study.booklist.IBookList; import yitian.study.booklist.MyBookList; @SpringBootApplication public class WebApplication { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } @Bean public IBookList myBookList() { return new MyBookList(); } } // MainController.java package yitian.study.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import yitian.study.booklist.IBookList; @Controller public class MainController { @Autowired private IBookList myBookList; @RequestMapping("/") String index(Model model) { model.addAttribute("booklist",myBookList.getBookList()); return "index"; } } 

最终项目结构如图所示。

img_4e3c0acdc72f4fb34a6e99b91fe77fae.png
项目结构

还有一些Thymeleaf的代码我就不贴了,直接看一下webapp显示即可。

img_53429f3450d8bdac3f7584636959fa29.png
最终效果

以上就是一个简单的基于Spring IO Platform的多模块Gradle项目示例,向大家简单介绍一下Gradle多模块项目的用法,希望对大家有所帮助。项目已经上传到Github,有兴趣的同学可以看看。

原文链接:https://yq.aliyun.com/articles/653993
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章