Spring-boot 2.3.x源码基于Gradle编译过程详解

  

下面我会详细讲解“Spring-boot 2.3.x源码基于Gradle编译过程详解”的攻略。

标题

Spring-boot 2.3.x源码基于Gradle编译过程详解

代码块

在markdown中,我们可以使用代码块来展示代码,格式如下:

Your code goes here

或者指定代码块的语言,格式如下:

Your code goes here

正文

为了更好的理解Spring-boot 2.3.x源码基于Gradle编译过程,我们可以按照以下步骤进行操作。

1. 获取源码

到Spring-boot官网上 https://github.com/spring-projects/spring-boot 下载源码。

然后在本地使用git克隆源码仓库:

git clone https://github.com/spring-projects/spring-boot.git

2. 创建一个基于Gradle的项目

gradle init --type java-application

3. 编写build.gradle文件

在项目的根目录下,创建build.gradle文件,追加一下内容:

// set up repositories
repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/milestone" }
    maven { url "https://repo.spring.io/snapshot" }
}

//dependencies
dependencies {
    compile 'org.springframework.boot:spring-boot-starter:2.3.8.RELEASE'
}

// set up a task to create a JAR
task jar(type: Jar) {
    baseName = 'spring-boot-2.3.x-example'
    version = '0.1.0'
    from sourceSets.main.output
}

4. 构建项目

执行以下命令,构建项目:

gradle build

5. 运行项目

执行以下命令,运行项目:

java -jar build/libs/spring-boot-2.3.x-example-0.1.0.jar

示例说明

示例1:增加一个web服务

如果我们需要为刚刚创建的项目增加一个web服务,我们可以在build.gradle文件中增加以下依赖:

compile 'org.springframework.boot:spring-boot-starter-web:2.3.8.RELEASE'

然后创建一个RestController类:

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}

最后重新编译、打包、运行项目即可。然后在浏览器中输入 http://localhost:8080/hello 就可以看到“Hello, World!”的字样了。

示例2:增加一条编译输出信息

在build.gradle文件中增加以下代码:

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

然后重新编译、打包、运行项目即可。在编译输出信息中,会发现源码的编译编码已经设置成了UTF-8。

结论

通过以上攻略,我们可以了解到Spring-boot 2.3.x源码基于Gradle的编译过程,并且通过示例可以更好的理解这个过程。

相关文章