Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
756 views
in Technique[技术] by (71.8m points)

build.gradle - Gradle does not use the Maven Local Repository for a new dependency

I have Maven with M2_HOME defined to /Users/manuelj/apache/maven/3.2.5

I have the settings.xml file, located on /Users/manuelj/apache/maven/3.2.5/conf/settings.xml

where I have the following declared:

<localRepository>/Users/manuelj/apache/maven/repository</localRepository>

Until here with Maven all works fine. Any new dependency goes there.

I have a project based with Gradle, among many things in my build.gradle, exists the following:

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'application'

version = '1.0.0'
sourceCompatibility = '1.8'

repositories {
   mavenLocal()
   mavenCentral()
}
… more

Until here, all works fine too. Code compile, executes well.

My confusion is the following.

According with my understanding is that Gradle's mavenLocal() should use the same path than <localRepository> defined on Maven's settings.xml file.

Now confirming that in the Maven local repository exists some dependencies already downloaded.

When I execute for example gradle build, I did realize that

  • If a dependency already exists from the Maven Local Repository, it is used from there.
  • If a dependency does not exist from the Maven Local Repository Gradle download the new dependency to: /Users/manuelj/.gradle/caches/modules-2/files-2.1

I want that the new dependency go directly to the same Maven Local Repository.

Therefore, what extra configuration is need it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Resolving Dependencies From Local Maven Repository

Gradle is able to resolve artifacts stored in the local Maven repository (usually ~/.m2/repository) via mavenLocal().

According to the documentation, mavenLocal() is resolved like this:

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.xmlis available, Gradle uses the default location USER_HOME/.m2/repository.

To resolve artifacts from a non-standard local Maven repository, you can use the following configuration in your build.gradle:

repositories {
    maven {
        url '/Users/manuelj/apache/maven/repository'
    }
}

(From: How does Gradle resolve the directory of the local maven repository?)

Custom Maven repositories are documented here.

Storing Artifacts in the Local Maven Repository

Gradle stores resolved dependencies in its own Dependency Cache. The dependency cache is so much more than just a simple Maven artifact repository:

  • Stores binaries (jars), artifact meta-data (POM, Ivy files), dependency resolution results and module descriptors.
  • Tuned for performance, for example shorter file paths.
  • De-duplicates artifacts: Same binaries are stored only once.
  • Tracks where a dependency came from. A dependency resolved from jcenter() might be different to the one resolved from mavenCentral().
  • Automatic, time and usage bases, cache cleanup.

Artifacts produced by the build can be easily pushed to the local Maven repository via publishToMavenLocal task contributed by the Maven Publish Plugin.

But what about resolved dependencies? For the aforementioned reasons, Gradle cannot store dependencies in the local Maven repository. There's currently no built-in functionality to even publish dependencies to the Maven's local repository from the build script. So what are your options:

  • Create a shell script that does the necessary legwork. Daniel Dietrich once wrote one and published it on Twitter.
  • Use an artifact proxy like Nexus or Artifactory. Maven and Gradle can be configured to consume dependencies from the same proxy. This setup is quite common in professional environments and my personal preference.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...