IDEA+Gradle搭建Spring Boot项目
IDEA和Gradle创建一个Spring boot项目并不复杂,但是在搭建的时候容易遇到一些小问题,会花费不少时间。 在此记录一下在搭建过程,为以后的项目能够借鉴使用
1. 下载&安装工具
需要用到下面几个工具,请下载安装:
Java JDK 1.8+
两个地址选其一:
国内下载地址:https://oomake.com/download/java-jdk
官网下载地址:https://www.oracle.com/technetwork/java/javase/downloads/index.html
IntelliJ IDEA
(使用Ultimate
版本需自行处理License
)Jetbrain
官网下载地址:https://www.jetbrains.com/idea/download/
Gradle
安装:https://docs.gradle.org/current/userguide/installation.html
2. IDEA创建项目
1、打开 IDEA
2、菜单选择 File > New > Project...
3、然后选择Gradle
和SDK
版本,以及Java
库,如下:
4、输入GroupId
和ArtifactId
,这两个是为保证项目唯一性存在的,GroupId
一般与域名对应,ArtifactId
则是项目名称,这里我们写的是一个演示功能,设置如下:
5、接下来的界面直接点Next
,不需要配置
这样我们就创建完项目了,IDEA
帮我们自动创建了2个文件:build.gradle
和settings.gradle
;接下来还需要一些配置
3. 配置&依赖
3.1 build.gradle
配置
打开build.gradle
文件,自动生成的内容为:
plugins {id 'java'
}group 'michael.spica.gradle'
version '1.0-SNAPSHOT'repositories {mavenCentral()
}dependencies {testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}test {useJUnitPlatform()
}
需要添加一些内容,添加后build.gradle
文件内容如下:
// gradle 自身需求资源库 放头部
buildscript {repositories {maven { url 'https://maven.aliyun.com/repository/public' }// 加载其他Maven仓库mavenCentral()}dependencies {classpath('org.springframework.boot:spring-boot-gradle-plugin:2.1.1.RELEASE')// 加载插件,用到里面的函数方法}
}// 插件
//plugins {
// id 'java'
//}apply plugin: 'java'
apply plugin: 'idea'
// 使用spring boot 框架
apply plugin: 'org.springframework.boot'
// 使用spring boot的自动依赖管理
apply plugin: 'io.spring.dependency-management'// 版本信息
group 'michael.spica.gradle'
version '1.0-SNAPSHOT'// 执行项目中所使用的的资源仓库
repositories {maven { url 'https://maven.aliyun.com/repository/public' }mavenCentral()
}// 项目中需要的依赖
dependencies {// 添加 jupiter 测试的依赖testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'// 添加 jupiter 测试的依赖testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'// 添加 spring-boot-starter-web 的依赖 必须 排除了security 根据自身需求implementation('org.springframework.boot:spring-boot-starter-web') {exclude group: 'org.springframework.security', module: 'spring-security-config'}// 添加 spring-boot-starter-test 该依赖对于编译测试是必须的,默认包含编译产品依赖和编译时依赖testImplementation 'org.springframework.boot:spring-boot-starter-test'// 添加 junit 测试的依赖testImplementation group: 'junit', name: 'junit', version: '4.11'// 添加 lombokannotationProcessor 'org.projectlombok:lombok:1.18.22' // annotationProcessor代表main下代码的注解执行器testAnnotationProcessor 'org.projectlombok:lombok:1.18.22'// testAnnotationProcessor代表test下代码的注解执行器compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.22' // compile代表编译时使用的lombok
}// 第二种排除依赖方法
//configurations {
// compile.exclude group:"org.codehaus.jackson", module:"jackson-mapper-asl" // 排除依赖
//}test {useJUnitPlatform()
}
保存文件,IDEA
提示下载依赖,选择自动下载。
3.2 gradle-wrapper.properties
(版本管理)
若发现gradle
版本问题,可以修改此文件,提升gradle
版本;项目默认为:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
修改后
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
更改distributionUrl
,将版本更换为gradle-7.2-bin
4. 编写源文件
1、在项目中新建目录 src/main/java
(这是java默认可以识别的目录)
2、点选新建的 java
目录,右键选择 New > Package
新建一个包,包名:michael.spica.gradle
3、在michael.spica.gradle
包下面创建一个入口文件:MichaelSpicaGradleApplication.java
,内容:
package michael.spica.gradle;import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;/*** Created by michael on 2020/04/28.*/
@Slf4j
@SpringBootApplication
public class MichaelSpicaGradleApplication {private static final String SPRING_PROFILE_DEFAULT = "spring.profiles.default";/*** dev:开发、qa:测试、pre:预发、prod:生产;为空则默认读取“application.yml”*/private static final String PROFILE = "";private static void addDefaultProfile(SpringApplication app) {Map<String, Object> defProperties = new HashMap();defProperties.put(SPRING_PROFILE_DEFAULT, PROFILE);app.setDefaultProperties(defProperties);}private static String[] getActiveProfiles(Environment env) {String[] profiles = env.getActiveProfiles();return profiles.length == 0 ? env.getDefaultProfiles() : profiles;}public static void main(String[] args) {SpringApplication app = new SpringApplication(MichaelSpicaGradleApplication.class);addDefaultProfile(app);Environment env = app.run(args).getEnvironment();String applicationName = env.getProperty("spring.application.name");String serverPort = env.getProperty("server.port");String configServerStatus = env.getProperty("spring.profiles.active");try {String hostAddress = InetAddress.getLocalHost().getHostAddress();log.info("\n---------------------------------------------------------------------------------\n\t" +"Application '{}' is running! Access URLs:\n\t" +"Local: \t\thttp://localhost:{}\n\t" +"External: \thttp://{}:{}\n---------------------------------------------------------------------------------",applicationName,serverPort,hostAddress, serverPort);log.info("\n---------------------------------------------------------------------------------\n\t" +"Config Server: \t{}\n---------------------------------------------------------------------------------",configServerStatus == null ? "Not found or not setup for this application" : configServerStatus);} catch (UnknownHostException e) {e.printStackTrace();}}
}
4、再在michael.spica.gradle
包下创建一个目录,名称:HelloController
,这里主要用来放控制器。
5、在controller
目录下创建一个 HelloController.java
控制器文件,内容:
package michael.spica.gradle.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** Created by michael on 2020/04/28.*/
@RestController
@RequestMapping("/hello")
public class HelloController {@GetMapping("/welcome")public String welcome() {return "Hello Word";}
}
5、运行&测试
5.1 用Gradle
命令启动
gradle bootrun
5.2 IDEA
启动
打开MichaelSpicaGradleApplication.java
文件,右键选择
在通过浏览器访问http://192.168.2.157:8989/hello/welcome
,就看到效果了
至此,项目搭建完成~~
IDEA+Gradle搭建Spring Boot项目相关推荐
- Spring boot(6)---在Eclipse中搭建Spring boot 项目
Spring boot入门:在Eclipse中搭建Spring boot 项目 Eclipse中的STS插件 打开Eclipse-Help-Eclipse Marketplace-popular 下载 ...
- 手把手教你搭建Spring Boot项目
Spring Boot是目前流行的微服务框架,倡导"约定优先于配置",其设计目的是用来简化新Spring应用的初始化搭建以及开发过程.Spring Boot提供了很多核心的功能,比 ...
- 使用 spring tool suit 搭建 spring boot 项目
使用 eclipse 搭建 spring boot 项目详细步骤 相对繁琐. 使用 spring tool suit 则简单多了. 直接new一个project ,选项 spring starter ...
- 怎样把一个项目加入微服务器,构建微服务:快速搭建Spring Boot项目
Spring Boot简介: Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...
- Myeclipse下使用Maven搭建spring boot项目(第二篇)
现在需要搭建spring boot框架,并实现一个HelloWorld的项目,让程序真正运行起来. 一.在pom.xml中引入spring-boot-start-parent,spring官方的叫st ...
- springboot controller访问不到_构建微服务:快速搭建Spring Boot项目
Spring Boot简介: Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...
- VSCode搭建Spring Boot项目
下载安装Java Extension Pack和Spring Boot Extension Pack两个插件 打开VSCode的设置,搜索框输入maven,然后打开settings.xml文件编辑配置 ...
- Gradle构造Spring boot项目(使用私服地址)
安装配置环境变量这里就不说了,跟maven没什么大的差别,唯一不同的就是仓库地址的设置吧. 相关设置如下: group 'com.clark.daxian' version '1.0-SNAPSHOT ...
- 从零开始使用idea搭建spring boot 项目
第四章 spring boot 的日志管理 一. 使用spring boot 默认日志管理 1.输出日志到控制台 2. 通过application.properties 配置日志: logging.f ...
- idea 搭建spring boot 项目
1 2 然后点击下面的next 之后点击web选择web 点击sql 选择mysql mybatis 点击next 如果你使用的数据库是mysql 那么需要配置下在 里面填写 ######3##### ...
最新文章
- Linux环境编程--文件基本操作
- Dreamweaver——如何使网页中的第一个DIV水平居中
- Java实现二维码QRCode的编码和解码
- linux c/c++ 代码使用 doxygen 自动生成文档
- JavaScript 学习随记——==和===及常见元素的真假值
- eclipse工具栏sdk和avd图标
- 数据结构(十六)多源最短路径
- C# Serializable学习
- 68 道Redis面试题
- 【⭐阅读推荐指数:5颗星★★★★★⭐】关于多副本纠删码,你想知道的全都在这里
- HTML菜单中有关selected=true和setAttribute(“selected“,“selected“)的异同以及selected设置无法生效的问题解析
- 射手影音播放器android,射手影音播放器安卓版
- Winedit7.0自定义一键编译
- mac系统安装Anaconda后再打开终端自动进入Anaconda环境
- 10没有基于策略的qos_电力通信 | 基于智能光网络系统的配用电业务承载方案研究...
- CocosCreator-精灵动态加载图片资源,实例化精灵
- C++ doesn't name a type
- 水电一体化预付费云平台
- 构建磷脂双分子层(一)
- 浅谈exp与expdp的区别