Why gradle not creating local repository and downloading same dependencies for every project - maven

I'm using Gradle with Java Project in IntelliJ Idea.
I see that Gradle downloading dependencies for first time on opening project.
But there is another project with same dependencies then also it's re-downloading those libs. why?
Why doesn't it maintain Maven like local repository even after configured?
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
How can Gradle maintain local repository and next it should first check local repo and go for download if no matching dependencies found?

With that piece of code you instruct gradle to look at the local maven repository, then at the central maven repository and last in JCenter when looking for dependencies. The first one it finds your dependency it takes it from.
It does not instruct Gradle to put resolved dependencies to the local maven repository though. This is e. g. helpful if you have two projects in two separate builds, one depends on the other and you install the first dependency to the local maven repository with the respective Gradle task and then build the second one, depending on the version you just built and installed.
Gradle has a resolution cache though in ~/.gradle/caches/modules-2/files-2.1/ where it caches all downloaded dependencies and also reuses this from different builds.

Related

Gradle dependencies pom files locations on local file system

I understand that Gradle stores project dependencies cache in ~/.gradle/caches/modules-2 for the actual jar files. However, does Gradle also store .pom anywhere locally? Also, if I previously downloaded same dependencies through Maven (stored in ~/.m2/repositories) would Gradle reuse them automatically? In Maven, pomfiles are downloaded along side jars.
If POMs are not downloaded as part of gradle build, what's the best way to programmatically get the POMs for package metadata?
Gradle seems to use a local cache and not a repository, unless depending on mavenLoacal().
You could use the API which mavenCentral() provides:
https://central.sonatype.org/search/rest-api-guide

Redirect Gradle project dependency on Maven artifact to another Gradle project

Is there any way to redirect a Gradle project dependency on Maven artifact coordinates to the output of another Gradle project in the same build?
I don’t see the point in having dependencies on things like project(‘:xyz’), since that requires that project :xyz be on my file system & built, and for the referencing project to know that :xyz is also in the same build. It makes more sense to me to have dependencies on Maven artifact coordinates, and to use artifacts built from projects in the same build that specify matching coordinates, instead of downloading the artifact from a repository.
If this functionality exists, or if I were to make it myself, I’d imagine that it would be implemented as an object that can be listed in the repositories closure before all other repositories like mavenCentral().
What you are looking for is composite builds. It will allow you to define that a build includes another build and thus Gradle figures out that a dependency notation group:name:version gets replaced by an included build.
However I would still recommend using the project(:xyz) notation when the projects belong to the same unit, which usually is a single checkout from source control.
You can publish xyz in your local repository.
In the project xyz, use this plugin :
apply plugin: 'maven-publish'
And just run gradle publishToMavenLocal
Then, in the "root" project, use in addition the local repository and use xyz as a regular dependency :
repositories {
mavenLocal()
}
dependencies {
compile('your.company:xyz:1.0.0')
}

When does gradle store in .m2 and when in cache?

In which scenario will gradle store artifacts in the directory .m2 and in which scenario will it store them in gradle\caches?
I am trying to resolve my issue wherein I have a dependency within my local build
Gradle will read from your local maven repository only when you declare it as a valid repository:
repositories {
mavenLocal()
}
Gradle will write to your local maven repository only when you publish artifacts and tell it to publish to the local maven repo.
If you are using the maven plugin, when executing the task install
If you are using the maven-publish plugin, when executing the task publishToMavenLocal
Gradle will use its own internal cache for all resolved dependencies, including ones coming from the local maven repository.
For example, if you use a dependency org:foo:1.0 from your maven local repository, the metadata and artifact will be copied to the Gradle cache on first resolution. From then on, the dependency will be resolved from the Gradle cache.
However, if the dependency is changing, such as when using a -SNAPSHOT version, the Gradle cache will by default keep the last one resolved for 24h. After which it will perform a new resolution, hitting again the local maven repository in this example.
See the documentation for controlling that cache duration for dynamic and/or changing dependencies.

Maven - Why Does it Keep Redownloading Dependencies?

When I add a new Maven dependency that I've never used before, I will do Maven build and see the dependencies being downloaded into my local machine from Nexus. All is good.
I will then create another project, specify the same dependency with the same version, do a Maven build, and I will again see the dependencies being downloaded from Nexus into my local machine.
Why are my dependencies re-downloaded every time? Aren't these dependencies already installed in my local repository?
Maven will NOT download artifacts repeatedly. The only exceptions are if you are deleting your local repository (in ~/.m2/repository by default), you are configuring usage of a different local repository and if a new SNAPSHOT version is available.

what's the difference between .ivy2 and .m2

Previously I am using sbt, and looks like it put jar under .ivy2. And then I used gradle, I have thought it also put jar under .ivy2, but the following link told me I need to delete corrupted package from .m2. So gridle is using .m2 and can not use ivy2?
spring boot mvc: failed after following the sample
Ivy, Maven and Gradle each have their own dependency cache in ~/.ivy2/cache, ~/.m2/repository, and ~/.gradle/caches, respectively. Gradle will only use Maven's dependency cache (known as the local Maven repository) if mavenLocal() is declared as a repository in the build script. This should only be done if the Gradle build needs to consume artifacts produced by a local Maven build. (There aren't any efficiency gains; in fact declaring mavenLocal() will make the build slower and less reliable.)

Resources