LoginSignup
1
0

More than 3 years have passed since last update.

Gradle の使い方

Last updated at Posted at 2020-01-12

参考ページ
Building a RESTful Web Service

1) ファイルの用意

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

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

Building a RESTful Web Service のものと、次の2つのファイルが違います。

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

$ 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

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

$ 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) プログラムの実行

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

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

gradle01.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0