Gradle: How can I configure repositories in buildscript block of external script - gradle

In external script common/buildversion.gradle I have:
buildscript {
// Copy repositories definitions from this buildscript to all projects
(allprojects*.repositories + [repositories]).each {
it.configure {
apply from: rootProject.file('../../common/repositories.gradle')
}
}
dependencies { classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:1.2" }
}
apply plugin: org.sonarqube.gradle.SonarQubePlugin
In common/repositories.gradle:
repositories{
maven { url "https://plugins.gradle.org/m2/" }
}
I am getting error:
Cannot resolve external dependency
sonarqube-gradle-plugin because no repositories are
defined.

Here is a way to have a single source for repositories definitions.
declare the repositories as an "ext" variables
// repositories.gradle
ext.repos = {
maven {
name "repo1"
url "repo1_url"
}
maven {
name "repo2"
url "repo2_url"
}
}
"apply" in your build.gradle
// build.gradle
apply from: "${project.projectDir}/repositories.gradle"
use variable in repositories declaration
// build.gradle
repositories repos

Externalizing sections of the buildScript block into other scripts is not supported. There is an open defect, you should vote on it.

Related

Add dependency and plugin in seperate file using "apply from" in gradle

My build.gradle inside app directory contains:
apply plugin: 'com.android.application'
buildscript {
repositories {
mavenCentral()
jcenter()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
}
}
allprojects {
repositories {
mavenCentral();
jcenter()
}
}
def hasBuildExtras = file('build-extras.gradle').exists()
if (hasBuildExtras) {
apply from: 'build-extras.gradle'
}
And this is my build-extras.gradle file:
buildscript {
repositories {
mavenCentral()
jcenter()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath 'com.github.triplet.gradle:play-publisher:1.2.2'
}
}
apply plugin: 'com.github.triplet.play'
Am I correct to assume that build-extras.gradle should be "merged" inside original file? No matter how I move it around I get Error:Plugin with id 'com.github.triplet.play' not found.
If i move classpath 'com.github.triplet.gradle:play-publisher:1.2.2' up to the main file and leave just apply plugin: 'com.github.triplet.play' inside build-extras.gradle then it seems to work fine. So am I wrongly defining dependencies?
A build.gradle file can only ever have one buildscript block. You are correct with the paragraph explanation. Add the plugin dependency to your main build file's dependencies block within the buildscript, then conditionally apply the plugin using whatever logic you'd like.

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/"
}
}
}
}

How to prioritize mavenLocal over artifactory repo in gradle?

I have multi-modules project and I'm using artifactory for resolving custom libraries:
build.gradle of parent project:
...
subporjects {
...
apply plugin: "com.jfrog.artifactory"
artifactory {
resolve {
contextUrl = ext.getProperty('ARTIFACTORY_URL')
repoKey = ext.getProperty('ARTIFACTORY_REPO_NAME')
username = ext.getProperty('ARTIFACTORY_USERNAME')
password = ext.getProperty('ARTIFACTORY_PASSWORD')
}
}
}
It works as expected my library is published to artifactory with gradle artifactoryPublish and then it's fetched from there. But in some cases I want to fetch my custom library from mavenLocal() repo. I have next subproject build.gradle
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile 'my-custom-library'
}
But as I can see it is still resolves from artifactory. Can I somehow prioritize mavenLocal() over it ?
The repository priority will be the order in which they were added to the RepositoryHandler
I'm guessing that the artifactory repository is added when the plugin is applied so you could delay this by
afterEvaluate {
subprojects {
apply plugin: "com.jfrog.artifactory"
// etc
}
}
Or maybe
evaluationDependsOnChildren()
subprojects {
apply plugin: "com.jfrog.artifactory"
// etc
}
If all you want to do is to depend on one subproject from within another, you should declare dependencies using the project notation:
dependencies {
compile project(':shared')
}
https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:project_jar_dependencies

Import buildscripts in gradle

