Get started with Gradle

Gradle overview

Purpose

Environmental setup

Project initialization

Example interactive interface
```shell:Interactive interface
$ gradlew init

Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 2

Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Swift
Enter selection (default: Java) [1..5] 3

Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2] 2

Select test framework:
1: JUnit 4
2: TestNG
3: Spock
4: JUnit Jupiter
Enter selection (default: JUnit 4) [1..4] 4

Project name (default: test): testapp
Source package (default: testapp): 
> Task :init
Get more help with your project: https://docs.gradle.org/6.1.1/userguide/tutorial_java_projects.html

BUILD SUCCESSFUL in 4m 44s
2 actionable tasks: 2 executed
```
Project initial structure example
```shell:Initial project structure
.
├── build.gradle.kts
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle.kts
└── src
    ├── main
    │   ├── java
    │   │   └── testapp
    │   │       └── App.java
    │   └── resources
    └── test
        ├── java
        │   └── testapp
        │       └── AppTest.java
        └── resources

11 directories, 8 files
```

Dependency management

Formatter

Subject: Static code analysis task

Main subject: Unit test

Test coverage

Sample: Full

build.gradle


plugins {
  id 'java'
  id 'com.github.sherter.google-java-format' version '0.8'
  id 'checkstyle'
  id "com.github.spotbugs" version "3.0.0"
  id 'jacoco'
}

File tollSettingsDirectory = new File("${rootProject.projectDir}/config/")

group 'org.example'
version '1.0-SNAPSHOT'

//noinspection GroovyUnusedAssignment
sourceCompatibility = 1.11

repositories {
  mavenCentral()
}

dependencies {
  implementation "org.slf4j:slf4j-api:1.7.30"
  testImplementation "org.slf4j:slf4j-api:1.7.30"
  runtimeOnly "ch.qos.logback:logback-classic:1.2.3"
  testRuntimeOnly "ch.qos.logback:logback-classic:1.2.3"

  testImplementation "org.junit.jupiter:junit-jupiter-api:5.6.0"
  testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.6.0"
}

test {
  useJUnitPlatform()
}

checkstyle {
  toolVersion = '8.29'
  configFile = rootProject.file(new File(tollSettingsDirectory.toString(), "checkstyle/checkstyle.xml").toString())
}

spotbugs {
  toolVersion = "3.1.12"
  excludeFilter = rootProject.file(new File(tollSettingsDirectory.toString(), "spotbugs/exclude_filter.xml").toString())
}

jacoco {
  toolVersion = "0.8.5"
  reportsDir = file("${buildDir}/JacocoReports")
}
jacocoTestReport {
  reports {
    html.destination file("${buildDir}/JacocoReports/html")
  }
}
jacocoTestReport.shouldRunAfter(test)

About Jar file build

2020/02/28 postscript:

Since there was a story that it is difficult to use in the release because the dependency is not included when building the Jar file, I will add it. It is troublesome to manually place the library even though it is managed by maven2. So, refer to the reference information and install Gradle Shadow Plugin. (In the case of WAR, the official WAR plugin looks good. I don't know EAR) In this case, isn't the license (redistribution) caught? That Let's check it properly.

build.gradle


plugins {
    id 'com.github.johnrengelman.shadow' version '5.2.0'
}

Add a class that implements the entry point main () to Manifest.

Example of a class with an entry point


public class Main {
    public static void main(String[] args){
        //I do something.
        new HelloWorld().execute();
    }
}

build.gradle


jar {
  manifest {
    attributes "Main-Class" : "Main" //Full package name.name of the class
  }
}

Build with the shadowJar task. It seems to inherit the settings of the Jar task by default.

$ java -jar ./build/libs/sample_tdd-1.0-SNAPSHOT-all.jar 
Hello, World!

reference:

reference

Recommended Posts

Get started with Gradle
Get started with Spring boot
Get started with DynamoDB with docker
Let's get started with parallel programming
How to get started with slim
I tried to get started with WebAssembly
Let's get started with Java-Create a development environment ②
Use ProGuard with Gradle
Integration Test with Gradle
Getting Started with DBUnit
Install Gradle with ubuntu16.04
Getting Started with Ruby
How to get started with Eclipse Micro Profile
Maybe it works! Let's get started with Docker!
Getting Started with Swift
Rails beginners tried to get started with RSpec
Use jlink with gradle
I tried Getting Started with Gradle on Heroku
I tried to get started with Spring Data JPA
Get started with "Introduction to Practical Rust Programming" (Day 3)
How to get started with creating a Rails app
Get started with serverless Java with the lightweight framework Micronaut!
Java multi-project creation with Gradle
Getting Started with Doma-Annotation Processing
Getting Started with Java Collection
Gradle + Kotlin-Generate jar with DSL
Remote debugging with Gradle test
Getting Started with JSP & Servlet
Getting Started with Java Basics
Getting Started with Spring Boot
Use log4j2 with YAML + Gradle
Hello World with SpringBoot / Gradle
Getting Started with Ruby Modules
How to get started with Gatsby (TypeScript) x Netlify x Docker
Now is the time to get started with the Stream API
How to get started with JDBC using PostgresSQL on MacOS
I tried to get started with Swagger using Spring Boot
Build a Java project with Gradle
Easy code review to get started with Jenkins / SonarQube: Static analysis
Gradle
Getting Started with Java_Chapter 5_Practice Exercises 5_4
I tried using JOOQ with Gradle
Output test coverage with clover + gradle
Get validation results with Spring Boot
[Google Cloud] Getting Started with Docker
Get related table columns with JPA
How to get along with Rails
Getting started with Java lambda expressions
Getting Started with Docker with VS Code
Memo to get with Struts2 + Ajax
Develop Processing with IntelliJ + Kotlin + Gradle
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
Get started with "Introduction to Practical Rust Programming" (Day 4) Call Rust from Ruby
(Note) Get a set of dependent library jars with the help of Gradle
Get a backup with mysqldump (Docker's MySQL)
[Java] Get List / Map elements with Iterator
[Java] Create an executable module with Gradle
Getting Started with Ruby for Java Engineers
[I get] Until deploying with SAM CLI
Get in touch with Eclipse MicroProfile Health