[JAVA] I want to write a nice build.gradle

What I got stuck or couldn't find when I looked up build.gradle


--Gradle6.4 (released 5/6/2020)

At the beginning

For the time being, Gradle documentation may be a good idea to take a look.

Convenient plugin

Name link to documentation

-java Plugin. Java source cannot be compiled without it -checkstyle Plugin. Useful when aligning styles ([To the description section](introduce #checkstyle)) -shadow Plugin. Required to generate Fat-Jar (Uber-Jar) plugin ([Go to description section](Generate fat-jaruber-jar using #shadow plugin)) -ErrorProne Plugin. Static analysis tool. The one who looks for anti-patterns ([To the description section](introduces #errorprone))

Use Wrapper

Something like ./gradlew or ./gradlew.bat. Can be initialized and updated by running the wrapper task With this, it's okay if the version of gradle installed in the developer's environment is different!

Official documentation: The Gradle Wrapper

Specify the encoding

If not specified, the build result may change between Linux and Windows.

For example, when specifying UTF-8, it looks like this


//compile Java task encoding
compileJava.options.encoding = 'UTF-8'
//compileTest Java task encoding
compileTestJava.options.encoding = 'UTF-8'
//javadoc task encoding
javadoc.options.encoding = 'UTF-8'

Dependency declaration

** Do not use compile or testCompile because they are deprecated ** There are various things such as ʻimplementation and testImplementation, but since it is troublesome, the official document [The Java Plugin](https://docs.gradle.org/6.4/userguide/java_plugin.html#sec:java_plugin_and_dependency_management) What is ʻapi? It is written in The Java Library Plugin

A brief explanation compileOnly is a dependency required at compile time. Not included in FatJar etc. (provided scope in Maven) runtimeOnly is a dependency required at runtime. It can be entered in FatJar etc., but it cannot be referenced from within the source code (I think it is, but I do not know because I have never used it) ʻImplementationis a required dependency at compile time and run time. It can be referenced from within the source code as well as in FatJar, but it cannot be referenced from projects that have this as a dependency (recognized asprovided in Maven). Use Java Library Plugin ʻapi to make it visible

Also, those with test at the beginning (for example, testImplementation) are valid only during testing.

Specify the version range

The official documentation is here: Declaring Versions and Ranges


testImplementation 'org.junit.jupiter:junit-jupiter:5.6.+'

If you do, 5.6. Below will use the latest version at that time. The build result may change depending on the build environment due to this effect, so if you want to prevent it, [Fix Dependencies](#Fix Dependencies))

Fix dependencies

The official documentation is here: Locking dependency versions

Introduce ErrorProne

Add to plugins


    id 'net.ltgt.errorprone' version '1.1.1'

Add to dependency


dependencies {
    // ErrorProne
    errorprone 'com.google.errorprone:error_prone_core:2.+'

For Java8, insert the Java9 compiler


dependencies {
    errorproneJavac 'com.google.errorprone:javac:9+181-r4173-1'

If you want to be able to run in Java 8 environment or Java 9 or higher, this may be good


if (!JavaVersion.current().isJava9Compatible()) {
    dependencies {
        errorproneJavac 'com.google.errorprone:javac:9+181-r4173-1'

It seems that javac will be replaced, so it applies to all compile tasks

Introduce CheckStyle

Add to plugins


plugins {
    id 'checkstyle'

Tweak config / checkstyle / checkstyle.xml. For example


<?xml version="1.0"?>
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
<module name="Checker">
    <module name="FileTabCharacter"/>
    <module name="TreeWalker">
        <module name="Indentation">
            <property name="lineWrappingIndentation" value="8"/>
        <!-- Imports -->
        <module name="UnusedImports"/>
        <module name="RedundantImport"/>
        <module name="ImportOrder">
            <property name="separated" value="true"/>
            <property name="sortStaticImportsAlphabetically" value="true"/>
        <!-- Brackets -->
        <module name="LeftCurly"/>
        <module name="RightCurly"/>
        <module name="NeedBraces"/>

Official documentation looks good

If you execute the build task, it will be executed without permission

Unit test with JUnit

Add to dependency


testImplementation 'org.junit.jupiter:junit-jupiter:5.6.+'

It will be nice to set it like this


tasks.test {
    testLogging.showStandardStreams = true // System.Display out or err in the log
    testLogging {
        events('passed', 'skipped', 'failed')

JUnit 5 User Guide

Generate Fat Jar (Uber Jar) using shadow plugin

Add as a plugin


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

Execute the shadowjar task and it will be completed under build / libs

You can relocate, minimize, and rename the jar, but it's a hassle, so the official docs https://imperceptiblethoughts.com/shadow/introduction/

Generate a searchable Javadoc


javadoc {
    if(JavaVersion.current().isJava9Compatible()) {
        options.addBooleanOption('html5', true)

This makes the javadoc task look good (requires Java 9 or above)

Recommended Posts

I want to write a nice build.gradle
I want to write a unit test!
I want to simply write a repeating string
I want to develop a web application!
[Ruby] I want to do a method jump!
I want to design a structured exception handling
I want to write quickly from java to sqlite
I want to convert characters ...
I want to use a little icon in Rails
I want to monitor a specific file with WatchService
I want to define a function in Rails Console
To write a user-oriented program (1)
I want to add a reference type column later
I want to click a GoogleMap pin in RSpec
I want to connect to Heroku MySQL from a client
I want to create a generic annotation for a type
I want to add a delete function to the comment function
[Java] I want to convert a byte array to a hexadecimal number
I want to find a relative path in a situation using Path
I want to implement a product information editing function ~ part1 ~
I want to make a specific model of ActiveRecord ReadOnly
I want to make a list with kotlin and java!
I want to call a method and count the number
I want to make a function with kotlin and java!
I want to create a form to select the [Rails] category
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to give a class name to the select attribute
I want to create a Parquet file even in Ruby
I want to use FireBase to display a timeline like Twitter
Swift: I want to chain arrays
I want to use FormObject well
I want to convert InputStream to String
How to write a ternary operator
I want to docker-compose up Next.js!
I want to write a loop that references an index with Java 8's Stream API
I want to recursively search for files under a specific directory
I want to create a chat screen for the Swift chat app!
I want to make a button with a line break with link_to [Note]
I want to be able to think and write regular expressions myself. ..
I want to add a browsing function with ruby on rails
I want to use swipeback on a screen that uses XLPagerTabStrip
I just want to write Java using Eclipse on my Mac
I tried to write code like a type declaration in Ruby
[Ruby] I want to put an array in a variable. I want to convert to an array
I got stuck trying to write a where in clause in ActiveRecord
I want to extract between character strings with a regular expression
A flowing interface? -I want to give you an opportunity to write good code. 3 [C # refactoring sample]
I want to eliminate duplicate error messages
I want to make an ios.android app
[Basic] How to write a Dockerfile Self-learning ②
I want to display background-ground-image on heroku.
I want to use DBViewer with Eclipse 2018-12! !!
I want to RSpec even at Jest!
[Introduction to Java] How to write a Java program
I want to install PHP 7.2 on Ubuntu 20.04.
I want to stop Java updates altogether
I want to use @Autowired in Servlet
I want to target static fields to @Autowired
I want to do team development remotely
[SpringBoot] How to write a controller test
[Rails] I want to send data of different models in a form