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文件夹就可以(推荐此方法)
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论