How to use Gradle

Reference page Building a RESTful Web Service

  1. File preparation

Prepare a configuration file and three java files.

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

The following two files are different from those of Building a RESTful Web Service.

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 = "Hello, %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));
  }
}
  1. 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

A file similar to the following will be created

$ 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
  1. Program execution
$ java -jar build/libs/hello-service-0.1.0.jar
  1. Go to http: // localhost: 8080 / greeting

gradle01.png

Recommended Posts

How to use Gradle
How to use Map
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use TreeSet
[How to use label]
How to use hashes
How to use JUnit 5
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
How to use Big Decimal
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
How to use Lombok now
[Creating] How to use JUnit
[Rails] How to use Scope
How to use the link_to method
How to use arrays (personal memorandum)
How to use scope (JSP & Servlet)
How to use the include? method
[Rails] How to use devise (Note)
How to use the form_with method
How to use EventBus3 and ThreadMode
How to use Spring Data JDBC
How to run Ant in Gradle
How to use binding.pry [53 days left]
How to use Java HttpClient (Post)
[Java] How to use join method
How to use Ruby on Rails
How to use equality and equality (how to use equals)