使用Eclipse4.7和Gradle来构建SpringBoot2.0.1的新项目
使用Eclipse4.7和Gradle创建一个新的SpringBoot2.0.1项目。
环境
作为开发环境的选项有:
– 64位Windows 10操作系统
– Eclipse集成开发环境
– Spring Boot开发框架
以下是搭建利用的开发环境的步骤。
使用中的工具
-
- Eclipse
Oxygen.2 Release (4.7.2)
Pleiades all in oneを使用
http://mergedoc.osdn.jp/
JDK8
Gradleを利用するためにJava7以上が必要。
Eclipse的安装
安装Spring工具(STS)。
通过Eclipse的市场功能安装STS。
截至2018/05/01,Spring工具(又称为Spring IDE和Spring Tool Suite)的版本为3.9.4.RELEASE。

在Windows環境中准备使用Gradle
下载gradle的模块
请从以下链接下载zip文件。
https://gradle.org/next-steps/?version=4.7&format=all
解压缩到任意目录
将下载的zip文件解压到任意目录中。
在gradle.bat中添加编码定义
将以下定义添加到gradle-4.7\bin\gradle.bat中。
将DEFAULT_JVM_OPTS设置为”-Dfile.encoding=UTF-8″。
设置环境变量
将gradle-4.7\bin的路径添加到环境变量的PATH中。
请确认安装是否成功。
为了确认,请执行gradle命令并检查Gradle的版本。
请启动命令提示符并执行以下命令。
gradle -v
# 以下のような結果が出力されればOK
Welcome to Gradle 4.7!
Here are the highlights of this release:
- Incremental annotation processing
- JDK 10 support
- Grouped non-interactive console logs
- Failed tests are re-run first for quicker feedback
For more details see https://docs.gradle.org/4.7/release-notes.html
------------------------------------------------------------
Gradle 4.7
------------------------------------------------------------
Build time: 2018-04-18 09:09:12 UTC
Revision: b9a962bf70638332300e7f810689cb2febbd4a6c
Groovy: 2.4.12
Ant: Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM: 1.8.0_112 (Oracle Corporation 25.112-b15)
OS: Windows 10 10.0 amd64
开始一个新项目
创建用于项目的目录
在这里,我们将C:\projects\gradle-spring-boot作为项目的根文件夹,
创建一个新的空文件夹。
项目的初始化
执行以下命令以初始化Gradle项目。
cd /D C:\projects\gradle-spring-boot
gradle init
# コマンドの結果
BUILD SUCCESSFUL in 4s
2 actionable tasks: 2 executed
使用上述命令生成的文件结构如下。
dir
# コマンドの結果
2018/05/03 21:26 <DIR> .
2018/05/03 21:26 <DIR> ..
2018/05/03 21:26 <DIR> .gradle
2018/05/03 21:26 207 build.gradle
2018/05/03 21:26 <DIR> gradle
2018/05/03 21:26 5,296 gradlew
2018/05/03 21:26 2,260 gradlew.bat
2018/05/03 21:26 375 settings.gradle
4 個のファイル 8,138 バイト
4 個のディレクトリ 29,642,649,600 バイトの空き領域
编辑build.gradle文件。
使用文本编辑器将build.gradle文件编辑为以下内容。
参考:https://spring.io/guides/gs/spring-boot/
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'gs-spring-boot'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("junit:junit")
}
为了验证操作,先创建一个样本源代码。
src/main/java/hello/HelloController.java的中文简述:
你好控制器(HelloController.java)位于src/main/java/hello/目录下。
package hello;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
src/main/java/hello/Application.java
来源于主要路径下的hello模块下的Application.java文件。
package hello;
import java.util.Arrays;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}
执行构建任务
当准备完成后,我们将执行一次Gradle的构建任务。
通过gradle init对Gradle项目进行初始化时,gradlew也会同时被设置好。
在接下来的步骤中,我们将使用gradlew来记录操作步骤。
gradlew build
# コマンドの結果
BUILD SUCCESSFUL in 16s
2 actionable tasks: 2 executed
在执行build任务后的目录结构
C:\projects\gradle-spring-boot>dir
ドライブ C のボリューム ラベルは Windows です
ボリューム シリアル番号は FE8D-C53C です
C:\projects\gradle-spring-boot のディレクトリ
2018/05/04 08:39 <DIR> .
2018/05/04 08:39 <DIR> ..
2018/05/03 21:29 <DIR> .gradle
2018/05/04 08:40 <DIR> build # ← buildディレクトリが追加で生成されている。
2018/05/03 21:28 840 build.gradle
2018/05/03 21:26 <DIR> gradle
2018/05/03 21:26 5,296 gradlew
2018/05/03 21:26 2,260 gradlew.bat
2018/05/03 21:26 375 settings.gradle
2018/05/04 08:39 <DIR> src
4 個のファイル 8,771 バイト
6 個のディレクトリ 29,562,568,704 バイトの空き領域
C:\projects\gradle-spring-boot\build>dir /S /B
C:\projects\gradle-spring-boot\build\classes
C:\projects\gradle-spring-boot\build\libs
C:\projects\gradle-spring-boot\build\tmp
C:\projects\gradle-spring-boot\build\classes\java
C:\projects\gradle-spring-boot\build\classes\java\main
C:\projects\gradle-spring-boot\build\classes\java\main\hello
C:\projects\gradle-spring-boot\build\classes\java\main\hello\Application.class
C:\projects\gradle-spring-boot\build\classes\java\main\hello\HelloController.class
C:\projects\gradle-spring-boot\build\libs\gs-spring-boot-0.1.0.jar # ← 生成された実行可能jar(FAT jar)
C:\projects\gradle-spring-boot\build\tmp\bootJar
C:\projects\gradle-spring-boot\build\tmp\compileJava
C:\projects\gradle-spring-boot\build\tmp\bootJar\MANIFEST.MF
确认运行生成的可执行jar的操作。
cd /D C:\projects\gradle-spring-boot
java -jar build\libs\gs-spring-boot-0.1.0.jar
执行命令后,如果访问以下URL并显示“来自Spring Boot的问候!”,则可以将样例源代码作为一个新项目完成构建。