What I got stuck or couldn't find when I looked up build.gradle
--Gradle6.4 (released 5/6/2020)
For the time being, Gradle documentation may be a good idea to take a look.
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))
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
If not specified, the build result may change between Linux and Windows.
For example, when specifying UTF-8, it looks like this
build.gradle
//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'
** 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 as
provided 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.
The official documentation is here: Declaring Versions and Ranges
build.gradle
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))
The official documentation is here: Locking dependency versions
Add to plugins
build.gradle
id 'net.ltgt.errorprone' version '1.1.1'
Add to dependency
build.gradle
dependencies {
// ErrorProne
errorprone 'com.google.errorprone:error_prone_core:2.+'
}
For Java8, insert the Java9 compiler
build.gradle
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
build.gradle
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
Add to plugins
build.gradle
plugins {
id 'checkstyle'
}
Tweak config / checkstyle / checkstyle.xml. For example
checkstyle.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="FileTabCharacter"/>
<module name="TreeWalker">
<module name="Indentation">
<property name="lineWrappingIndentation" value="8"/>
</module>
<!-- Imports -->
<module name="UnusedImports"/>
<module name="RedundantImport"/>
<module name="ImportOrder">
<property name="separated" value="true"/>
<property name="sortStaticImportsAlphabetically" value="true"/>
</module>
<!-- Brackets -->
<module name="LeftCurly"/>
<module name="RightCurly"/>
<module name="NeedBraces"/>
</module>
</module>
Official documentation looks good
If you execute the build task, it will be executed without permission
Add to dependency
build.gradle
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.+'
It will be nice to set it like this
build.gradle
tasks.test {
testLogging.showStandardStreams = true // System.Display out or err in the log
useJUnitPlatform()
testLogging {
events('passed', 'skipped', 'failed')
}
}
Add as a plugin
build.gradle
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/
build.gradle
javadoc {
if(JavaVersion.current().isJava9Compatible()) {
options.addBooleanOption('html5', true)
}
}
This makes the javadoc task look good (requires Java 9 or above)
Recommended Posts