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

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

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"

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
}

Kotlin Quasar example not working

I am testing the Kotlin Quasar actor example.
Quasar and Kotlin – a Powerful Match
So the question is, is this example out of date and is there any documentation in which I can find out how to use Kotlin and Quasar?
This is my gradle.build file.
group 'no.inmeta.kotlin.akka'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.0.1'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "co.paralleluniverse:quasar-kotlin:0.7.4"
testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
}
I'm part of the Quasar team.
The post cites Quasar tests which you can run by cloning the Quasar repo and running e.g. gradle :quasar-kotlin:build (requires Gradle installed) but for new projects/experiments I suggest to start instead from the Gradle template, kotlin branch which now uses the latest Kotlin 1.0.1-2 (and for simplicity the latest Quasar 0.7.5-SNAPSHOT that depends on it).
Starting from that template I built this project (more info about how to configure it and run it in the main README) that runs the same Quasar actor tests as normal programs rather than tests. Here's its build.gradle:
group 'no.inmeta.kotlin.akka'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlinVer = '1.0.1-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVer"
}
}
apply plugin: 'kotlin'
apply plugin: 'application'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceCompatibility = 1.8 // 1.7
targetCompatibility = 1.8 // 1.7
configurations {
quasar
}
configurations.all {
resolutionStrategy {
failOnVersionConflict()
}
}
repositories {
// mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/releases" }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
// maven { url 'https://maven.java.net/content/repositories/snapshots' }
}
ext.classifier = ':jdk8' // ':'
ext.quasarVer = '0.7.5-SNAPSHOT'
dependencies {
compile "co.paralleluniverse:quasar-core:${quasarVer}${classifier}"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVer"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlinVer"
compile "co.paralleluniverse:quasar-kotlin:${quasarVer}"
quasar "co.paralleluniverse:quasar-core:${quasarVer}${classifier}#jar"
}
applicationDefaultJvmArgs = [
"-Dco.paralleluniverse.fibers.verifyInstrumentation=true",
"-Dco.paralleluniverse.fibers.detectRunawayFibers=false",
"-javaagent:${configurations.quasar.singleFile}" // =v, =d
]
// mainClassName = 'co.paralleluniverse.kotlin.actors1.PingPongKt'
mainClassName = 'co.paralleluniverse.kotlin.actors2.PingPongWithDeferKt'
task wrapper(type: Wrapper) {
gradleVersion = '2.12'
}
defaultTasks 'run'
Some notes about the differences with your build file:
Since I converted the tests to programs, I'm including the application plugin and its configuration (here, applicationDefaultJvmArgs and mainClassName) as well as setting the default Gradle task to run.
In addition to the above, a gradle wrapper has been generated and pushed so that ./gradlew is all you need on the command line, with no need to have a local Gradle installation (how to run it in an IDE depends on the IDE).
You need to run the Quasar agent (or AoT instrumentation but using the agent here) so there's a quasar configuration pointing to the artifact that is then used to pass the -javaagent:${configurations.quasar.singleFile} JVM argument.
Using Java 8 as Quasar has a specific optimized build for it.
Also note that there is now a 1.0 branch of the quasar-kotlin-jetbrains-webinar project (which is now the HEAD one in fact), which contains the companion source code of this guest webinar with IntelliJ, ported to the latest Kotlin and Quasar as well.
Let me know if this helps.

How to get Gradle Spring dependency plugin to work?

Because I'm such a newb at this, the problem is context. Their example doesn't match my build. I've installed the jar locally so it should definitely be available.
My project looks like:
allprojects {
apply plugin: 'maven'
apply plugin: "io.spring.dependency-management"
group = 'com.wnp'
version = '6.5.0-SNAPSHOT'
}
subprojects {
apply plugin: 'java'
sourceCompatibility = System.getProperty("java.version")
targetCompatibility = System.getProperty("java.version")
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
dependencyManagement {
imports {
mavenBom 'org.springframework.ws:spring-ws:2.1.4.RELEASE'
mavenBom 'org.jboss.as:jboss-as-jms-client-bom:7.5.0.Final-redhat-21'
}
dependencies {
dependency "antlr:antlr:2.7.7"
}
}
apply plugin: "io.spring.dependency-management"
Their project looks like:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE"
}
}
apply plugin: "io.spring.dependency-management"
What I'm getting from the system is :
Caused by: org.gradle.api.plugins.UnknownPluginException: Plugin with
id 'io.spring.dependency-management' not found.
Clues? What does "buildscript" represent? Is that a task? I've tried putting the "dependencies" section just about everywhere. Same goes with the "apply plugin:" line. Is my dependencyManagement section in the right place? One of the things I keep seeing is "dependencies cannot be applied to closure" which is pretty unhelpful as far as error messages go.
What you need to do is to define a dependency for build.gradle itself that has the plugin you're looking for. It may be e.g.:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'io.spring.gradle:dependency-management-plugin:0.5.3.RELEASE'
}
}
allprojects {
apply plugin: 'maven'
apply plugin: "io.spring.dependency-management"
group = 'com.wnp'
version = '6.5.0-SNAPSHOT'
}
Take a look at the official user guide, section called "external dependencies". According to it:
If your build script needs to use external libraries, you can add them to the script's classpath in the build script itself. You do this using the buildscript() method, passing in a closure which declares the build script classpath.
In your case,a plugin is an external dependency, and you have to provide buildscript section for all the project, which need to apply this plugin.

Resources