I am pretty new with gradle, I want to include a plugin globally, and I don't know how.
In my project(wish I DO NOT OWN), there are multiple build.gradle, and there is a commons.gradle folder. In there is created a script like this
myscript.gradle
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.nl.javadude.gradle.plugins:license-gradle-plugin:0.13.1"
}
}
Then in of the build.gradle I have this:
buildscript {
repositories {
mavenCentral()
maven {
url 'https://plugins.gradle.org/m2/'
}
}
apply from: "$path-to/myscript.gradle", to: buildscript
}
apply plugin: "com.github.hierynomus.license"
but I keep getting a Plugin with id com.github.hierynomus.license" not found. I am using Gradle 3.1. any help or hint would greatly help.
Thanks
I was able to make this work by doing the following:
myScript.gradle
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.nl.javadude.gradle.plugins:license-gradle-plugin:0.13.1"
}
}
then in the build.gradle
buildscript {
repositories {
mavenCentral()
maven {
url 'https://plugins.gradle.org/m2/'
}
}
apply from: "$path-to/myScript.gradle", to: buildscript
}
plugins{id "com.github.hierynomus.license" version "0.13.1"}
apply plugin: "com.github.hierynomus.license"
I was not able to find anything online, so I hope this will help someone one day.
ps: Sorry for my bad English

Gradle Custom buildScriptRepository methods

I would like to make a custom buildScript repository method so I can easily reference our internal maven repo. Right now I'm required to declare a maven block everywhere we use our plugin.
Here is the current setup
buildscript {
repositories {
jcenter()
maven { url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
}
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
What I would like to do is something like this
buildscript {
repositories {
jcenter()
myReleaseRepo()
}
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
How can I make a method available to create a repository anywhere we may use the plugin in the future?
Another solution is to add custom methods on RepositoryHandler using some Groovy goodness. Just chuck this in ~/.gradle/init.gradle
RepositoryHandler.metaClass.myReleaseRepo = {
delegate.maven {
name 'myReleaseRepo'
url 'http://myNexus:8081/nexus/content/repositories/My-Release'
}
}
After that, you can use it just as you described:
buildscript {
repositories {
myReleaseRepo()
}
}
Metaclasses in Groovy are just great. The delegate in this case is pretty much like the javascript this. This code is essentially using the RepositoryHandler instance (delegate keyword) and just calling repositoryHandlerInstance.maven(name, url).
It is possible to add a repo to an init script which would then apply to all gradle invocations that use the init script - without having to individually declare your maven repo in each build.gradle.
Solution 1:
Partial solution, does not do exactly what you're asking for. In init.gradle:
allprojects{
buildscript{
repositories{
maven{ url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
}
}
}
Then your build.gradle can skip buildscript repo declaration entirely:
buildscript {
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
Solution 2:
In fact, you can even move your buildscript classpath declaration to init and have the plugin apply to all projects that use the init script:
beefier init.gradle
allprojects{
buildscript{
repositories{
maven{ url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
}
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
}
gives you a lighter build.gradle
apply plugin: 'my-plugin'
I tried to, but apparently you cannot move the apply line to init.gradle as well. see this defect.
Solution 3:
I retract what I said in the comment above, I figured out how to do exactly what you're asking for. Apparently you can create extensions for the buildscript block using the initscript. However I still prefer solution2, because it gives you a cleaner build.gradle.
To create a buildscript extension, in your init.gradle:
class customRepos {
def buildscript
customRepos(buildscript) {
this.buildscript = buildscript
}
void addMyRepo() {
buildscript.repositories {
maven{ url 'http://myNexus:8081/nexus/content/repositories/My-Release' }
}
}
}
allprojects {
extensions.create('myRepo', customRepos, buildscript)
}
which then allows you to do this in your build.gradle
buildscript{
myRepo.addMyRepo()
dependencies {
classpath 'com.example.plugin:my-plugin:1+'
}
}
apply plugin: 'my-plugin'

Resources