How to rename a maven repository in Gradle - gradle

If I add 2 repositories in my build.gradle file, they will be autonamed ("maven" and "maven2") :
repositories {
maven { url "http://maven.springframework.org/release" }
maven { url "https://maven.fabric.io/public" }
}
Is there a way to name them explicitely ?

You should just be able to use name
repositories {
maven {
url "http://maven.springframework.org/release"
name 'spring-repo'
}
maven {
url "https://maven.fabric.io/public"
name 'fabric-repo'
}
}

Related

Gradle common repositories for multi module project

I have below project setup for gradle scripts, but I would like to keep one single common repository file for both main and subprojects.
apply from : "$rootDir/gradle/repositories.gradle" does not work inside build.gradle for this purpose. It would work only if I declare repositories again inside build.gradle
**build.gradle**
--------------------------
{
repositories {
maven {
url "https://myprivate.repo.com/artifactory"
credentials {
username = "user1"
password = "psswd"
}
}
}
dependencies {
# some list of dependencies
}
}
**gradle/repositories.gradle**
-------------------------
repositories {
maven {
url "https://myprivate.repo.com/artifactory"
credentials {
username = "user1"
password = "psswd"
}
}
}
**subprojects.gradle**
--------------------------
subprojects {
apply from : "$rootDir/gradle/repositories.gradle"
}

In build.gradle how to extract and reuse Maven repository configuration?

I've got this build.gradle file:
repositories {
maven {
credentials {
username "$artifactory_user"
password "$artifactory_password"
}
url 'http://some.domain/artifactory/repo'
}
}
publishing {
repositories {
publications {
maven(MavenPublication) {
from components.java
}
}
maven {
credentials {
username "$artifactory_user"
password "$artifactory_password"
}
url 'http://some.domain/artifactory/repo'
}
}
}
I want to extract and reuse the Maven repository definition.
This is not working:
repositories {
mavenRepository()
}
publishing {
repositories {
publications {
maven(MavenPublication) {
from components.java
}
}
mavenRepository()
}
}
private void mavenRepository() {
maven {
credentials {
username "$artifactory_user"
password "$artifactory_password"
}
url 'http://some.domain/artifactory/repo'
}
}
It results in
Could not find method mavenRepository() for arguments [gradle-dev] on
repository container of type
org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler.
How can I achieve this goal?
lets say you have a separate repositories.gradle file and name your maven repo..
maven {
name 'myrepo'
url 'https://xxxx'
credentials {
username = System.getenv("xxx") ?: "${xxx}"
password = System.getenv("xxx") ?: "${xxx}"
}
}
maven { name 'anotherrepo' }
then at root project level you can run:
apply from: "$rootDir/gradle/repositories.gradle"
This will populate repositories with all repos. Then to use populate a specific one somehwere else, you can use:
repositories.add(rootProject.repositories.getByName('myrepo'))
I found the answer on this blog page: https://themightyprogrammer.dev/snippet/reusing-gradle-repo
thanks to mightprogrammer!

Get a nexus artifact as a dependency in gradle?

