how to make a gradle build using only the local dependencies? - gradle

I am trying to make a gradle build.
The repositories are now unaccessible (due my internet connection).
But I had made a build with those libraries already and are compiled at my computer locally.
I want to make a gradle build without going to the internet and look for them, instead, want to fetch them locally.
I have tried adding this:
buildscript {
repositories {
mavenLocal()
...
But is not really working, any idea how to do it? or through the command line?

There are 2 places where you can declare your repositories, each of them affects different things in the build.
The one you mentioned, within the buildscript only states that for this buildscript (build.gradle), resolve the dependencies (practically classes) from the local maven repository.
If you want to resolve the dependencies which you declared under the dependencies block, you have to go straight with
repositories {
mavenLocal()
}

Related

Gradle, mavenLocal() and --refresh-dependencies

When declaring the following into my build.gradle:
repositories {
mavenLocal()
}
I can re-deploy similar Maven Artifacts (similar = with the exact same version) to Maven local, Gradle will pick up the latest which has been installed, without the need of using --refresh-dependencies
If, instead of declaring a mavenLocal() repository, I'm declaring a Maven Remote Repository, then I'll have to include --refresh-dependencies in order to be 100% sure I'm getting the latest of a published version. No problem here, this is expected ...
However, I don't understand why the same is not true for a mavenLocal() repository. I couldn't find any explanation in the documentation: Declaring a changing version.
Does anyone have any hints?
Caching is disabled for local repositories.
The paragraph The case for mavenLocal() from the Declaring repositories enumerates the various downsides of using mavenLocal() and explains the behavior you observe:
To mitigate the fact that metadata and/or artifacts can be changed, Gradle does not perform any caching for local repositories

How to cache and use gradle plugins locally?

I am trying to build my application with limited access to internet with Gradle.
I was successfully able to use normal jars with
repositories {
mavenLocal()
...
and populating ~\.m2\repository with appropriate files.
But I also have statements like
plugins {
id 'org.hidetake.ssh' version '2.4.0'
}
in my build.gradle. When build, I get the following error
Error resolving plugin [id: 'org.hidetake.ssh', version: '2.4.0']
> Could not GET 'https://plugins.gradle.org/api/gradle/2.9/plugin/use/org.hidetake.ssh/2.4.0'.
> Connection to https://plugins.gradle.org refused
Apparently, Gradle is trying to get plugin from Internet, which is prohibited.
How can I cache this plugin?
On my another machine, which has full internet access, I found directories like
.gradle\caches
but it has some strange hash subdirectories, which I am not sure how to use. Copying of modules-2 directory from another machine didn't help.
I suppose, it's not possible with the new plugins DSL, because in that case, plugin is specified by it's global unique id and version, which are not stored in your cache.
You can use for that some local plugin repository with required additional Plugin Marker Artifact, read about it here.
Or just don't use plugins dsl and declare your dependencies in plain old way with apply plugin: 'plugin_name' and storing required jars in your local repository the same way, it's done with you project dependencies.
you add the file settings.gradle
pluginManagement {
repositories {
mavenLocal()
}
}
and now it will onlz try to resolve plugins from ~/.m2/repository/

How to add an additional repository at the top of the list for a particular subproject in Gradle

I have a multi project setup in Gradle, the root of which specifies a bunch of repositories that are shared by all subprojects:
subprojects {
repositories {
maven { url '...' }
mavenLocal()
mavenCentral()
}
}
How can I specify an additional repository for a particular subproject, and have it as the first in the list of repositories?
Why do I need to do this? I need to include an Android library in .aar format. It is present in .aar format in this additional repository, and in .apklib format in Maven Central. If Gradle hits Maven Central first and finds the .apklib it will bail out.
By postponing the addition of the repositories in the root build script, the repositories are added after the ones added in the subproject:
subprojects {
afterEvaluate {
repositories {
maven { url '...' }
mavenLocal()
mavenCentral()
}
}
}
i would recommend to use something for external&internal repository management.
i think that artifactory or nexus can handle remote/virtual repositories, and makes it possible to use only one repository in your project.
note: if repository order is relevant to you, that might mean that the artifact identifier (group:ident:version) can mean different artifacts - i think that violates some maven/etc guideline which can cause invalid caches behind gradle and cause mysterious problems

Gradle prefer repository on duplicate entries

I have a build tool thats tied the version to the SCM. I can't set the version of a jar when I build it locally. If someone were to change what I'm working on locally it would push the version number (which I can get), but when I publish to my local repo (Ivy) Gradle seems to prefer the external repo.
build.gradle
repositories {
mavenCentral()
ivy {
url "${System.properties['user.home']}/.ivy2/local/"
layout "pattern", {
artifact "[organization]/[module]/[revision]/[artifact](-[classifier]).[ext]"
ivy "[organization]/[module]/[revision]/ivy.xml"
}
}
ivy {
url "https://repo/"
layout "pattern", {
artifact myPattern
ivy myIvyPattern
}
}
}
Without changing the build for the jar that I'm editing. How can I have gradle always prefer the local repo? I have a feeling that resolutionStrategy might be the best way, but I don't know how accomplish this.
Edit 1
To help clarify, Artifactory has a jar (published by jenkins) with version 1.2.3. I have a jar that I build locally that saves into my local repository as 1.2.3. When I build a project having both repositories in my repository closure (with my local one on top) Gradle seems to pull in the one from Artifactory.
Edit 2
Dependency definition
dependencies {
compile ('company:project:1.2.+')
}
I don't really understand what you are saying, but Gradle searches repositories in their declared order, and picks the first matching module that it finds (as least as long as fixed versions are used).

How to define repositories for all subprojects in Gradle

I am writing Gradle scripts to build a lot of projects. They are using the same repositories so I would like to define repositories for all of my sub-projects instead of defining in each of them.
So I try to move the repositories definition from the build.gradle in an individual project into the build.gradle in their parent folder.
subprojects{
repositories{
mavenCentral()
flatDir{
name 'uploadRepository'
dirs '../../sharedlib'
}
}
}
However, the sub-projects can't find the repository definition at all. Moving other configurations in subprojects closure work. I've tried dependencies and properties configuration. They all work with no problem. I don't know why repositories work differently.
When Googling, I can't find any example of putting repositories inside subprojects, I suspect I am doing it the wrong way. Please tell me what's wrong.
Thanks!
I finally figured out what the problem was.
Originally, I missed the settings.gradle in the parent folder. (I don't know why dependencies configuration works even without this file)
After I put that in, the sub-projects could find the repositories, but the dependencies and an one property (sourceCompatibility=1.5) I defined in the parent project no longer works.
I have to move the apply plugin:'war' from the subproject's build.gradle to the parent's subprojects{...}
I figure that's because the dependencies and sourceCompatibility are things provided by the plugin. And somehow Gradle doesn't look into the subproject's script to find the plugin first.
Your repository declarations look fine, except that an upload repository isn't declared in the project.repositories block but inside the upload task (e.g. uploadArchives). The Gradle User Guide has the details.

Resources