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

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.

Related

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

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.

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.

Add Gradle cache to Sonatype Nexus

Is there an option available to add the Gradle dependency cache to Sonatype Nexus repository so that i can use that cached dependencies for my project later.
The easiest way will be writing a Gradle script, that will exact all the artifacts from Gradle cache using Gradle Artifact Query APIĀ and save them in a Maven layout. Then you can import them to Nexus or Artifactory.

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.)

Maven private internal repository

I have create a maven local depository using "Artifactory". I want to add project dependencies to this local repository(all dependencies in my local .m2 folder), can I do that with Artifactory?. If can, how can I do it?
The dependencies will be added to Artifactory on the first run of your maven project (they will be fetched from one of the remote repositories Artifactory come preconfigured with).

Resources