Gradle single-project pluginManagement block not working (Kotlin DSL) - gradle

I need to change a multi-project build to a single-project build, as there is and only ever will be one project in this repo. Currently, in settings.gradle, I have a custom plugin repo that currently uses a pluginManagement block with resolutionStrategy and my list of repo's:
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == 'com.meanwhileinhell.plugin') {
useModule('com.meanwhileinhell:gradle-plugin:1.0.0-SNAPSHOT')
}
}
}
repositories {
mavenLocal()
maven { url "https://repo.spring.io/milestone" }
maven { url "https://plugins.gradle.org/m2/" }
// Meanwhileinhell repo
maven {
url "s3://mvn.meanwhileinhell.com/releases"
credentials(AwsCredentials) {
accessKey s3_access_key
secretKey s3_access_secret
}
}
}
plugins {
...
...
}
}
However, deleting settings.gradle and moving this block into my build.gradle.kts (Kotlin DSL) seems to do nothing. I've tried wrapping in a
configurations {
all {
resolutionStrategy {
eachPlugin {
...
}
}
}
}
and also
settings {
pluginManagement {
resolutionStrategy {
eachPlugin {
...
}
}
}
}
I found a SO answer that used settingsEvaluated in order to get the settings object, but again this was a no go.
Currently my build.gradle.kts looks like this, without pulling any plugin in from my repo:
val springBootVersion: String by project
group = "com.meanwhileinhell.myapp"
version = "$version"
repositories {
mavenCentral()
mavenLocal()
maven ("https://repo.spring.io/snapshot")
maven ("https://repo.spring.io/milestone")
maven ("https://plugins.gradle.org/m2/")
maven {
url = uri("s3://mvn.meanwhileinhell.com/releases")
credentials(AwsCredentials::class) {
accessKey = (project.property("s3_access_key") as String)
secretKey = (project.property("s3_access_secret") as String)
}
}
}
plugins {
base
eclipse
idea
java
id("io.spring.dependency-management") version "1.0.9.RELEASE"
// Load but don't apply to root project
id("org.springframework.boot") version "1.5.14.RELEASE" apply false
}
dependencies {
...
}
Whenever I try to add a plugin id like id("com.meanwhileinhell.plugin.hell2java") version "1.0.0-SNAPSHOT" I get an error that looks like it isn't even looking in my S3 location:
* What went wrong:
Plugin [id: 'com.meanwhileinhell.plugin.hell2java', version: '1.0.0-SNAPSHOT'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.meanwhileinhell.plugin.hell2java:com.meanwhileinhell.plugin.hell2java.gradle.plugin:1.0.0-SNAPSHOT')
Searched in the following repositories:
Gradle Central Plugin Repository
Any help on this would be appreciated!
EDIT !!!! -----------------------
I've just found this in the Gradle docs:
https://docs.gradle.org/current/userguide/plugins.html#sec:plugin_management
The pluginManagement {} block may only appear in either the settings.gradle file....
Looks like I'm going down the wrong way entirely, so will look into the initialisation script route.

I think you may have missed something about the file structure.
In the Groovy DSL, you have the following files:
build.gradle
settings.gradle
init.gradle
In the Kotlin DSL, you have the same files but with the .kts extension:
build.gradle.kts
settings.gradle.kts
init.gradle.kts
The Kotlin DSL doesn't differ to the Groovy DSL in where to put things. pluginManagement need to go in to the settings file, so for Kotlin that would be settings.gradle.kts. If you are in doubt, look at the documentation. For almost all code examples, you can switch between Groovy and Kotlin DSL to see how to do it (and which files they are supposed go to into).

Related

Add specific plugin version in Gradle using 'Apply'

How to apply a specific plugin version using newer Gradle syntax? I would like to do something like this but this gives an error of unknown property 'version':
apply plugin: 'com.bmuschko.docker-remote-api', version: '2.0.3'
The new plugin syntax can be seen on the Gradle Plugins Repository page for the plugin you wish to apply: https://plugins.gradle.org/plugin/com.bmuschko.docker-remote-api
Using the plugins DSL:
plugins {
id "com.bmuschko.docker-remote-api" version "6.1.3"
}
Using legacy plugin application:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.bmuschko:gradle-docker-plugin:6.1.3"
}
}
apply plugin: "com.bmuschko.docker-remote-api"
In your build.gradle file, apply the plugin with a plugins block near the top of your script:
plugins {
id "com.bmuschko.docker-remote-api" version "2.0.3"
}
The syntax you have there is not new, it the legacy plugin application
To specify the version for the legacy way, you need to use the buildscript { } block:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.bmuschko:gradle-docker-plugin:2.0.3"
}
}
apply plugin: "com.bmuschko.docker-remote-api"

