Gradle Custom buildScriptRepository methods - gradle

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'

Related

Unable to apply plugin in gradle init script

Here is my gradle init script.
initscript {
repositories {
gradlePluginPortal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.5.1")
}
}
allprojects {
apply(plugin="java")
apply(plugin="application")
apply(plugin="org.springframework.boot.gradle.plugin.SpringBootPlugin")
repositories {
gradlePluginPortal()
mavenCentral()
}
}
However, I am getting Plugin with id 'org.springframework.boot.gradle.plugin.SpringBootPlugin' not found. error.
I have tried the solutions from the following questions but none of them solves the problem.
Add Android plugin to gradle init script
I have found my solution after googling around.
In order to apply plugins in gradle init script that is written in kotlin DSL,
one has to use the following
initscript {
repositories {
gradlePluginPortal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.5.1")
}
}
allprojects {
apply(plugin="java")
apply(plugin="application")
apply<org.springframework.boot.gradle.plugin.SpringBootPlugin>()
repositories {
gradlePluginPortal()
mavenCentral()
}
}
In order to apply third party plugins in the init script, one has to use the form apply<path.to.classname>()

Cannot define anything in publishing block of build.gradle

I'm trying to create a library and publish it to maven local. When I start trying to add a MavenPublication to publications, the IDE gives it a dotted underline like it's an unresolved reference. Same with parameters like from and artifact.
If I uncomment the pom block, gradle sync fails and says Cause: invalid type code: B3, ultimately because of org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException on one of the parameters within.
This build.gradle file is in one of my modules. The top level build.gradle is pretty simple.
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
jcenter()
}
dependencies {
}
}
allprojects {
apply plugin: "idea"
group = 'com.mygroup'
version = '1.0.0'
repositories {
mavenLocal()
mavenCentral()
jcenter()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
And here's the module's build.gradle.
plugins {
id 'java-library'
id 'maven-publish'
id 'signing'
}
task sourcesJar(type: Jar) {
from sourceSets.main.allJava
archiveClassifier = 'sources'
}
task javadocJar(type: Jar) {
from javadoc
archiveClassifier = 'javadoc'
}
publishing {
publications {
mavenJava(MavenPublication) {
artifactId = 'mylibrary'
from components.java
artifact sourcesJar
artifact javadocJar
pom {
name = 'MyLibrary'
organization {
name 'MyOrg'
url 'www.myurl.com'
}
}
}
}
}
signing {
sign publishing.publications.mavenJava
}
javadoc {
if(JavaVersion.current().isJava9Compatible()) {
options.addBooleanOption('html5', true)
}
}
I'm using Gradle 5.1.1, IntelliJ Idea 2019.1.2, and JDK 12.0.1.
As it's a bit long explanation, let me clarify the situation here instead of replying in the comments to my question about providing the complete example with a pom-offender block. As the OP has already figured out, the problem is in the missing = sign. It's necessary for pom.organization and must not be present in the plugin.id.
For plugins block, it's a well-known Groovy convention of method call, which allow us to omit parentheses. And you can't use assignment here because id is not a property of a class. Look at the Gradle source code for plugins block.
However, if we look at pom.organization block we see the receiver of Action is MavenPomOrganization which has getName() and getUrl() returning Property<String> and not taking any arguments. From bare Groovy standpoint, it's definitely not the case of omitting parentheses. But I don't know for sure how the assignment syntax (name = 'MyOrg') works here under the hood. At first glance I can just speculate that it's some sort of Gradle magic using Groovy magic, e.g. AST transformation or something

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.

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

Apply external Plugin to a subset of subprojects

I am using the Gradle SSH Plugin to deploy some, but not all subprojects to two different remote machines. I want to configure these remotes in the main build script to avoid duplication, but cannot apply the plugin as per this:
configure([
project(':Subproject1'), project(':Subproject5'), project(':Subproject7')
])
{
buildscript {
dependencies {
classpath 'org.hidetake:gradle-ssh-plugin:2.4.2'
}
}
apply plugin: "org.hidetake.ssh"
ssh.settings {
user = getProperty('ssh.username')
identity = file(getProperty('ssh.identity'))
knownHosts = allowAnyHosts
}
remotes {
tomcat {
host = getProperty('ssh.hosts.tomcat')
}
jboss {
host = getProperty('ssh.hosts.jboss')
}
}
}
Gradle fails with > Plugin with id 'org.hidetake.ssh' not found.
Everything is fine when the contents of the configuration closure are applied per project. How can I elegantly solve this issue?
Guessing here as I haven't reproduced your issue, but the buildscript block is special as it is pre-parsed before normal groovy parsing of the build files and I suspect that having it inside a project block like you have will not work.
Have you tried moving the buildscript block to the root level in the script above?
edit 1: old gradle forums post discussion this can be found here
edit 2: adding a snippet of code in response to a comment.
To remove duplication and only define say the repository references in one place, you could use the following pattern:
buildscript {
ext.RepositoryConfigurator = {
maven {
credentials.username artifactoryReader
credentials.password artifactoryReaderPwd
url artifactoryReaderUrl
}
jcenter()
mavenCentral()
}
ext.DependencyConfigurator = {
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:1.2"
}
ext.ResolutionConfigurator = {
resolutionStrategy.cacheChangingModulesFor 60, 'minutes'
}
repositories RepositoryConfigurator
dependencies DependencyConfigurator
configurations.classpath ResolutionConfigurator
}
apply plugin: "some.plugin.requiring.above.classpath"
gradle.rootProject {
buildscript {
repositories RepositoryConfigurator
dependencies DependencyConfigurator
configurations.classpath ResolutionConfigurator
}
}
gradle.allprojects {
buildscript {
repositories RepositoryConfigurator
dependencies DependencyConfigurator
configurations.classpath ResolutionConfigurator
}
}
In other words: since the buildscript block is special and pre-parsed, you can not use things defined elsewhere in the buildscript block. Going in the other direction is ok though, i.e. you can define things in the buildscript block which are then visible elsewhere. We can use this to define the repository references once inside the buildscript block and then use that reference in other places in the build file.
The above is from a settings.gradle file so might or might not be a perfect fit, but should demonstrate the idea.

Resources