使用 Gradle 构建 SpringBoot 2.x 应用程序

前言

环境: JDK 1.8 , Gradle 6.1.1 , IDEA 2019

创建并初始化 Gradle 项目

  • build.gradle

    plugins {
        id 'java'
        id 'org.springframework.boot' version '2.0.5.RELEASE'
        id 'io.spring.dependency-management' version '1.0.7.RELEASE'
    }
    
    group 'com.szxy.gradle'
    version '1.0-SNAPSHOT'
    
    sourceCompatibility = 1.8
    
    repositories {
        mavenLocal()
        maven {
            url 'http://maven.aliyun.com/nexus/content/groups/public/'
        }
        maven {
            url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
        }
    }
    
    dependencies {
        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' } }
                    }
                }
            }
        }
    }
    
    bootJar {
        mainClassName = 'com.szxy.gradle.springboot.App'
    }
    
    
  • application.yml

    在 src/main/resources 目录下新建文件,命名为 applicaton.yml。

    在 yml 配置文件中指定 Tomcat 服务器的端口号和 SpringBoot 应用程序的名称

    server:
      port: 80
    
    spring:
      application:
        name: gradle_spring_boot
    
  • App.java

    @SpringBootApplication
    public class App {
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    }
    
  • HelloController.java

    @RestController
    public class HelloController {
    
        @RequestMapping("/hello")
        public Map hello() {
            Map<String, String> map = new HashMap<>();
            map.put("msg", "Hello");
            return map;
        }
    }
    
    
  • AppTest.java

    对控制层 HelloController.java 中 hello 方法进行测试,这里采用 Spring-Boot-Test 测试。

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = App.class)
    @AutoConfigureMockMvc
    public class AppTest {
    
        @Autowired
        private MockMvc mvc;
    
        @Test
        public void helloTest() throws Exception {
            mvc.perform(get("/hello"))
                    .andExpect(status().isOk())
                    .andExpect(content().json("{msg:Hello}"));
            System.out.println("ok......");
        }
    
    }
    

打包

在 build.gradle 中添加 booJar ,在 bootJar 中指定主类名

bootJar {
    mainClassName = 'com.szxy.gradle.springboot.App'
}

测试

点击 IDEA 右边栏 Gradle,选择 Tasks->build->bootJar

在项目的根目录下 build/libs 有生成的 jar 包

打开 terminal 终端,运行 jar 文件

java -jar build/libs/gradle-spring-boot-1.0-SNAPSHOT.jar

打开浏览器,地址栏输入 http://localhost/hello 回车访问

参考

使用 Gradle 构建SpringBoot 2.x应用程序

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值