what's the difference between .ivy2 and .m2 - gradle

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

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

Does n't gradle have its own repository in the way Maven has m2?

Gradle always seems to use maven m2 repository. Does n't it have an equivalent of m2?
Gradle can consume dependencies in different layouts, including Maven, Ivy, Flat directory structures and absolute file locations. So it doesn't always use a Maven repository as you say.
When it downloads external dependencies, it caches them in $HOME/.gradle/caches/modules-2. This is partly what the .m2 is about, but the Gradle one is just a cache and things will be wiped from it after having been unused for a while.
You are also not supposed to use the Gradle cache directory as a local repository for sharing SNAPSHOT versions between projects during development, like you would with .m2. If you need to to this, Gradle can work with a local .m2 repository. Just declare:
repositories {
mavenLocal()
}
And you can consume and publish artifacts to the local .m2 folder, just like Maven would do.
There are also other ways to produce and consume local artifacts in Gradle, including multi-projects and composite builds, that are sometimes better suited than going through the .m2 repository.

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.

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.

Grails refresh-dependencies doesn't download snapshot dependency from local maven repository

I am using Grails 2.1.1 and Maven 3.0.3.
In my buildConfig.groovy, I have pom true and I generated the pom.xml via grails create-pom. In this pom I have a dependency with <version>1.0-SNAPSHOT</version> which exists only in my local maven repository. I can successfully run mvn clean compile on this pom.
However running grails refresh-dependencies does not download the most recent version of my snapshot dependency from my local maven repository. The only way I can get it to download the latest version is to manually delete it from the ivy cache.
According to the documentation:
All dependencies (jars and plugins) with a version number ending in -SNAPSHOT are implicitly considered to be changing by Grails.
I assume it would recognize my snapshot file as changing and download it when it is modified. Am I missing some other configuration step? I only want to use maven for dependency management, but is this entirely the wrong way to use Maven with Grails?
This is actually the normal behavior of the Aether resolver.
--refresh-dependencies doesn't bypass your local maven cache. To do that, you'll need to set the maven repository that contains your dependency to always download new snapshots. In BuildConfig.groovy's repositories block:
mavenRepo ("http://my.server/repos/my-grails-plugins") {
updatePolicy 'always'
}
Credit to http://asoftwareguy.com/2013/10/25/grails-2-3-maven-dependency-caching-issues/.
Since I haven't got any responses, what seems like the solution to this is to just not use the grails command line, but rather use the maven goals for Grails.
mvn grails:run-app does the trick. All snapshot dependencies are refreshed and I can start up my app and see the local changes reflected. This way I'm ignoring ivy altogether and letting maven take care of everything.
Edit: If you go this route, I suggest following chapter 5 of the User Guide on Maven Integration for setting up your pom.xml, etc. I was able to follow this and get it set up without any surprises.

Resources