Gradle的SpringBoot工程

本文详细介绍如何使用Gradle创建SpringBoot项目,包括配置依赖、设置服务器监听端口及实现简单API示例。


为了测试向Beanstalk构建Java工程环境,通过Eclipse做了一个简单的API样例。
GitHub: 样本资源.

前提

Eclipse· 4.7 Oxygen
Spring Tools 3 Add-On (aka Spring Tool Suite 3)插件
STS(Gradle IDE pack)插件

创建工程

新建工程

在这里插入图片描述
名称;springBootSample
型;Gradle(Buildship 2.X)
包;jp.sample.api
在这里插入图片描述
版本;2.1.2
点击完了后,自动生成下列的文件结构
在这里插入图片描述

文件(build.gradle)内添加依赖关系

implementation(‘org.projectlombok:lombok:1.16.6’)
implementation(‘org.springframework.boot:spring-boot-starter-web’)

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation('org.projectlombok:lombok:1.16.6')
    implementation('org.springframework.boot:spring-boot-starter-web')
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

文件(application.properties)内添加服务器监听端口

#server.port for test
server.port=1234

添加Controller

路径 jp.sample.api 下添加 controller 的文件夹,在文件夹内创建 sampleController 的类文件。
sampleController.java

package jp.sample.api.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class sampleController {
    // http://localhost:1234/getFirstMethod
    @GetMapping("/getFirstMethod")
    public String getTest() {
        return "Hello!, This is a sample for SpringBoot API. ";
    }
}

实行确认

右键点击实行→SpringBoot应用

Started SpringBootSampleApplication in 1.219 seconds (JVM running for 1.816)

地址栏输入;

http://localhost:1234/getFirstMethod

画面显示
在这里插入图片描述

### 使用 Gradle 构建 Spring Boot 工程的方法 为了使用 Gradle 构建 Spring Boot 工程,需要完成以下几个方面的配置: #### 1. 应用 Spring Boot 插件 Spring 提供了一个专门用于支持 Spring Boot 的 Gradle 插件。此插件会自动添加一些必要的任务和配置来简化基于 Spring Boot 的项目开发工作[^1]。 以下是 `build.gradle` 文件中的插件部分示例: ```groovy plugins { id 'java' id 'org.springframework.boot' version '2.0.5.RELEASE' id 'io.spring.dependency-management' version '1.0.7.RELEASE' } ``` 上述代码展示了如何引入 Spring Boot 和依赖管理插件,并指定了它们的具体版本号。 --- #### 2. 配置仓库地址 在多模块工程或者特定场景下,可能需要自定义 Maven 或其他类型的仓库地址。这些可以通过 `buildscript` 块来进行声明[^3]。 以下是一个典型的仓库配置例子: ```groovy buildscript { ext { springBootVersion = '2.2.1.RELEASE' } repositories { mavenCentral() maven { url 'https://maven.aliyun.com/repository/public' } maven { url 'https://repo.spring.io/milestone' } } dependencies { classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion" } } ``` 这段代码设置了多个远程仓库以及对应的 Spring Boot 版本信息。 --- #### 3. 添加项目依赖 除了基本的插件外,还需要根据实际需求添加相应的库依赖。例如,在一个典型的应用程序中可能会涉及 Web 支持、数据访问等功能[^2]。 下面是一些常见的依赖项示例: ```groovy dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' runtimeOnly 'com.h2database:h2' testImplementation 'org.springframework.boot:spring-boot-starter-test' } ``` 此处列出了几个常用的 Starter 组件及其功能描述。 --- #### 4. 自定义应用属性 如果希望进一步调整服务器行为或其他运行参数,则可通过 `application.properties` 文件实现个性化设置[^5]。 例如: ```properties server.port=8081 server.servlet.context-path=/myapp spring.datasource.url=jdbc:h2:mem:testdb spring.jpa.hibernate.ddl-auto=create-drop ``` 以上内容说明了端口绑定、上下文路径设定以及其他数据库连接细节等内容。 --- #### 5. 打包与部署 当所有准备工作完成后,就可以利用 Gradle 来生成最终产物——即一个可直接执行的 JAR 文件。 命令如下: ```bash ./gradlew bootJar ``` 这一步骤将会把整个项目编译并打包成标准格式存放在 `build/libs/` 路径下。 --- ### 总结 综上所述,借助于强大的 Gradle 工具链配合灵活易扩展的设计理念,开发者可以快速搭建起满足业务诉求的现代化微服务架构体系[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值