Gradle下搭建SpringCloud分布式环境
1.idea配置好gradle
这一步不必多说, 常规操作
2.创建一个空的gradle项目
3.创建好后, 注意版本号
4.在本地配置好gradle,将idea的gradle配置改为本地(可选项)
5修改build.gradle,注意springcloud的版本需要和springboot版本对应, 不然出大问题(不可盲目追新)
推荐
buildscript {
ext {
springBootVersion = '2.2.5.RELEASE'
springCloudVersion = 'Hoxton.SR1'
}
repositories {
mavenLocal()
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
allprojects {
group 'com.qpf'
version '1.0-SNAPSHOT'
apply plugin: 'java'
// 指定JDK版本
sourceCompatibility = 1.8
targetCompatibility = 1.8
//指定编码格式
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
repositories {
mavenLocal()
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
mavenCentral()
}
}
subprojects {
//dependency-management 插件
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
//spring bom helps us to declare dependencies without specifying version numbers.
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootVersion}"
}
}
jar {
manifest.attributes provider: 'gradle'
}
}
5、添加.gitignore文件
根据需要自己修改内容即可,若idea中没有该插件,自己添加一个,不往git上提交的可以忽略此步
6.创建子模块
点gradle
7、检查子模块有没有添加到settings.gradle中,若没有手动添加
8、处理:将子模块下的build.gradle文件中除了dependencies中的内容之外全部删除,添加
apply plugin: 'org.springframework.boot'
例如:
apply plugin: 'org.springframework.boot'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
9、删除子模块下多余文件(可选,自己决定)
10.点开右上角的gradle, 将与父模块并列的子模块删除
点开父模块, 可以看到其下的子模块
说明:删除模块时不要直接删文件,需要从项目中移除然后删除File->Project Structure…
然后删除根项目settings.gradle中的include(‘eureka’),最后从项目中删除子模块文件即可
提示:若不想修改gradle本地文件编译,则不用删除子模块中gradle文件夹就可以(推荐此方法)
参考: https://blog.csdn.net/weixin_41996632/article/details/105041256
花了半天时间去找的资料 + 亲身试验试错,