Gradle の使い方

参考ページ
[Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/)

1) ファイルの用意

設定ファイル　と　３つの　java ファイルを用意します。

```text
$ tree 
.
├── build.gradle
└── src
    └── main
        └── java
            └── hello
                ├── Application.java
                ├── Greeting.java
                └── GreetingController.java
```

[Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/)  のものと、次の２つのファイルが違います。

```text:build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.1.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'hello-service'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile('org.springframework.boot:spring-boot-starter-test')
}
```

```java:src/main/java/hello/GreetingController.java

package hello;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;

@RestController
public class GreetingController {

  private static final String template = "こんにちは, %s!";
  private final AtomicLong counter = new AtomicLong();

@RequestMapping(value="/greeting",method=RequestMethod.GET)
  public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
    return new Greeting(counter.incrementAndGet(),
              String.format(template, name));
  }
}
```

2) build

```text
$ gradle build

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.0.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed
```

次のようなファイルが作成されます

```text
$ tree
.
├── build
│   ├── classes
│   │   └── java
│   │       └── main
│   │           └── hello
│   │               ├── Application.class
│   │               ├── Greeting.class
│   │               └── GreetingController.class
│   ├── generated
│   │   └── sources
│   │       └── annotationProcessor
│   │           └── java
│   │               └── main
│   ├── libs
│   │   └── hello-service-0.1.0.jar
│   └── tmp
│       ├── bootJar
│       │   └── MANIFEST.MF
│       └── compileJava
├── build.gradle
└── src
    └── main
        └── java
            └── hello
                ├── Application.java
                ├── Greeting.java
                └── GreetingController.java

18 directories, 9 files
```

3) プログラムの実行

```bash
$ java -jar build/libs/hello-service-0.1.0.jar
```

4)  http://localhost:8080/greeting にアクセス

![gradle01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/1dea3e9f-6520-5650-0480-cd2f28ba46cb.png)