How to provide correct settings for gradle? - gradle

Using Jenkins for CI, I need to use hidden credentials for Gradle or maven publishing. My credentials are applied in Jenkins rather than in source code. Maven settings are in a settings.xml, but I would like to define properties in Jenkins. What would I use in a command-line to do this? I imagine
gradle -Dsomething.username=blah -Dsomething.password=secret

There may be a better way but this is how we do it: we define target maven repository in build.gradle using custom project properties and then provide those properties via command line:
uploadArchives {
repositories {
mavenDeployer {
repository(url: "https://your-repo-server.company.com/your-built-artifacts") {
authentication(userName: project.getProperties()['nexusUploadUsername'], password: project.getProperties()['nexusUploadPassword'])
}
}
}
}
gradle -PnexusUploadUsername=blah -PnexusUploadPassword=secret
You can also take one step further and configure them as Jenkins build parameters or environment variables in order to prevent people from seeing them in logs (do not forget to set that 'Mask passwords' checkbox): gradle -PnexusUploadUsername=$JENKINS_NEXUS_USER -PnexusUploadPassword=$JENKINS_NEXUS_PASSWORD.

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'
}
}
}

Setting up Artifactory in gradle global

currently I have a project which is deploying artifacts in our artifactory. For that Project everything is setted up perfect and it works.
I just wonderd if there is a possibility to set up the whole artifactory configuration global in gradle, so that I don't have to write the artifactory {...} stuff for each project.
You can maintain a file lets say build_dependency.gradle and define the task for all project
allprojects
{
//task common for all the project
}
subprojects
{
//task for subprojects
}
or specify the type of project e.g ext.warProject = 1 in dependency file and refer it in build_dependency.gradle as
if(project.hasProperty('warProject '))
{
//task here
}
and use this file in build.gradle like apply from: "$rootDir/path_to_file/build_dependency.gradle"
"$rootDir/path_to_dependenccy_file"`
You could simply write your own Gradle plugin that would be responsible for:
applying the artifactory plugin and other related plugin(s) like maven-publish
provide default values for the artifactory extension properties , like contextUrl, repoKey, credentials, etc...
Then your different projects will just have to apply your custom plugin, and provide only the project-specific configuration (configuration of the artefact to be published, for example, in publishing extension)
EDIT there are other ways to implement that, but it depends on what you mean by "global in gradle":
global to your own computer? then you could create a User InitScript that would contain the artifactory plugin configuration part
global to your team/company ? then you could need to implement a custom plugin, and maybe include this plugin into a custom gradle wrapper distribution (see example here
EDIT2 If you just want to set the artifactory plugin configuration of different sub-project of a same multi-project build, then the simpliest solution would be to define this configuration in the subprojects block of the root project build script:
subprojects {
apply plugin: "com.jfrog.artifactory"
artifactory {
publish {
contextUrl = '<repo url>'
repository {
repoKey = "<repo name>"
username = "user"
password = "pass"
}
}
}
}

Gradle - globally define plugins repository proxy-cache for all projects

I need to globally set the plugin repositories for all Gradle projects. There is a way of doing that by adding the below section in settings.gradle file:
pluginManagement {
repositories {
maven {
url 'https://artifactory.mycompany.org/artifactory/gradle-plugins/'
credentials {
username = 'some-user'
password = 'some-password'
}
}
}
}
However, when I put this file in ~/.gradle/settings.gradle, it doesn't seem to be working. I don't want to add such settings.gradle file in each project due to corporate reasons.
Is there a way of telling Gradle globally where to fetch all of the plugins?

Add dynamic dependencies via command line

Is it possible to use CLI to add a jar with Gradle plugin (or task) to the build classpath without modifying build.gradle? Can the add JAR be resolved from Maven repository?
Is buildscript { dependencies { classpath }} controllable from CLI? And can I use CLI to make Gradle to resolve the JAR from Maven?
Basically I need to achieve the same situation as with Maven, which allows invoking any plugin by
mvn <plugin-group-id>:<plugin-artifact-id>:<plugin-version>:<plugin-goal>
I'm writing a pair of Maven and Gradle plugins to extract information about projects and their dependencies into JSON file, which can be later processed programatically. The idea is to be able to apply it on a large number of OSS projects and, therefore, without modifying them.
I think I get it now
myinit-script.gradle
if (hasProperty('extraDependencies')) {
def extraDeps = property('extraDependencies').split(',')
allprojects {
buildscript {
dependencies {
classpath extraDeps
}
}
}
}
command line
gradlew --init-script myinit-script.gradle -PextraDependencies=org.foo:bar:1.0,org.foo:baz:1.2 build

How do I specify nexus credentials with `gradle init`?

When running gradle init to facilitate migration from a maven pom to a gradle build, is there a way to specify credentials for a repository that requires them for read-only access (our local nexus repo requires auth even for reads)?
Usually it fails because our parent pom would require read-only authentication to pull down from the local repo, but it doesn't seem like you can specify these credentials until after the build.gradle file already exists, which is counter-intuitive to the gradle init purpose.
Adding them via ~/.gradle/.gradle.properties or through various ~/.gradle/init.gradle methods doesn't seem to work.
What does your ~/.gradle/init.gradle look like? Something like this should work:
allprojects {
repositories {
mavenLocal()
maven {
url 'https://your.nexus/server'
credentials {
username "your_username"
password "your_password"
}
}
}
}

Resources