当项目比较大,设计的业务比较多的时候,我们就会想到将项目进行拆分,类似于分布式。这样在更改某个模块时可以在不影响其它模块下更快的进行部署,方便开发/运维人员
以下是在windows平台下使用IDEA开发工具,gradle构建工具,SpringBoot来进行多模块项目简单搭建的步骤。
选择一个目录在该目录下新建 BingoProject ,打开cmd,输入gradle init 完成根目录的搭建
使用windows命令创建多个模块
创建BingoCommon模块
mkdir BingoCommon\src\main\java,BingoCommon\src\main\resources,BingoCommon\src\test\java,BingoCommon\src\test\resources
创建BingoWeb模块
mkdir BingoWeb\src\main\java,BingoWeb\src\main\resources,BingoWeb\src\test\java,BingoWeb\src\test\resources
打开IDEA导入BingoProject项目
修改BingoProject目录下的setting.gradlet添加如下代码
include 'BingoCommon','BingoWeb'
修改build.gradle配置文件
// 所有子项目的通用配置 subprojects { apply plugin: 'java' apply plugin: 'idea' version = '1.0.0' // JVM 版本号要求 sourceCompatibility = 1.8 targetCompatibility = 1.8 // java编译的时候缺省状态下会因为中文字符而失败 [compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8' repositories { mavenLocal() maven { url "http://maven.aliyun.com/nexus/content/groups/public" } mavenCentral() } jar { manifest { attributes("Implementation-Title": "Gradle") } } // 显示当前项目下所有用于 compile 的 jar. task listJars(description: 'Display all compile jars.') << { configurations.compile.each { File file -> println file.name } } gradle.projectsEvaluated { tasks.withType(JavaCompile) { options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" } } }
OK,两个模块搭建完毕,但是!!!只是整合到了这个BingoProject的gradle目录下
接下来需进行一下配置
在BingoCommon文件名上右击新建文件命名为build.gradle
添加以下配置
buildscript { repositories {
//使用的仓库优先级 mavenLocal() maven { url "http://maven.aliyun.com/nexus/content/groups/public" } maven { url "http://repo.spring.io/snapshot" } maven { url "http://repo.spring.io/milestone" } jcenter() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.5.RELEASE") } } apply plugin: 'org.springframework.boot' archivesBaseName = 'BingoCommon' ext { } bootRepackage{ enabled = false //添加原因请参考http://docs.spring.io/autorepo/docs/spring-boot/1.4.5.RELEASE/reference/html/build-tool-plugins-gradle-plugin.html#build-tool-plugins-understanding-the-gradle-plugin } build{ } dependencies { } configurations { // 所有需要忽略的包定义在此 //all*.exclude group: 'commons-httpclient' //all*.exclude group: 'commons-beanutils', module: 'commons-beanutils' }
同样再在BingoWeb项目下新建build.gradle并添加如下代码
buildscript { repositories { mavenLocal() maven { url "http://maven.aliyun.com/nexus/content/groups/public" } maven { url "http://repo.spring.io/snapshot" } maven { url "http://repo.spring.io/milestone" } jcenter() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.5.RELEASE") } } apply plugin: 'org.springframework.boot' archivesBaseName = 'BingoWeb' ext { springBootVar = '1.4.5.RELEASE' } dependencies { compile project(':BingoCommon') //要依赖的模块 // spring boot compile "org.springframework.boot:spring-boot-starter-web:$springBootVar" testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVar" }
添加测试代码
package com.bingo.hello; /** * Created by on 2017/10/17. */ public class SayHello { public void sayHello(){ System.out.println(123); } }
package com.bingo.test; import com.bingo.hello.SayHello; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestWords { @RequestMapping(value = "/hello", method = RequestMethod.GET) @ResponseBody public String test1() { SayHello hello = new SayHello(); hello.sayHello(); return "spring boot multiple modules test"; } }
package com.bingo.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; /** * Created by bingo on 2017/10/17. */ @ComponentScan( basePackages = {"com.bingo"}) //多模块项目中各模块中的包名需保持一致,否则可能会出现找不到类的情况 @EnableAutoConfiguration public class BingoApplication extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer { public static void main(String args[]) { SpringApplication.run(BingoApplication.class, args); } @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(8080); } }
启动main方法即可。
访问
查看BingoCommon中的类是否被调用
好啦,大工告成了!
原文请参考:http://blog.csdn.net/formularoom/article/details/70354562