[JAVA] Take a quick look at Gradle and read the build.gradle generated by Spring Initializr

Implementing a simple Web REST API server with Spring Boot + MySQL --Qiita

Outline Read the build.gradle automatically generated by Spring Initializr. Take a look at Gradle to read.

References Gradle Build Tool Gradle User Guide

What is Gradle

Chapter 1 Introduction

Gradle is a build system in Java (JVM) environment

Use instead of maven. It imports dependent libraries, compiles, jars, runs tests, and much more.

Install If you are using IntelliJ, it is installed with it. You can also install it with Homebrew. It was here in my environment.

% ~/.gradle/wrapper/dists/gradle-4.5.1-bin/a5vbgfvpwtoqz8v2cdivxz28k/gradle-4.5.1/bin/gradle -v  

------------------------------------------------------------
Gradle 4.5.1
------------------------------------------------------------

Build time:   2018-02-05 13:22:49 UTC
Revision:     37007e1c012001ff09973e0bd095139239ecd3b3

Groovy:       2.4.12
Ant:          Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM:          1.8.0_162 (Oracle Corporation 25.162-b12)
OS:           Mac OS X 10.13.3 x86_64

build.gradle Basically edit this file. It's like pom.xml. Here is the one automatically generated by creating the project.

build.gradle


buildscript {
    ext {
        springBootVersion = '2.0.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

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

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('mysql:mysql-connector-java')
    compileOnly('org.projectlombok:lombok')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

Let's see what is written in order from the top.

buildscript Resolve Dependencies A block for resolving gradle.build's own dependencies. It seems that the Gradle project generated by Spring Boot uses the Gradle plugin of Spring Boot.

ext When you declare a variable.

repositories Specify the repository of the dependent library. It specifies Maven's Central repository. The method of specifying by URL is like this.

repositories {
    maven {
        url 'https://repo.spring.io/libs-snapshot'
    }
}

[Chapter 51 Dependency Management](http://gradle.monochromeroad.com/docs/userguide/dependency_management.html#sec: repositories)

dependencies When you specify the library. You're using the springboot gradle plugin.

apply Declare a plugin. These two plugins depend on spring Boot.

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

org.springframework.boot This was added.

Application tasks
-----------------
bootRun - Runs this project as a Spring Boot application.

A task that even executes an application. Now you can launch the Spring Boot application.

io.spring.dependency-management This guy looks great. A plugin that reproduces Maven's BOM (bill of materials) mechanism. ※BOM (bill of materials) How each library defines its own dependencies. In short, if you have a library you want to use, you can use it by describing that library and the BOM of that library.

Unfortunately, Gradle doesn't have this mechanism. You can do it with this plugin.

However, BOM is not used in this build.gradle.

When actually using it, use it in the following blocks. spring-cloud-stream example

dependencyManagement {
     imports {
          mavenBom 'org.springframework.cloud:spring-cloud-stream-dependencies:Fishtown.BUILD-SNAPSHOT'
     }
}

Deliverables

The version will be operated here Below Icchan is the version of jdk

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories With the buildscript guy

dependencies Describe the dependent library. The one written at the beginning is called configuration. Chapter 23 Java Plugins

name Expansion source Tasks to use meaning
compile - compileJava Compile-time dependencies
runtime compile - Runtime dependencies
testCompile compile compileTestJava Additional compile-time dependencies for tests
testRuntime runtime, testCompile test Additional dependencies when running the test
archives - uploadArchives Artifacts generated by this project(jar file etc.)
default runtime - The default configuration used by projects that depend on this project. It contains the artifacts and the dependencies that this project requires at run time.

There are many other ways to write it as follows.

dependencies {
    runtime group: 'org.springframework', name: 'spring-core', version: '2.5'
    runtime 'org.springframework:spring-core:2.5',
            'org.springframework:spring-aop:2.5'
    runtime(
        [group: 'org.springframework', name: 'spring-core', version: '2.5'],
        [group: 'org.springframework', name: 'spring-aop', version: '2.5']
    )
    runtime('org.hibernate:hibernate:3.0.5') {
        transitive = true
    }
    runtime group: 'org.hibernate', name: 'hibernate', version: '3.0.5', transitive: true
    runtime(group: 'org.hibernate', name: 'hibernate', version: '3.0.5') {
        transitive = true
    }
}

bonus

Hello World This is a script.

% cat build.gradle
task hello {
    doLast {
        println 'Hello world!'
    }
}

% gradle hello 

> Task :hello
Hello world!


BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

Recommended Posts

Take a quick look at Gradle and read the build.gradle generated by Spring Initializr
A quick look at the Monty Hall problem
Let's take a look at the Hotspot JVM startup procedure
Let's take a look at the screen of Quant Analyzer!
A quick look back at Java over the last five years
Examine the contents of the WAR file generated by the project created by Spring Initializr
A look at Jenkins, OpenJDK 8 and Java 11
Let's take a look at the functions of Keycloak's management console (administrator edition)
Read the file line by line VS read at once
Let's take a look at the functions of Keycloak's management console (user edition), user account service