Gradle plugin with custom group id

Gradle 6.1
I am having difficulties to use the new plugin configuration mode in Gradle with a custom plugin coming from a custom repository.
buildscript {
repositories {
maven {
url = uri("https://custom")
}
mavenCentral()
jcenter()
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
}
plugins {
java
idea
id("com.custom.gradle.plugin.myplugin") version "1.1.0"
}
I get this error:
Plugin [id: 'com.custom.gradle.plugin.myplugin', version: '1.1.0'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.custom.gradle.plugin.myplugin:com.custom.gradle.plugin.myplugin:1.1.0')
Searched in the following repositories:
Gradle Central Plugin Repository
Gradle will use the plugin id as its group id.
It works if I use the old ways:
buildscript {
repositories {
maven {
url = uri("https://custom")
}
mavenCentral()
jcenter()
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("com.custom:com.custom.gradle.plugin.myplugin:1.1.0")
}
}
apply(plugin = "com.custom.gradle.plugin.myplugin")
Is there a way to specify the group id with the 'id' command? Or am I breaking the plugin definition's contract with that old plugin?
In order to use the newer/preferred plugins { } DSL, the custom plugin must publish a plugin marker artifact.
If the custom plugin is able to be modified, then I suggest updating to make use of the Java Gradle Plugin Development plugin which will create the marker for you.
If the plugin is not able to be updated, then you can still use the plugins { } block, but you'll need to manually resolve the plugin:
In the main build.gradle:
plugins {
id("com.custom.gradle.plugin.myplugin") version "1.1.0"
}
Then resolve the plugin manually in settings.gradle:
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == "com.custom.gradle.plugin.myplugin") {
useModule("com.custom:com.custom.gradle.plugin.myplugin:${requested.version}")
}
}
}
}
See Plugin Resolution Rules
for more details.

Gradle: manage dependency/plugin versions in multi-project builds

I was working with Maven before Gradle and Maven has such things like dependencyManagement and pluginManagement what allows to keep all versions "DRY" in one place with help of "properties".
Gradle supports project properties, so I can declare versions like:
buildscript {
ext.kotlin_version = '1.1.61'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
But this approach cannot be applied to plugins {} DSL. I cannot write something like this:
plugins {
id 'org.jetbrains.kotlin:kotlin-gradle-plugin' version $kotlin_version
}
As according to docs:
values must be literal (e.g. constant strings, not variables)
Is there a way to workaround this limitation?
The latest versions of Gradle allows you to define your versions in a property file, map that version in a pluginManagement block, and then omit the version from all downstream plugin blocks. The pluginManagement block does not have the only-constant restriction.
In fact, once you use this approach, it is a compile time error to even try to declare a version downstream.
After using this approach, there is a good chance you can completely omit your buildscript.
gradle.properties
kotlinVersion=1.3.50
settings.gradle
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == "org.jetbrains.kotlin.jvm") {
useVersion(kotlinVersion)
}
}
}
}
build.gradle
plugins {
id("org.jetbrains.kotlin.jvm")
}
If you are into kotlinscript, get your version from a delegate:
settings.gradle.kts
val kotlinVersion: String by settings
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == "org.jetbrains.kotlin.jvm") {
useVersion(kotlinVersion)
}
}
}
}
Of course, you do not need the properties file. You can just hard code the version in your gradle.settings. But by having the property, you can then declare dependencies to the stdlib using it.
I think you can put the ext closure in a separate properties.gradle, and then reference the properties twice in buildscript as well as project build (buildscript block is evaluated at the very beginning, before any other part of groovy script).
For example, in $projectRoot/gradle/properties.gradle as below:
ext {
kotlinVersion = '1.1.61' // NOTE, the naming convention is better with kotlinVersion instead of kotlin_version
}
And your $projectRoot/build.gradle would look like this:
buildscript {
apply from: "gradle/properties.gradle"
repositories {
jcenter()
}
dependencies {
println "In buildscript, kotlinVersion is $kotlinVersion"
}
}
apply plugin: 'java'
apply from: "gradle/properties.gradle"
repositories {
jcenter()
}
dependencies {
println "In project, kotlinVersion is $kotlinVersion"
}
When you run ./gradlew dependencies, it would show you the populated versions:
> Configure project :
In buildscript, kotlinVersion is 1.1.61
In project, kotlinVersion is 1.1.61

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.

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