3. 创建 Gradle 项目

3. 创建 Gradle 项目

3.1. 创建标准 Java 项目

Eclipse Gradle 工具提供了创建基于 Java 的 Gradle 项目的向导。您可以通过File ▸ New ▸ Other… 菜单条目来达到它。

New

单击 Next > 按钮。
New Gradle Project

按 “Finish” 按钮创建项目。这将触发 gradle init-类型 java 库命令并导入项目。Next > 按钮在创建项目之前获取配置的预览。
New Gradle Project2

创建的项目看起来类似于下面的截图。
Preview

3.2. 创建 web 应用程序

下面演示如何使用 Gradle 创建弹簧引导 webapplication。

首先, 创建一个名为 com.vogella.springboot.gradle.minimal 的标准 Gradle 项目。

将生成. gradle 文件更改为以下内容:

buildscript {
    ext {
        springBootVersion = '1.5.3.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'com.vogella.springboot'
    version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    compile('org.springframework.boot:spring-boot-starter-cache')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-hateoas')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-data-rest')

    compile('com.h2database:h2')

    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
}

确保更新项目类路径, 请参阅通过 Gradle 更新 Java 类路径以了解详细信息。

创建以下类。

package com.vogella.springboot.gradle.minimal;

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);
    }

}

选择此类, 然后通过上下文菜单Run-As ▸ Java Application。

扫描二维码关注公众号,回复: 2155758 查看本文章

这将在 http://localhost:8080 上启动字符串引导和选定的应用程序。

要了解有关 spring 引导的更多信息, 请参阅Spring教程

猜你喜欢

转载自blog.csdn.net/scorpio_3715/article/details/80997379