IDEA中使用springBoot+gradle構建多模組專案
阿新 • • 發佈:2018-12-30
最近專案中用到了springboot+gradle來構建多模組專案,寫這篇文章純當作筆記吧,同時也分享給大家。
gradle和maven是目前很便捷的兩款專案構建工具,都有各自的優勢。據官方統計,gradle比maven構建專案時快100倍以上。廢話不多說,下面介紹一下如何使用gradle建立多模組化工程。
然後,建立多模組。這裡以webOne和webTwo模組為例,先建立兩個目錄:
Windows下建立命令如下:
其次,在webOne模組下新建settings.gradle檔案,配置對webTwo模組的依賴,如下:
使用瀏覽器訪問http://127.0.0.1:8080/test1,瀏覽器顯示spring boot multiple modules test。
使用瀏覽器訪問http://127.0.0.1:8080/test2/10/5,得到結果number1+number2=15.
gradle和maven是目前很便捷的兩款專案構建工具,都有各自的優勢。據官方統計,gradle比maven構建專案時快100倍以上。廢話不多說,下面介紹一下如何使用gradle建立多模組化工程。
1.建立專案
首先建立專案,名稱為 myGradleProject,建立命令如下:
mkdir myGradleProject
cd myGradleProject
gradle init
執行如下:
這時候的專案結構如下:
然後,建立多模組。這裡以webOne和webTwo模組為例,先建立兩個目錄:
Windows下建立命令如下:
Linux下建立命令如下:mkdir webOne\src\main\java,webOne\src\main\resource,webOne\src\test\java,webOne\src\test\resource mkdir webTwo\src\main\java,webTwo\src\main\resource,webTwo\src\test\java,webTwo\src\test\resource
mkdir -p webOne/src/main/{java,resources} webOne/src/test/{java,resource}
mkdir -p webTwo/src/main/{java,resources} webTwo/src/test/{java,resource}
此時的目錄結構如下:
2.修改配置
我們首先修改根目錄下的 settings.gradle 檔案,引入子模組:
include 'webOne','webTwo'
編輯根目錄下的 build.gradle,具體配置如下:
// 所有子專案的通用配置 subprojects { apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' version = '1.0.0' // JVM 版本號要求 sourceCompatibility = 1.8 targetCompatibility = 1.8 // java編譯的時候預設狀態下會因為中文字元而失敗 [compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8' //定義版本號 ext { springBootVar = '1.4.5.RELEASE' } repositories { mavenLocal() maven { url "http://maven.aliyun.com/nexus/content/groups/public" } mavenCentral() jcenter() maven { url "http://repo.spring.io/snapshot" } maven { url "http://repo.spring.io/milestone" } maven { url 'http://maven.springframework.org/release' } maven { url 'http://maven.springframework.org/milestone' } } dependencies { compile( ) testCompile( ) } jar { manifest { attributes("Implementation-Title": "Gradle") } } configurations { // 所有需要忽略的包定義在此 //all*.exclude group: 'commons-httpclient' //all*.exclude group: 'commons-logging' //all*.exclude group: 'commons-beanutils', module: 'commons-beanutils' } // 顯示當前專案下所有用於 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" } } }
其次,在webOne模組下新建settings.gradle檔案,配置對webTwo模組的依賴,如下:
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 = 'webOne' ext { springBootVar = '1.4.5.RELEASE' } dependencies { compile project(':webTwo') // spring boot compile "org.springframework.boot:spring-boot-starter-web:$springBootVar" testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVar" }
最後,在webTwo模組下新建settings.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 = 'webTwo'
ext {
}
bootRepackage{
enabled = false
}
build{
}
dependencies {
}
configurations {
// 所有需要忽略的包定義在此
//all*.exclude group: 'commons-httpclient'
//all*.exclude group: 'commons-beanutils', module: 'commons-beanutils'
}
3.編寫測試程式碼
使用intelliJ Idea匯入專案(專案路徑D:\myproject\myGradleProject)。新增webOne模組專案啟動類:webOne\src\main\java\com\forMelo\Application.java,程式碼如下:
package main.java.com.forMelo;
/**
* Created by songqiuming on 2018/1/7.
*/
@ComponentScan( basePackages = {"com.forMelo"})
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(8080);
}
}
新增webOne模組測試類webOne\src\main\java\com\forMelo\controller\test.java,程式碼如下:package main.java.com.forMelo.controller;
/**
* Created by songqiuming on 2018/1/7.
*/
@Controller
public class test {
@RequestMapping(value = "/test1", method = RequestMethod.GET)
public String test1(){
return "spring boot multiple modules test";
}
@RequestMapping(value = "/test2/{number1}/{number2}", method = RequestMethod.GET)
@ResponseBody
public String test2(@PathVariable int number1, @PathVariable int number2) {
return number1 + "+" + number2 + "=" + testUtils.add(number1,number2);
}
}
新增webTwo模組測試類webTwo\src\main\java\com\forMelo\testUtil.java
package main.java.com.forMelo;
/**
* Created by songqiuming on 2018/1/7.
*/
public class testUtil {
public static int add(int a, int b){
return a + b;
}
}
此時的專案結構下圖:
4.測試Demo
執行啟動類中main方法。使用瀏覽器訪問http://127.0.0.1:8080/test1,瀏覽器顯示spring boot multiple modules test。
使用瀏覽器訪問http://127.0.0.1:8080/test2/10/5,得到結果number1+number2=15.