I have the following nexus artifact :
com/companyName/my-awesome-util/0.0.0/my-awesome-util-0.0.0.jar
and I just have absolutely no clue how to get it within my gradle build file. I have googled it, followed examples, and I just cannot get it to work. I come from the maven world, and I just need to get this one thing to work so that I can at least understand what the hell I need to do in future.
First, add a repository in your build.gradle:
repositories {
maven { url 'http://nexus.acme.corp' }
}
Then, refer to that artifact:
dependencies {
compile 'com.companyName:my-awesome-util:0.0.0'
}
That's basically it!
If your Nexus is secured by credentials, use this snippet:
repositories {
maven {
url 'http://nexus.acme.corp'
credentials {
username = 'darth'
password = 'vader'
}
}
}
Add some Nexus maven2 proxy repositories with the following remote storage urls each:
https://jcenter.bintray.com/
https://repo1.maven.org/maven2/
https://maven.google.com/ # used to download com.android.tools.build:gradle
https://plugins.gradle.org/m2/
And then add them into a meven2 group repository, named such as maven-public:
And then use this group repo's url in your file build.gradle:
buildscript {
repositories {
google()
mavenCentral()
// jcenter()
maven {
// url "https://plugins.gradle.org/m2/"
url "https://YOUR-NEXUS-URL.com/repository/maven-public/"
}
}
......
........
......
allprojects {
repositories {
buildscript {
apply from: 'dependencies.gradle'
repositories {
google()
mavenCentral()
// jcenter()
maven {
// url "https://plugins.gradle.org/m2/"
url "https://YOUR-NEXUS-URL.com/repository/maven-public/"
}
}
}
}

Can I add a custom repository to gradle.properties?

I'd like to be able to define a repository in settings (ideally user gradle.properties)
The end goal is something like this:
repositories {
mavenCentral() // Can't/don't want to use this
nexusCentral() // Can use these - on network Nexus server
nexusSnapshot()
}
How would I go about doing this? Again, this would go in the user-level gradle.properties file ideally, so we don't have to reference it in every single module.
This is just a plain maven style artifact repository provided by Maven, the manual way would be:
maven {
url "http://path/to/nexus"
}
One other requirement is the use of the "publish" task, which has credentials defined for a repository (that Jenkins uses to publish the module):
publishing {
...
maven {
url "http://path/to/nexus"
// Jenkins provides these as -P Gradle parameters.
credentials {
username = "${uploaderUser}"
password = "${uploaderPassword}"
}
}
These credentials would not be known to regular users, but would ideally be configured in Jenkin's gradle.properties. We wouldn't want users builds to fail because they can't resolve the credentials - they would never even use the "publish" task.
You can use somenthing like this:
maven {
credentials {
username getCredentialsMavenUsername()
password getCredentialsMavenPassword()
}
url 'xxxxx'
}
/**
* Returns the credential username used by Maven repository
* Set this value in your ~/.gradle/gradle.properties with CREDENTIALS_USERNAME key
* #return
*/
def getCredentialsMavenUsername() {
return hasProperty('CREDENTIALS_USERNAME') ? CREDENTIALS_USERNAME : ""
}
/**
* Returns the credential password used by Maven repository
* Set this value in your ~/.gradle/gradle.properties with CREDENTIALS_PASSWORD key
* #return
*/
def getCredentialsMavenPassword() {
return hasProperty('CREDENTIALS_PASSWORD') ? CREDENTIALS_PASSWORD : ""
}
If the user hasn't the credentials the script doesn't fail.
Not sure if that answers your question, but you can put this in the gradle.properties file:
nexusUrl=http://path/to/nexus
and do this in the build.gradle:
maven {
url project.property(nexusUrl)
}
EDIT:
regarding your credentials, all you should need is something like
if (project.hasProperty('uploaderUser') && project.hasProperty('uploaderPassword')) {
credentials {
username = project.property('uploaderUser')
password = project.property('uploaderPassword')
}
}
Solved this issue by replacing jcenter() in Project/andoird/build.gradle with maven { url 'http://nexusUrl' } under buildscript and allprojects:
buildscript {
repositories {
google()
maven { url 'http://nexusUrl' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}
allprojects {
repositories {
google()
maven { url 'http://nexusUrl' }
}
}
and in fluttersdk/packages/flutter_tools/gradle/flutter.gradle replaced jcenter with maven { url 'http://nexusUrl' } under buildscript:
buildscript {
repositories {
google()
maven { url 'nexusUrl' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}

Gradle global variable not in scope within buildscript

I have something like this in my top level build.gradle (Gradle 2.2)
ext.repo = "https://my-artifactory-repo"
buildscript {
repositories {
maven {
credentials {
username foo
password bar
}
url repo //doesn't work
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.1'
}
}
allprojects {
repositories {
maven {
credentials {
username foo
password bar
}
url repo //works
}
}
}
This is the error
Could not find property 'repo' on org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository_Decorated#718afa64.
So it works in allprojects but not buildscript.
You can define your variable as an extra property with ext in the buildscript {...}. This variable is then also accessible in the scope of allprojects {...}:
buildscript {
ext {
repo = "https://my-artifactory-repo"
}
repositories {
maven {
url repo // works
}
}
}
allprojects {
repositories {
maven {
url repo // works
}
}
}
This is happening because the buildscript {...} configuration closure is always evaluated first, so the property is not yet defined. A workaround would be to define the property outside of the build script, either by placing it in a gradle.properties file or via the command line.

Resources