🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Applying Plugins
To apply a plugin in Gradle, you can use the plugins block in your build.gradle file:
plugins {
id 'java' // Java plugin
id 'application' // Application plugin
}
Essential Gradle Plugins
Java Plugin
The Java plugin adds support for Java projects, including compiling Java source code, generating JAR files, and running tests.
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
}
test {
useJUnitPlatform()
}
Application Plugin
The Application plugin facilitates building and packaging Java applications. It provides tasks for running and creating distributions.
plugins {
id 'application'
}
mainClassName = 'com.example.Main' // Replace with your main class
application {
applicationDefaultJvmArgs = ["-Dmyapp.property=somevalue"]
}
Maven Publish Plugin
The Maven Publish plugin allows you to publish build artifacts to a Maven repository.
plugins {
id 'maven-publish'
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
maven {
url = uri('https://repo.mycompany.com/maven2')
credentials {
username = project.findProperty("repo.user")
password = project.findProperty("repo.password")
}
}
}
}
Shadow Plugin
The Shadow plugin packages an application and its dependencies into a single executable JAR file, also known as a fat JAR.
plugins {
id 'com.github.johnrengelman.shadow' version '8.0.0'
}
dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.2'
}
shadowJar {
archiveClassifier.set('all')
}
Checkstyle Plugin
The Checkstyle plugin performs static code analysis to help enforce coding standards.
plugins {
id 'checkstyle'
}
checkstyle {
toolVersion = '10.12.0'
config = resources.text.fromFile('config/checkstyle/checkstyle.xml')
}
tasks.withType(Checkstyle).configureEach {
reports {
xml.required.set(false)
html.required.set(true)
}
}
SpotBugs Plugin
The SpotBugs plugin analyzes code for bugs and potential issues.
plugins {
id 'com.github.spotbugs' version '5.0.13'
}
tasks.withType(com.github.spotbugs.SpotBugsTask).configureEach {
reports {
xml.enabled = false
html.enabled = true
html.stylesheet = 'fancy-hist.xsl'
}
}
Jacoco Plugin
The Jacoco plugin provides code coverage metrics for your tests.
plugins {
id 'jacoco'
}
jacoco {
toolVersion = '0.8.8'
}
tasks.test {
finalizedBy tasks.jacocoTestReport
}
tasks.jacocoTestReport {
reports {
xml.required.set(true)
html.required.set(true)
}
}
Example: Complete Build Script
Here is a complete example of a build.gradle file using some of the plugins mentioned above:
plugins {
id 'java'
id 'application'
id 'maven-publish'
id 'com.github.johnrengelman.shadow' version '8.0.0'
id 'checkstyle'
id 'com.github.spotbugs' version '5.0.13'
id 'jacoco'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
}
mainClassName = 'com.example.Main' // Replace with your main class
application {
applicationDefaultJvmArgs = ["-Dmyapp.property=somevalue"]
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
maven {
url = uri('https://repo.mycompany.com/maven2')
credentials {
username = project.findProperty("repo.user")
password = project.findProperty("repo.password")
}
}
}
}
shadowJar {
archiveClassifier.set('all')
}
checkstyle {
toolVersion = '10.12.0'
config = resources.text.fromFile('config/checkstyle/checkstyle.xml')
}
tasks.withType(Checkstyle).configureEach {
reports {
xml.required.set(false)
html.required.set(true)
}
}
tasks.withType(com.github.spotbugs.SpotBugsTask).configureEach {
reports {
xml.enabled = false
html.enabled = true
html.stylesheet = 'fancy-hist.xsl'
}
}
jacoco {
toolVersion = '0.8.8'
}
tasks.test {
useJUnitPlatform()
finalizedBy tasks.jacocoTestReport
}
tasks.jacocoTestReport {
reports {
xml.required.set(true)
html.required.set(true)
}
}
Conclusion
Gradle plugins are essential for extending the functionality of your build scripts. They simplify tasks such as compiling code, packaging applications, and running tests. This guide covered some of the most commonly used plugins with examples. Always ensure to use the latest versions of dependencies and plugins for better performance and security.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment