Spring boot Gradle项目搭建

2019-08-16 11:41:32来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Spring boot Gradle项目搭建

Spring boot Gradle项目搭建


使用IDEA创建Gradle工程

????操作大致为:File->new->Project->Gradle(在左侧选项栏中)

????创建常规以后生成的工程目录如下:

  • build
  • gradle
    • wrapper
      • gradle-wrapper.jar
      • gradle-wrapper.properties
  • 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

参考链接

  • 1.Building Spring Boot 2 Applications with Gradle
  • 2.Learn Spring Boot
  • 3.Building a RESTful Web Service
  • Gradle配置阿里云Maven镜像仓库地址

原文链接:https://www.cnblogs.com/freedom-only/p/11285351.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Dubbo从入门到实战:入门篇

下一篇:静态方法