Spring boot Gradle项目搭建
程序员文章站
2022-04-10 15:46:15
使用IDEA搭建Java Spring Boot Gradle工程 ......
spring boot gradle项目搭建
使用idea创建gradle工程
操作大致为:file->new->project->gradle(在左侧选项栏中)
创建常规以后生成的工程目录如下:
- build
- gradle
- wrapper
- gradle-wrapper.jar
- gradle-wrapper.properties
- wrapper
- src
- java
- resources
- test
- java
- resources
- build.gradle
- gradlew
- gradlew.bat
- settings.gradle
配置spring boot
下面需要对两个文件进行编辑:
build.gradle文件修改后的内容如下,依赖一般是前面是groupid,中间是artifactid,第三个一般是版本。在repositories配置使用阿里云的仓库,避免下载过慢。
plugins { id 'java' id 'com.gradle.build-scan' version '2.0.2' id 'org.springframework.boot' version '2.0.5.release' id 'io.spring.dependency-management' version '1.0.7.release' } group 'seckill' version '1.0-snapshot' sourcecompatibility = 1.8 repositories { maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } mavencentral() } dependencies { testcompile group: 'junit', name: 'junit', version: '4.12' implementation 'org.springframework.boot:spring-boot-dependencies:2.0.5.release' implementation 'org.springframework.boot:spring-boot-starter-web' testimplementation 'org.springframework.boot:spring-boot-starter-test' components { withmodule('org.springframework:spring-beans') { allvariants { withdependencyconstraints { // need to patch constraints because snakeyaml is an optional dependency it.findall { it.name == 'snakeyaml' }.each { it.version { strictly '1.19' } } } } } } } buildscan { // always accept the terms of service termsofserviceurl = 'https://gradle.com/terms-of-service' termsofserviceagree = 'yes' // always publish a build scan publishalways() }
setting.gradle文件修改后的内容如下:
rootproject.name = 'seckill' enablefeaturepreview('improved_pom_support')
编写类
首先在src/java下生成源码目录com.seckill.spring(相当于com/seckill/spring)
在src/java下创建程序入口类application.java,其内容如下:
package com.seckill.spring; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplication public class application { public static void main(string[] args) { springapplication.run(application.class, args); } }
在src/java/com.seckill.spring下创建目录controllers,并在controllers目录创建类:helloworld,在其中定义根路由并返回hello world,其代码如下:
package com.seckill.spring.controllers; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller; import java.util.hashmap; import java.util.map; @restcontroller public class helloworld { @requestmapping(value = "/", method = requestmethod.get) public map helloworld() { map<string, object> ret = new hashmap<>(); ret.put("ret", "hello world"); return ret; } }
运行访问
- 运行application类,可以在控制台看到起默认监听在8080端口
- 浏览器访问:localhost:8080,可以看到返回字符串hello world
参考链接
推荐阅读
-
Spring Boot项目实战之拦截器与过滤器
-
spring-cloud项目搭建
-
基于Flyway实现简化Spring Boot项目部署
-
spring boot admin 搭建详解
-
在 Spring Boot 项目中使用 activiti
-
spring boot 一个项目启动多个实例
-
Jenkins + Docker + dockerfile-maven-plugin + Harbor CI/CD spring-boot项目的最轻量级配置
-
创建简单spring boot项目
-
Spring Boot入门(一):搭建Spring Boot项目
-
15 个优秀开源的 Spring Boot 学习项目