🎓 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
1. Define Dependencies Clearly
The first step in managing dependencies is to clearly define them in your build.gradle file. Group dependencies by their scope, such as implementation, testImplementation, and runtimeOnly.
Example
dependencies {
implementation 'org.apache.commons:commons-lang3:3.13.0'
implementation 'com.google.guava:guava:32.0.1-jre'
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2'
testImplementation 'org.mockito:mockito-core:5.3.1'
runtimeOnly 'mysql:mysql-connector-java:8.1.0'
}
2. Use Dependency Management Plugins
Gradle provides plugins to help manage dependencies more effectively. The java-library plugin, for instance, provides capabilities for defining API and implementation dependencies.
Example
plugins {
id 'java-library'
}
dependencies {
api 'com.google.code.gson:gson:2.8.9'
implementation 'org.apache.commons:commons-math3:3.6.1'
}
Explanation
api: Dependencies exposed to consumers of the library.implementation: Dependencies used internally within the library.
3. Leverage BOM (Bill of Materials)
Using a BOM ensures consistent versions of dependencies across different modules of a project. The platform dependency type is used to import a BOM.
Example
dependencies {
implementation platform('org.springframework.boot:spring-boot-dependencies:3.0.2')
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
}
4. Declare Versions in ext or versions.properties
To manage versions centrally, declare them in an ext block or in a separate versions.properties file. This makes version upgrades easier and ensures consistency across the project.
Example
Using ext Block
ext {
guavaVersion = '32.0.1-jre'
commonsLangVersion = '3.13.0'
}
dependencies {
implementation "com.google.guava:guava:$guavaVersion"
implementation "org.apache.commons:commons-lang3:$commonsLangVersion"
}
Using versions.properties File
guavaVersion=32.0.1-jre
commonsLangVersion=3.13.0
def versions = new Properties()
file("versions.properties").withInputStream { versions.load(it) }
dependencies {
implementation "com.google.guava:guava:${versions.guavaVersion}"
implementation "org.apache.commons:commons-lang3:${versions.commonsLangVersion}"
}
5. Use Dependency Constraints
Dependency constraints allow you to define the version of a dependency used across the entire build, even if it's brought in transitively by another dependency.
Example
dependencies {
constraints {
implementation 'com.google.guava:guava:32.0.1-jre'
implementation 'org.apache.commons:commons-lang3:3.13.0'
}
}
6. Exclude Unwanted Transitive Dependencies
Sometimes, dependencies may bring in unwanted transitive dependencies. Use the exclude keyword to prevent them from being included.
Example
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
}
7. Use Dependency Locking
Dependency locking helps to ensure that the same versions of dependencies are used every time you build the project, which is crucial for consistent builds.
Example
dependencyLocking {
lockAllConfigurations()
}
tasks.register('saveLock') {
doLast {
project.configurations.each { configuration ->
configuration.resolvedConfiguration.recompile()
}
}
}
Conclusion
Effective dependency management is key to maintaining a healthy and manageable project. By following these best practices, you can ensure your Gradle builds are reliable, maintainable, and up-to-date. Gradle's rich set of features and plugins provides a powerful toolkit for handling dependencies in any project.
Further Reading
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