Gradle plugin with custom group id - gradle

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.

Related

Gradle 6.5 issue with plugin “com.gradle:gradle-enterprise-gradle-plugin:3.3.3

I am really annoyed with com.gradle.enterprise plugin as the id and groupid different
I finally tried to redirect using
pluginManagement {
repositories {
maven { url "https://artifactory.XXX/artifactory/public/" }
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == "com.gradle.enterprise") {
useModule("com.gradle:gradle-enterprise-gradle-plugin:3.3.3")
}
}
}
}
plugins {
id "com.gradle.enterprise" version "3.3.3"
}
Now all I am getting the error is
Plugin not found in org.gradle instead of searching in com.gradle
Exception is:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: ‘com.gradle.enterprise’, version: ‘3.3.3’, artifact: ‘com.gradle:gradle-enterprise-gradle-plugin:3.3.3’] 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.gradle:gradle-enterprise-gradle-plugin:3.3.3’)

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

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).

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"

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

How can I force a gradle build to re-download a SNAPSHOT plugin from Maven repository on every build

As shown below, I have a gradle plugin that is built deployed as a SNAPSHOT version in a maven repository.
I am aware that -SNAPSHOT dependencies should automatically be recognised as changing=true
I also know that the resolutionStrategy should allow forcing a new download for all configurations.
However, in this context, the setting has no effect.
Question: Can you provide a corrected sample, based on the one below, that forces a re-download on every run, or explain how such a configuration can be achieved?
buildscript {
repositories { maven { url "http://moomoomoo.com:8081/artifactory/moomoomoo-plugins" } }
dependencies { classpath 'com.moomoomoo:moomootools:1.3-SNAPSHOT' }
}
configurations.all { resolutionStrategy.cacheChangingModulesFor 0, 'seconds' }
apply plugin: 'com.moomoomoo.moomootool'
To configure the resolution strategy of the classpath configuration for the build script you have to configure it inside the buildscript block:
buildscript {
repositories { maven { url "http://moomoomoo.com:8081/artifactory/moomoomoo-plugins" } }
configurations.all { resolutionStrategy.cacheChangingModulesFor 0, 'seconds' }
dependencies { classpath 'com.moomoomoo:moomootools:1.3-SNAPSHOT' }
}
apply plugin: 'com.moomoomoo.moomootool'

Resources