Write in Groovy-based or Kotolin-based DSL
By using Plug-in, you can prevent reinvention of the wheel or follow the general flow.
Of course, you need to make your own to customize it according to your own environment.
Manage build tasks
Manage dependencies
Become an Ant rapper
** No separate installation required due to Gradle wrapper **
When the command is executed as shown below, the required version will be downloaded and cached the next time the command is executed.
# Version 6.1.1 is 2020/02/Latest as of 07
./gradlew wrapper --gradle-version=6.1.1 --distribution-type=bin
gradle
to ./gradlew
as appropriate.Groovy and Kotlin can be selected for DSL.
For learning purposes, Groovy seems to be easier to refer to older documents.
If you are not good at English, there will be no problem as both are officially sampled.
Consult with team skills in actual operation.
For example, for application development using JUnit5: Build Init Plugin
gradle init --type java-application --test-framework junit-jupiter
If you init without selecting the plugin, should you add it to build.gradle?
Even if you simply gradle init
, you can select it with an interactive interface, so you can rest assured.
```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
```
```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
```
The Maven public repository is set by default as the repository.
When using the original Maven2, it seems to work with Pom.xml
, but it has not been confirmed. Tools are also introduced in the manual.
Define product and test dependencies, respectively.
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
}
Performed by the check task.
Use a formatter plugin that applies Google Java Format.
plugins {
id 'com.github.sherter.google-java-format' version '0.8'
}
Performed by the check task.
Introduced checkstyle plugin.
Check variables that are not mainly used, program complexity, etc.
In a nutshell, check for compliance with coding conventions.
Refer to Reference to specify the version.
Introduce google-java-style by referring to Reference
plugins {
id 'checkstyle'
}
checkstyle {
toolVersion = '8.29'
configFile = rootProject.file(new File(tollSettingsDirectory.toString(), "checkstyle/checkstyle.xml").toString())
}
Introduced SpotBugs Plugin.
Check for bug-prone structures.
plugins {
id "com.github.spotbugs" version "3.0.0"
}
spotbugs {
toolVersion = "3.1.12"
excludeFilter = rootProject.file(new File(tollSettingsDirectory.toString(), "spotbugs/exclude_filter.xml").toString())
}
Run with jacocoTestReport task.
Create a report with jacoco
Introduced jacoco plugin.
Set by referring to the Gradle user manual.
plugins {
id 'jacoco'
}
jacoco {
toolVersion = "0.8.5"
reportsDir = file("${buildDir}/JacocoReports")
}
jacocoTestReport {
reports {
html.destination file("${buildDir}/JacocoReports/html")
}
}
jacocoTestReport.shouldRunAfter(test)
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)
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:
Recommended Posts