Gradle configuration to use maven local repository - gradle

I have Maven and Gradle both installed in my system.
By default maven stores the dependency jars in the USER_HOME/.m2/repository and Gradle stores in folder USER_HOME/.gradle/caches/modules-2 respectively.
For the same project, I am end up with 2 copies of jars in my system ultimately.
Is there any configuration to set up in the gradle at global level so that it uses existing maven repository instead of its own.
Thanks in advance.

You can use
mavenLocal()
If location of repository must be in other place you can use
repositories {
maven {
url '/path/to/your/repository'
}
}

Related

Gradle repositories in Gradle settings instead of build?

In Maven, you can store the list of repositories in settings.xml, which means each developer can have his own settings (when you are inside a company, you will use the local Nexus as a mirror, but when outside, you will use Maven Central).
How do you do that with Gradle ?
You can use a gradle init script for that. these init scripts can live in the ~/.gradle/init.d/ folder. Having on (e.g. default-repos.gradle) with adding default repositories can look like this:
allprojects {
repositories {
maven {
url = 'http://nexus.local.org'
}
}
}

Dependenciies in Gradle not working correctly

We have a project that is using Java 1.5 and we are trying to convert from Maven to Gradle.
We have a repository that is local to us containing all the versions of all the jars we need as the dev environment has no access to the internet.
The problem we are seeing is that it cannot find the commons-io jar and keeps trying to goto the external maven repo. we have not even set that up so where is it finding it from?
we have repositories and dependencies set up in the All projects section as follows
allprojects {
apply plugin: 'java'
sourceCompatibility = 1.5
targetCompatibility = 1.5
project.tasks.withType(AbstractCompile, { AbstractCompile ac -> ac.options.bootClasspath = "C:/Program Files/java/1.5.0_14/jre/lib/rt.jar" })
repositories {
mavenLocal()
maven { url "http://internalrepo/maven-local" }
}
dependencies {
compile "org.apache.commons:commons-io:1.3.2"
}
But its reporting
Could not resolve org.apache.commons:commons-io:1.3.2.
inconsistent module metadata found
even though it works fine in Maven using mvn install
Gradle will never query a repo that isn't set up. mavenlocal() is misspelled (should be mavenLocal()), which will make the build fail. "Inconsistent metadata" could mean that the group ID, artifact ID, or version in the POM doesn't match the one in the build script. mavenLocal() should only be used if the Gradle build needs to exchange artifacts with local Maven builds.
Found the issue,
Unbeknownst to me there was a hidden repo in the maven settings.xml in the maven install folder.
Adding that resolved the issue.

How to set global repositories for gradle that I can use them in each gradle project

It's slow to visit the maven official repositories from my country, so I want to try some local repositories first.
Is it able to add them to some global gradle configuration file, that I can just use them in each gradle project, without modifying the project-scope build.gradle file?
I would suggest to use the init.gradle script which is located in your home folder $HOME/.gradle/init.gradle which might contain the following content:
allprojects {
repositories {
mavenLocal()
maven {
url "http://localhost:8081/nexus/content/groups/public/"
}
}
}
I guess that what are You looking for are init scripts.

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 publish in order to resolve latest.integration with gradle?

What I have is a maven repository (nexus) to which maven has been publishing. In each artifact version folder in my artifact repository folder there are the standard maven artifacts: a maven-metadata.xml, a jar, and a pom.xml, etc.
Now I want to resolve these using gradle. In my gradle.build file if I list them as:
dependencies {
compile group: 'com.company', name: 'artifact', version: '1.0-SNAPSHOT'
}
Then they will resolve correctly. However, I want to use the version "latest.integration" so that I can automatically integrate the latest versions of my dependencies. When I do this though, gradle fails to resolve it.
I imagine that gradle is looking for some ivy specific files that maven is not publishing up to the repository in order to resolve latest.integration, but I am not sure. Should I go back and re-publish all of my upstream dependencies with gradle before trying to resolve down stream? It would seem that since gradle supports maven repositories under the repositories element that it should already know how to interpret "latest.integration" for that repository type.
This is my repositories section:
repositories {
mavenCentral()
maven { url "http://<server>/nexus/content/repositories/snapshots" }
}
Thank you for any help you can provide
latest.integration is an Ivy concept, and only works for Ivy repositories. In other words, both publication and consumption would have to happen in an Ivy-compatible manner. (Gradle is capable of this; not sure about Nexus.)
The obvious alternative is to use Maven snapshot dependencies. What do you hope to gain from using latest.integration?

Resources