使用idea构建一个springboot+gradle项目

时间:2024-03-30 20:36:12

1、根据idea向导创建gradle项目:

使用idea构建一个springboot+gradle项目

使用idea构建一个springboot+gradle项目

2、配置gradle

使用idea构建一个springboot+gradle项目

3、配置build.gradle文件

buildscript {
    ext {
        springBootVersion = '2.2.4.RELEASE'
        springDependencyVersion = '1.0.9.RELEASE'
    }
    repositories {
        maven {
            url 'https://repo.spring.io/release'
        }
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath(
                "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}",
                "io.spring.gradle:dependency-management-plugin:${springDependencyVersion}"
        )
    }
}

apply {
    plugin("java")
    plugin("maven")
    plugin("idea")
    plugin("org.springframework.boot")
    plugin("io.spring.dependency-management")
}

group 'com.test.springboot'
version '1.0'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    maven {
        url 'https://repo.spring.io/release'
    }
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile(
            "org.springframework.boot:spring-boot-starter-web"
    )
//    implementation 'org.springframework.boot:spring-boot-starter'
//    testImplementation('org.springframework.boot:spring-boot-starter-test') {
//        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
//    }

}

4、编写代码

主程序:

package com.test.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Author LiangXiong
 * @Date 2/8/2020 1:14 PM
 * @Version 1.0
 */
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
package com.test.boot.controller;

import com.test.boot.domain.Person;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


/**
 * @Author LiangXiong
 * @Date 2/8/2020 1:21 PM
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class MyController {
    @RequestMapping(value = "/person", method = RequestMethod.GET)
    public Person getPerson() {
        Person person = new Person();
        person.setId(25);
        person.setName("zhangsan");
        person.setBirthday(new Date());
        return person;
    }
}
import java.util.Date;

/**
 * @Author LiangXiong
 * @Date 2/8/2020 1:21 PM
 * @Version 1.0
 */
public class Person {
    private int id;
    private String name;
    private Date birthday;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

5、进入项目根目录(包含build.gradle的目录)输入命令:gradle bootRun【或者直接在主程序Ctrl +Shift +F10】

使用idea构建一个springboot+gradle项目

 

6、浏览器访问验证:

使用idea构建一个springboot+gradle项目

 

7、后续打包:

1.打包,在工程根目录输入如下命令:
    gradle bootJar
生成文件路径:
    工程根目录/build/libs,我这里生成的是“java -jar spring-boot-helloworld-1.0.jar”
2.运行,在jar目录下输入如下命令:
    java -jar spring-boot-helloworld-1.0.jar