Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle with Java: When does .m2 folder gets created while using gradle with Java

Tags:

java

maven

gradle

  1. Here, I understand that the dependencies are getting downloaded from the maven repository. And wondering if .m2 folder will be created when I execute this. or will this work without creating .m2 local repo. I read that local repo is needed only for maven, not grade. So, in this case where Gradle is used to access maven repo to download dependencies, is .m2 folder needed.
  2. When does this .m2 folder gets created?
  3. After the Gradle build is successful, I did check in my {user home} but there are no .m2 created
  4. I did try getting info from Google but didn't get any clarity on this. confused on how does Gradle uses .m2 while building its project.
 apply plugin: 'eclipse'
 apply plugin: 'maven'
 apply plugin: 'idea'
 apply plugin: 'groovy'

 sourceCompatibility = JavaVersion.VERSION_1_8
 targetCompatibility = JavaVersion.VERSION_1_8

 //Artifactory Central Repository
 buildscript {
     repositories {
         maven {
             url 'abc'
             credentials {
                 username = ""
                 password = ""
             }
like image 945
spandey Avatar asked Dec 17 '25 19:12

spandey


1 Answers

Gradle Dependency Cache

Gradle uses its own dependency cache:

Gradle contains a highly sophisticated dependency caching mechanism, which seeks to minimise the number of remote requests made in dependency resolution, while striving to guarantee that the results of dependency resolution are correct and reproducible.

The Gradle dependency cache consists of two storage types located under GRADLE_USER_HOME/caches:

  • A file-based store of downloaded artifacts, including binaries like jars as well as raw downloaded meta-data like POM files and Ivy files. The storage path for a downloaded artifact includes the SHA1 checksum, meaning that 2 artifacts with the same name but different content can easily be cached.
  • A binary store of resolved module metadata, including the results of resolving dynamic versions, module descriptors, and artifacts.

[...]

Where, as documented here, GRADLE_USER_HOME is:

The Gradle user home directory ($USER_HOME/.gradle by default) is used to store global configuration properties and initialization scripts as well as caches and log files. [...]


Local Maven Repository

You can configure Gradle to search your local Maven repository:

[...]

To declare the local Maven cache as a repository add this to your build script:

Example 8. Adding the local Maven cache as a repository

repositories {
    mavenLocal() 
}

Gradle uses the same logic as Maven to identify the location of your local Maven cache. If a local repository location is defined in a settings.xml, this location will be used. The settings.xml in USER_HOME/.m2 takes precedence over the settings.xml in M2_HOME/conf. If no settings.xml is available, Gradle uses the default location USER_HOME/.m2/repository.

Note, however, that Gradle cautions against using the local Maven repository unless you really need it.

With the maven-publish plugin you can publish to your local Maven repository, if needed:

For integration with a local Maven installation, it is sometimes useful to publish the module into the Maven local repository (typically at $USER_HOME/.m2/repository), along with its POM file and other metadata. In Maven parlance, this is referred to as 'installing' the module.

The Maven Publish Plugin makes this easy to do by automatically creating a PublishToMavenLocal task for each MavenPublication in the publishing.publications container. The task name follows the pattern of publishubNamePublicationToMavenLocal. Each of these tasks is wired into the publishToMavenLocal aggregate task. You do not need to have mavenLocal() in your publishing.repositories section.

like image 57
Slaw Avatar answered Dec 20 '25 08:12

Slaw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!