idea+gradle+springboot 多项目创建 by:zrs
创建文件结构
1.创建项目文件根目录文件夹:
many-product
2.在many-product下创建两个文件(注意:文件名一定不能错):
build.gradle 和 settings.gradle
3.在many-product下分别创建两个项目文件:
one-service 和 two-service
4.分别给one-service 和 two-service下创建目录结构
src/main/java
src/main/resources
5.分别给one-service 和 two-service根目录下创建gradle文件
build.gradle
6.分别给src/main/resources目录下创建yml配置文件
application.yml
7.分别给src/main/java创建包文件
com/zrs
8.分别在src/main/java/com/zrs 下创建java文件
OneServiceApplication.java
TwoServiceApplication.java
9.至此,文件结构已经创建完成。完整项目目录结构如下:
many-product
|--one-service
| |--src
| | |--main
| | |--java
| | | |--com
| | | |--zrs
| | | `--OneServiceApplication.java
| | |
| | |--resources
| | `--application.yml
| `--build.gradle
|--two-service
| |--src
| | |--main
| | |--java
| | | |--com
| | | |--zrs
| | | `--TwoServiceApplication.java
| | |
| | |--resources
| | `--application.yml
| `--build.gradle
`--build.gradle
|
`--settings.gradle
---------------------------------目录结构创建结束--------------------------------------------
进行gradle配置:
1.在项目根目录下的gradle.build中配置如下信息:buildscript{
ext{
springBootVersion = '1.5.9.RELEASE'
}
repositories{
jcenter()
mavenCentral()
}
dependencies{
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'idea'
repositories{
jcenter()
mavenCentral()
}
subprojects{
buildscript{
ext{
springBootVersion = '1.5.9.RELEASE'
}
repositories{
jcenter()
mavenCentral()
}
dependencies{
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
ext{
springBootVersion = '1.5.9.RELEASE'
}
apply plugin: 'idea'
apply plugin: 'java'
repositories{
jcenter()
mavenCentral()
}
dependencies{
compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
}
}
2.在settings.gradle中配置如下:
rootProject.name = 'many-product'
include 'one-service'
include 'tow-service'
3.分别在one-service 和 two-service根目录下的build.gradle分别下配置如下:
one-service下配置:
archivesBaseName= 'one-service'
two-service下配置:
archivesBaseName= 'two-service'
4.分别在one-service 和 two-service目录下的application.yml分别下配置启动端口:one-service下配置:
server:
port: 8080
two-service下配置:
server:
port: 8082
5.分别在one-service 和 two-service目录下的java文件中, 注解@SpringBootApplication和配置启动
one-service 如下:
package com.zrs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class OneServiceApplication {
public static void main(String[] args){
SpringApplication.run(OneServiceApplication.class, args);
}
@GetMapping("/hello")
public String hello(){
return "hello world!!! one";
}
}
two-service 如下:
package com.zrs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class TwoServiceApplication {
public static void main(String[] args){
SpringApplication.run(TwoServiceApplication.class, args);
}
@GetMapping("/hello")
public String hello(){
return "hello world!!! two";
}
}