

How to cache Gradle build dependencies to a local Maven repository
source link: https://blog.nishtahir.com/caching-gradle-build-dependencies-to-a-local-maven-repository/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to cache Gradle build dependencies to a local Maven repository
I've occasionally found it helpful to cache a copy of a project's dependencies to a local file system. My most recent use case for this called for a local containerized test build without access to an internal network to fetch internal dependencies. An easy workaround was make the project resolve dependencies from a local maven cache that can be mounted as a volume in the container. Building the cache was accomplished using a Gradle task[1] similar to this:
task.register("cacheToMavenLocal", Sync) {
from new File(gradle.gradleUserHomeDir, 'caches/modules-2/files-2.1')
into "${rootDir}/local-m2"
// Last copy target wins
duplicatesStrategy = 'include'
eachFile {
List<String> parts = it.path.split('/')
// Construct a maven repo file tree from the path
it.path = parts[0].replace('.','/') +
'/' + parts[1] +
'/' + parts[2] +
'/' + parts[4]
}
includeEmptyDirs false
}
This is a very aggressive way of building a cache since it is copying your entire Gradle cache into the target directory. You can use includes
and excludes
patterns[2] to copy only specific dependencies or exclude unnecessary ones.
This can be executed like any other Gradle task
./gradlew cacheToMavenLocal
And the resulting files will be copied into the target directory. In my example that would be the local-m2
folder in the project root directory.
To have your project search your local maven cache for dependencies you can point to your local cache like any custom maven repository in the repositories
block.
repositories {
maven {
url "${rootDir}/local-m2"
}
}
Gradle Forums. 2021. Need a Gradle task to copy ALL dependencies to a local maven repo. [online] Available at: https://discuss.gradle.org/t/need-a-gradle-task-to-copy-all-dependencies-to-a-local-maven-repo/13397/15 [Accessed 11 November 2021]. ↩︎
Gradle. 2021. Gradle API Documentation: Sync. [online] Available at: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Sync.html [Accessed 11 November 2021]. ↩︎
Recommend
-
67
My experiences with Gradle and why I finally moved back to Maven
-
27
As projects grow we can quickly run into issues with dependency management. We will explore the ways Gradle allows us to define our dependencies, ending with looking at using Kotlin source files and easily accessing them from our Gradle scripts....
-
8
One more step Please complete the security check to access www.mkyong.com What can I do to prevent t...
-
9
In the previous article, I’ve covered the general ideas around Swagger Brake and how to use the command-line interface to check your API for breaking changes. Now, I’ll...
-
57
Gradle 7.X Maven publish 适配 2021-08-02 就在前几天,Android studio 更新到了 4.2,伴随着正式的JDK 11环境以及 Gradle 7.0,迎来了一系列的 gradle 编译问题,当然,这里主要讨论 maven 发布相关的变更与适配。这里直接抛出...
-
14
Manage your Gradle dependencies with Version Catalog (not only) in AndroidIf you ever built a single module Android app, you probably managed your dependencies manually. We all started there. Then as you move to a multi-mo...
-
6
Keeping Gradle Dependencies up to Date with GitHub Actions and RefreshVersions How to use GitHub Actions to semi-automate dependency updates using RefreshVersions
-
9
Easy publishing to Maven Central with Gradle Nov 30, 2015 • Max •...
-
8
Conditional dependencies with Gradle 21 March 2022 Tags: gradle
-
8
Strategies for managing Gradle dependenciesYou choose how to manage the dependencies!
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK