45

For some reason my offshore team is not able to Download the artifacts(Dependencies) from my clients Artifactory which is our Organizations dependency repository."Refresh Dependencies" triggers nothing and gives TIME OUT EXCEPTION. I see the that my "Gradle Dependencies" are downloaded to the location "D:/C813507/Gradle/gradle-1.11/bin/caches/modules-2/files-2.1/". Can i zip this folder and send over to them ? How do they implement something in gradle that points to their local directory. My build gradle has the following line. How to point the URL to local directory in Windows OS

repositories {
         maven {
            url 'http://artifactory.myorg.com:8081/artifactory/plugins-release'
        }

}
Vinodh Thiagarajan
  • 698
  • 3
  • 9
  • 19

4 Answers4

45

If you can't give access to your offshore team, you can copy all dependencies jar that needed to a single directory, and then use flatDir repository.

repositories {
   flatDir {
       dirs 'D:/path/to/local/directory'
   }
}


dependencies {
   compile name: 'name-of-jar'
}

Another way without using flatDir repository is:

dependencies {
    compile files('/path/to/dir/something_local.jar')
}
dieend
  • 2,111
  • 1
  • 23
  • 29
  • 9
    Note that with a flatDir repository, there is no transitive dependency resolution. It would be better to investigate and fix the original problem. – Peter Niederwieser Sep 22 '14 at 08:26
  • I have question regarding the build dependencies in the gradle.build for the local repository (i.e. using the local directory) dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') } Does it solve the dependencies in the libs directory only or it is resolves the dependencies on all the subfolders of lib directory. If it does not resolve the dependencies of subfolders/subdirectories, how to resolve the dependencies? note: Our project depends on lot of jars files(instead of giving the full file name for each jars/libs), so wants to know any alternate way. – mgr Jun 27 '16 at 16:39
  • @mallikgm I think you should ask it in a new question to give clearer context. I am guessing fileTree should works with sub dir. But I haven't tried it – dieend Jun 28 '16 at 07:29
  • I have asked a new question http://stackoverflow.com/questions/38059123/gradle-repository-points-to-local-directory-with-multiple-libs – mgr Jun 28 '16 at 10:15
44

Instead of configuring flatDir repository, you can declare a local maven repository as following:

repositories {
   maven {
       url 'file://D:/path/to/local/directory'
   }
}

As @Peter Niederwieser mentioned, flatDir repository doesn't support transitive dependency resolution. maven local repository does.

Alex Lipov
  • 12,860
  • 5
  • 62
  • 83
  • 1
    This will not work across different environments (specifically on the CI server) and breaks the principle "the build process should be same everywhere" – user3118604 Nov 07 '17 at 09:21
  • 15
    True. However, OP explicitly stated that he's looking for **local directory** solution, and therefore your comment is irrelevant in this context. – Alex Lipov Nov 07 '17 at 09:30
  • Can the gradle cache be converted into local maven repository ? – Subra M Jul 29 '19 at 20:27
  • 1
    @SubraM I believe it's worth a separate question, though the workarounds provided here might answer your needs: https://discuss.gradle.org/t/need-a-gradle-task-to-copy-all-dependencies-to-a-local-maven-repo/13397 – Alex Lipov Jul 30 '19 at 08:01
  • You can also either supply this repo via init.gradle, or have some conditional logic in your build to only use the local repo if property 'offshore' is true, or something like that. – Cinderhaze Oct 03 '19 at 17:06
  • How do I refer to the dependency when using this trick? Nothing I try seems to work. – BlueRaja - Danny Pflughoeft Dec 01 '21 at 02:20
  • @BlueRaja-DannyPflughoeft In exactly the same way you declare other dependencies, i.e. `implementation 'org.apache.httpcomponents:httpclient:4.5.5'`. – Alex Lipov Dec 01 '21 at 07:03
15

Using flatDir like below is one option :

repositories {
   flatDir {
       dirs 'D:/path/to/local/directory'
   }
}

This works. But if there is mavenCentral() or other maven repositories that contain the same JAR, priority will be given to maven repository over flatDir.

Excerpt from Gradle Documentation : https://docs.gradle.org/current/userguide/repository_types.html

Gradle will dynamically generate a module descriptor (without any dependency information) based on the presence of artifacts. However, as Gradle prefers to use modules whose descriptor has been created from real meta-data rather than being generated, flat directory repositories cannot be used to override artifacts with real meta-data from other repositories. For example, if Gradle finds only jmxri-1.2.1.jar in a flat directory repository, but jmxri-1.2.1.pom in another repository that supports meta-data, it will use the second repository to provide the module.

So flatDir is not good. We should use local maven repository like mentioned in the second answer. But there are couple of issues with the second answer

  1. Not portable
  2. If the artifact is also present in the global repo's, it is unclear from where will it be picked from.

We can solve these issues by using a solution like below :

  1. Create a folder called libs in the project directory
  2. Copy your artifacts into this libs directory (if you are using git or any other scm's, make sure to distribute this libs fodler so that others can just gradle build without any issues)
  3. Declare a maven repo to your libs directory and call mavenLocal() before listing any other repos. This step is very important to make sure that the jar is picked from your local directory rather than from the other global repositories
repositories {
   maven {
       url uri("${projectDir}/libs")
   }

   mavenLocal()
   <All your Repos here>
   mavenCentral()
}

This way you can be sure that even if the JAR is present in other repo's it will only be picked from your local directory and the solution is also portable.

Sandeep
  • 17,036
  • 13
  • 64
  • 103
  • I followed your solution, it means I copied all folders of gradle cache [.gradle\caches\modules-2\files-2.1] into my local repository and after that I deleted the cache but I got an error during syncing gradle as [Could not resolve all artifacts for configuration ':classpath'. > Could not resolve com.android.tools.build:gradle:3.5.2.] this is my gradle: repositories { maven { url uri("./repository") } mavenLocal() jcenter() google() mavenCentral() maven { url 'https://maven.fabric.io/public' }} – Mojtaba Dec 03 '19 at 15:10
  • @Mojtaba You still facing the issue? – Sandeep Dec 29 '19 at 03:19
  • Yes, I still have the issue – Mojtaba Dec 31 '19 at 07:03
  • 1
    I realize this is an old issue, but maybe this will help someone anyway. The gradle caches don't work directly as a repository - the paths need to be converted to maven format first. This script for example does that: [link](https://github.com/sinsongdev/gradle-cash-to-maven-repo) – Pauli Kettunen Sep 20 '21 at 12:21
0

Open a file in the directory with a browser, and then copy the path.

 maven { url = "file:///C:/lib/repo/"}
Ag2S
  • 31
  • 2