Add specific plugin version in Gradle using 'Apply' - gradle

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"

Related

new gradle format confusion and migrating causes Could not find method testCompile()

I have a build.gradle with
plugins {
id {some plugin for all projects}
id "com.diffplug.spotless" version "5.1.1"
}
AND THEN I have an allprojects {} section that defines ONE apply plugin: 'jacoco' and a subprojects {} section that declares apply plugin: 'java' with a few others
Immediately adding spotless messed with stuff and errors out that it cannot find the java plugin so then I modify ALL plugins to be in the plugins section like so
plugins {
id "java"
id "checkstyle"
id "eclipse"
id "idea"
id "jacoco"
id "com.diffplug.spotless" version "5.1.1"
id "com.dorongold.task-tree" version "1.5" //This prints out a task tree (pretty print)
}
This then results in this error
Could not find method testCompile() for arguments [junit:junit:4.11] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
So for some reason the java plugin is lost. I can't figure out the right combination here to migrate everything to this new plugins section format.
How do I do that? I randomly tried putting a plugins section in allprojects and subprojects but that results in this new error
Could not find method plugins() for arguments [build_d8c2jgy4ua1m0vkv9kmvgefmc$_run_closure2$_closure5#62337fda] on root project 'providersvc-all' of type org.gradle.api.Project
How does this new plugins section work? I can't seem to migrate without it breaking everything. I just want java plugin, testCompile, and spotless to play nicely together right now
EDIT(forgot to attach the full trimmed down file that does not work):
plugins {
id "java"
id "com.diffplug.spotless" version "5.1.1"
}
ext {
//dependency versions every project usees so we version in one location all jars(less jar hell this way)
deps = [
'junit': 'junit:junit:4.11'
]
}
allprojects {
repositories {
jcenter()
mavenCentral()
maven {
//webpieces VERSIONED snapshots so you can lock on a snapshot
url "https://dl.bintray.com/deanhiller/maven"
}
//For testing locally
maven {
url uri('/tmp/myRepo/')
}
}
}
subprojects {
dependencies {
testCompile deps['junit']
}
}
thanks,
Dean
You are only applying the plugins to the root project - not the sub-projects. However, if you like to configure plugins through the subprojects configuration, you have to use the apply plugin syntax. But you don't have to use the old buildscript block for configuring the classpath and repositories if you a combination of the two.
Here is an example. I am assuming the root project is not a Java project. I have also removed your comments and inserted mine instead for the sole reason to make them easier to spot.
plugins {
id "com.diffplug.spotless" version "5.1.1" apply false // <-- Set "apply false" here
// This makes it configure which version to use on the classpath for the entire build, but without applying it.
// Notice that the Java plugin is not specified here anymore.
// This is because it is a core plugin so you can't set the version (and I am assuming you don't want it on in the root project).
}
ext {
deps = [
'junit': 'junit:junit:4.11'
]
}
allprojects {
repositories {
jcenter()
mavenCentral() // <-- You can remove this if you want as it is already present as a proxy in jcenter().
maven {
url "https://dl.bintray.com/deanhiller/maven"
}
maven {
url uri('/tmp/myRepo/')
}
}
}
subprojects {
// Here are the two plugins
apply plugin: "java"
apply plugin: "com.diffplug.spotless"
dependencies {
testImplementation deps['junit'] // <-- testCompile renamed to testImplementation as the former has been deprecated for a long time
}
}

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

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.

adding errorprone to gradle build file

I am trying to add errorprone to my gradle build file as follows:
relevant parts of build.gradle
buildscript {
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.9"
}
}
plugins {
id 'java'
id 'application'
id 'idea'
id 'com.github.johnrengelman.shadow' version '1.2.4'
id "net.ltgt.errorprone" version "0.0.9"
}
dependencies {
classpath 'net.ltgt.gradle:gradle-errorprone-plugin:0.0.9'
}
configurations.errorprone {
resolutionStrategy.force 'com.google.errorprone:error_prone_core:2.0.19'
}
error from classpath line when I run gradle clean
Could not find method classpath() for arguments [net.ltgt.gradle:gradle-errorprone-plugin:0.0.9] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Any thoughts on what is causing this issue and what might help resolve this? Thanks
I got errorprone to work with gradle by making the following changes:
replaced this from dependencies:
classpath net.ltgt.gradle:gradle-errorprone-plugin:0.0.9'
with
errorprone "com.google.errorprone:error_prone_core:latest.release"
and removed the configurations line:
configurations.errorprone {
resolutionStrategy.force 'com.google.errorprone:error_prone_core:2.0.19'
}
This worked.

Control the gradle task execute order

I have a strange problem about gradle task recently.
Assume I have a simple gradle config as follows
apply plugin: "java"
apply plugin: "maven"
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.diffplug.gradle.spotless:spotless:2.0.0"
}
}
apply plugin: "com.diffplug.gradle.spotless"
spotless {
java {
eclipseFormatFile 'format.xml' // XML file dumped out by the Eclipse formatter
}
}
spotlessJavaCheck.dependsOn(processResources)
version = '1.0-SNAPSHOT'
I just want to set the depends on relationship for the spotless check. After I run a build, the error looks like this
> Could not find property 'spotlessJavaCheck' on root project 'gradle-helloworld'.
I have done something similar with other plugins, it works well, but not for this spotless plugin.
Br,
Tim
Spotless Gradle plugin does magic at configuration time.
You need to set the dependency after evaluation time, once the magic is done:
afterEvaluate {
tasks['spotlessJavaCheck'].dependsOn processResources
}

Resources