is it possible put the repositories configuration in {gradle_home_user}/gradle.properties instead in {project}/build.gradle?
Something like this:
build.gradle
repositories {
maven {
url "maven_url"
credentials {
username = "user"
password = "password"
}
}
}
gradle.properties
repositories.maven.url=maven_url
repositories.maven.credentials.username=user
repositories.maven.credentials.password =password
Yes it is possible. But the property names with dots will need to be accessed using the following notation ${project["my.prop.name"]}. Instead I would recommend using underscores for property separators instead. These can be accessed simply by using ${my_prop_name}.
build.gradle
repositories {
maven {
url "${repositories_maven_url}"
credentials {
username = "${repositories_maven_credentials_username}"
password = "${repositories_maven_credentials_password}"
}
}
}
gradle.properties
repositories_maven_url=maven_url
repositories_maven_credentials_username=user
repositories_maven_credentials_password=password
Related
After the recent security fixes in Gradle 5.6.2+, we're unable to use global plugin management in init.gradle script.
The section, which is described in the Gradle documentation does not provide any help about using Nexus server requiring authentication.
The workaround I've found is following: I had to manually hardcode credentials inside of the script even though the nexusUsername and nexusPassword is already defined in gradle.properties.
allprojects {
repositories {
mavenLocal()
maven {
url "https://nexus-repo-requiring-auth/"
credentials {
username nexusUsername
password nexusPassword
}
}
}
settingsEvaluated { settings ->
settings.pluginManagement.repositories {
//This is a workaround, because the global properties are not available here
def localNexusUsername = "nexusUser1"
def localNexusPassword = "nexusPass1"
maven {
url "https://nexus-repo-requiring-auth/"
credentials {
username localNexusUsername
password localNexusPassword
}
}
}
}
Is there a way, how to read global variables inside of the settingsEvaluated block? Or any other way to define the plugin repository?
It's not a solution, but a different work-around. I found that if I defined my plugin repositories in the settings.gradle file it was able to read the variables:
pluginManagement {
repositories {
maven {
authentication {
basic(BasicAuthentication)
}
url "https://artifactory.redacted.com/gradle-plugins-mirror/"
credentials {
username "$artifactory_user"
password "$artifactory_password"
}
}
}
}
This worked better for me as we add the ~/.gradle/init.gradle file for each user, so everyone uses the same internal mirors.
I am trying to setup a gradle 5 project which uses a custom plugin someone developed and is available in my company internal repository.
Until now, we have been importing it using the following approach:
buildscript {
ext {
usr = System.env.usr != null ? System.env.usr : project.ext.properties.usr
pass = System.env.pass != null ? System.env.pass : project.ext.properties.pass
privateRepo = {
name "privateRepo"
url <url>
credentials {
username usr
password pass
}
}
}
repositories {
maven (privateRepo)
}
dependencies {
// various dependencies
classpath "org.something:some-plugin:1.0"
...
}
apply plugin: "someplugin"
Just like this question.
As far as I understood, this is a deprecated approach, so I would like to use the pluginManagement block gradle.settings (again, as suggested in this answer)
The problem is that my repository is private, so I would need to define the user and pass variables.
I tried similar approached inside the pluginManagement block in gradle.settings, but I could not get it to work: the pluginManagement does not support the ext block and the pluginManagement has to be the first block in the script, limiting my alternatives.
Is there anyway I can define the variables so it can be used in the pluginManagement block?
Disclaimer: my question is not a duplicated of the linked one because my problem is related to the credentials part, that I am having issues defining the variables and values.
Well I just added the def inside the block, not ideal but at least works:
pluginManagement {
def artifactoryUrl = "www.my.artifactory.url"
def artifactoryUser= "myUser"
def artifactoryPass= "myPass"
repositories {
maven {
name = "A-MavenGoogle"
url = uri(
"https://$artifactoryUrl/list/maven-google-remote/"
)
credentials {
username = artifactoryUser
password = artifactoryPass
}
}
maven {
name = "A-MavenCentral"
url = uri(
"https://$artifactoryUrl/list/maven-central-remote/"
)
credentials {
username = artifactoryUser
password = artifactoryPass
}
}
}
}
I am using gradle v3.4 and have populated properties from a secrets.properties file (passed into project.ext) but when I use the variables in the credentials section, I get an error from nexus complianing about authentication issues which makes me believe the string interpolation is not working correctly. I can print the variable value just before the credentials section.
build.gradle
maven {
credentials {
println(project.nexusUsername) //prints the value
username '${project.nexusUsername}'
password '${project.nexusPassword}'
}
if (project.version.endsWith("-SNAPSHOT")) {
url "http://nexus.somewhere.com/repository/some-java-snapshot/"
} else {
url "http://nexus.somewhere.com/repository/some-java-release/"
}
}
Update
I updated the credentials section above to use double quotes (not single) but that did not solve the issue. Single quotes are String literals - if you need String interpolation, you need to use double quotes in groovy.
The issue was how the properties was specified in the external properties file. I was using double quotes for the String values in the properties file and that was resulting in authentication failures. Once I removed the double quotes from the external properties file, I was able to publish to nexus.
Incorrect external properties file setting
someUsername="someuser"
Correct external properties file setting
someUsername=someuser
build.gradle
publishing {
publications {
shadow(MavenPublication) {
from components.shadow
groupId project.group
artifactId project.artifactId
}
}
repositories {
maven {
credentials {
username project.someUsername
password project.somePassword
}
if (project.version.endsWith("-SNAPSHOT")) {
url project.someSnapshot
} else {
url project.someRelease
}
}
}
}
this works.
Single quotes denote a String literal without variable expansion;
Please use
username project.nexusUsername
password project.nexusPassword
Reference: http://docs.groovy-lang.org/latest/html/documentation/#_single_quoted_string
My private repo which is loaded from bintray used for all projects and needs credentials:
allprojects {
jcenter()
repositories {
maven {
url "http://myurl.bintray.com/sdk"
credentials {
username 'JohnDoe'
password 'somePassword'
}
}
}
}
What's the best way to hide them?
Is it possible without creating new instance of Properties?
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
One way to do it is to set properties in the user specific file $HOME/.gradle/gradle.properties:
thePassword=somePassword
and in the build.gradle file:
credentials {
username 'JohnDoe'
password thePassword
}
Repositories{
maven{
name='tomRepo'
url=''
credentials(PasswordCredentials)
}
}
run a command in the console like this
./gradlew build --refresh-dependecies -PtomsRepoUsername=aws -PtomRepoPassword=$TOMS_REPO_PASSWORD
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'
}
}