Kotlin buildSrc failing on Gradle 4.10 due to missing dependency - gradle

When upgrading to Gradle 4.10, I faced the following error when attempting to compile:
Execution failed for task ':buildSrc:compileKotlin'.
> Could not resolve all files for configuration ':buildSrc:kotlinCompilerPluginClasspath'.
> Could not find org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.2.60.
Searched in the following locations: file:/Users/<user-name>/.gradle/caches/4.10/embedded-kotlin-repo-1.2.60-2/repo/org/jetbrains/kotlin/kotlin-scripting-compiler-embeddable/1.2.60/kotlin-scripting-compiler-embeddable-1.2.60.jar
Required by:
project :buildSrc
> Could not find org.jetbrains.kotlin:kotlin-sam-with-receiver:1.2.60.
Required by:
project :buildSrc
Note that I was using buildSrc as part of my Gradle compilation process.

This is due to a breaking change in Kotlin DSL 1.0:
The kotlin-dsl plugin now requires a repository to be declared
With Kotlin 1.2.60, the Kotlin Gradle Plugin driving the kotlin compiler requires extra dependencies that aren't required by Gradle Kotlin DSL scripts alone and aren't embedded into Gradle.
This can be fixed by adding a repository that contains Kotlin compiler dependencies on the project where the kotlin-dsl plugin is applied: repositories { jcenter() }

build.gradle.kts should contain
plugins {
`kotlin-dsl`
}
// Required since Gradle 4.10+.
repositories {
jcenter()
}

Related

Jacoco Report Aggregation Plugin - Spring Dependency Management

I’m doing a migration of aggregated code coverage from script Reporting code coverage with JaCoCo Sample to The JaCoCo Report Aggregation Plugin
This is a SpringBoot project and as it’s or it was a common practice, it uses Dependency Management Plugin.
After I run testCodeCoverageReport task to aggregate test reports I get following exceptions. I assume this is because I use BOM and dependency version constraint from within dependencyManagement instead of native Gradle dependency constraints.
Exception below:
Execution failed for task ':testCodeCoverageReport'.
Could not resolve all files for configuration ':allCodeCoverageReportClassDirectories'.
Could not find com.pizza.infrastructure:logging:.
Required by:
project : > project :pizza-server
Could not find com.pizza.infrastructure:multitenancy:.
Required by:
project : > project :pizza-server
Could not find com.pizza.infrastructure:rest:.
Required by:
project : > project :pizza-server
project : > project :pizza-server > project :pizza-rest
Could not find com.pizza.infrastructure:rest-test:.
Required by:
project : > project :pizza-server
Is there a solution, to be able to still rely on dependencyManagement plugin and also be able to use jacoco aggregation plugin?
Perhaps not ideal, but I have the following plugin block in my build.gradle.kts for my 'test-results' module to resolve spring related dependencies, by adding the last two gradle plugins
plugins {
id("jacoco-report-aggregation")
id("test-report-aggregation")
id("org.springframework.boot") version "2.6.6"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
}

How to access version property from BOM with Gradle?

I'm migrating a small multi-module project from Maven to Gradle, to evaluate and (hopefully) to learn something.
subprojects {
group = "com.company.project-name"
apply plugin: "java"
repositories {
mavenCentral()
}
dependencies {
implementation platform("org.springframework.boot:spring-boot-dependencies:2.5.2")
compileOnly("org.projectlombok:lombok")
annotationProcessor("org.projectlombok:lombok")
}
}
This is what I have so far. Despite a small problem it's already working. As you can see, I'm importing org.springframework.boot:spring-boot-dependencies as platform dependency / BOM (Bill of Materials). As far as I understand this is equivalent to a Maven parent. spring-boot-dependencies already defines almost everything I'm using in this project. However, I'm also using Lombok. Luckily Spring takes care of that too.
This work as expected for the compileOnly dependency org.projectlombok:lombok. The problem is the annotationProcessor. Compiling the project results in an error:
[...]
Execution failed for task [...].
> Could not resolve all files for configuration [...].
> Could not find org.projectlombok:lombok:.
Required by:
project [...]
[...]
If I change it to something like this:
annotationProcessor("org.projectlombok:lombok:1.18.20")
... it works. But I want Gradle to use the version definition for Lombok from spring-boot-dependencies, because I don't want to define it again myself. With Maven I could just use the Maven property lombok.version. So I also tried something like this:
annotationProcessor("org.projectlombok:lombok:${lombok.version}")
Unfortunately, this result in another error:
A problem occurred evaluating root project [...].
> Could not get unknown property 'lombok' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
What is the Gradle equivalent to access a defined version property from a platform dependency / BOM?
Have you tried this:
annotationProcessor platform("org.springframework.boot:spring-boot-dependencies:2.5.2")
Make sure the annotationProcessor is on the top of the dependencies.
dependencies {
annotationProcessor platform("org.springframework.boot:spring-boot-dependencies:2.5.2")
annotationProcessor("org.projectlombok:lombok")
implementation platform("org.springframework.boot:spring-boot-dependencies:2.5.2")
compileOnly("org.projectlombok:lombok")
}

Can't build kotlin files using gradle kotlin DSL in Intellij idea

I'm trying to set up a kotlin project with gradle kotlin DSL as build system in IntelliJ idea,but I'm getting below error when try to run buil.gradle.kts file. I have tried with different kotlin compiler version but no luck.
warning: default scripting plugin is disabled: The provided plugin org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar is not compatible with this version of compiler
error: unable to evaluate script, no scripting plugin loaded
IntelliJ Version:
Gradle version : 6.3
build.gradle.kts
plugins {
id("org.jetbrains.kotlin.jvm") version "1.3.70"
// Apply the application plugin to add support for building a CLI application.
application
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// Align versions of all Kotlin components
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
// Use the Kotlin JDK 8 standard library.
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// Use the Kotlin test library.
testImplementation("org.jetbrains.kotlin:kotlin-test")
// Use the Kotlin JUnit integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
application {
// Define the main class for the application.
mainClassName = "Music.AppKt"
}
The correct way to build a Gradle project in IDEA is to execute "Main menu | Build | Build project" if your build is delegated to Gradle in IDEA settings, or execute one of the build tasks in Gradle tool window: http://jetbrains.com/help/idea/work-with-gradle-tasks.html
However, the behavior you described can be considered a usability problem, I created an issue https://youtrack.jetbrains.com/issue/KT-37814, please follow it for updates.

Gradle plugins' transitive dependencies interfering and breaking the build

I am using Gradle 6.1 in a multimodule project. I am also using two plugins: kotlin("jvm") and id("com.google.cloud.tools.jib"), and they are loaded in the following modules:
root/
build.gradle.kts loads kotlin("jvm")
services/
my-service/
rest/
build.gradle.kts loads id("com.google.cloud.tools.jib")
(There are more modules, files etc. but these are the relevant ones.)
The build fails:
$ ./gradlew clean jibDockerBuild
...
* What went wrong:
Execution failed for task ':services:driver:rest:jibDockerBuild'.
> com.google.cloud.tools.jib.plugins.common.BuildStepsExecutionException: 'org.apache.http.client.config.RequestConfig$Builder
org.apache.http.client.config.RequestConfig$Builder.setNormalizeUri(boolean)'
I identified the issue: both the Kotlin and JIB plugins have a transitive dependency on org.apache.httpcomponents:httpclient: Kotlin requires 4.5.3 and JIB 4.5.10. The problem is, in this project setup only 4.5.3 is loaded, and JIB fails as the new method is not available. This can be checked with ./gradlew buildEnv.
I've found a workaround, I need to load both plugins at the root level (which one is first seems to be irrelevant) in the main Gradle file; now ./gradlew buildEnv shows that the higher dependency version is used, also for Kotlin (output shortened and incomplete):
classpath
+--- org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.3.61
| \--- org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61
| +--- de.undercouch:gradle-download-task:3.4.3
| | \--- org.apache.httpcomponents:httpclient:4.5.3 -> 4.5.10
It works in this case, but it could be that the new library version breaks the Kotlin plugin. The problem is that the plugins and their dependencies are on the classpath without separation, something that was normal on Java before Jigsaw etc. Is there any way for Gradle to be able to separate the dependencies so that each plugin uses exactly the version it declares? I am building on Java 11, so the module system could be utilized, but does Gradle have an option to turn it on?
EDIT: updating to Kotlin 1.3.70 also fixes the issue as it doesn't depend on the library any longer. The general question is still valid, though.
Is there any way for Gradle to be able to separate the dependencies so that each plugin uses exactly the version it declares
No.
All plugins share the same build script configuration: classpath
It follows the same dependency resolution that application dependencies follow. So you can enforce that for this particular dependency only use a specific version always:
buildscript {
configurations {
classpath {
resolutionStrategy {
force("org.apache.httpcomponents:httpclient:4.5.10")
}
}
}
}
That's just one of many ways you can take control of dependency resolution for build script dependencies. You could also use a platform to advise on the dependency versions:
buildscript {
dependencies {
classpath(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:2.2.5.RELEASE"))
}
}
Refer to the docs for more info:
https://docs.gradle.org/current/userguide/resolution_rules.html
https://docs.gradle.org/current/userguide/platforms.html

Fails to resolve dependency for a sub-module with spring boot plugin

I use gradle, and has a module/project that has spring boot plugin applied. Lets Say in a project sub-module, I have apply plugin: "spring-boot".
Now from a another module/project I am adding a testCompile dependency to the sub-module. Say from another project main-module, I have added the below dependency
dependencies {
testCompile project(":sub-module")
}
But when I test compile main-module, I get the below error,
* What went wrong:
Could not resolve all dependencies for configuration ':main-module:testCompile'.
> Could not find org.springframework.boot:spring-boot-starter-logging:.
Searched in the following locations:
https://jcenter.bintray.com/org/springframework/boot/spring-boot-starter-logging//spring-boot-starter-logging-.pom
https://jcenter.bintray.com/org/springframework/boot/spring-boot-starter-logging//spring-boot-starter-logging-.jar
Required by:
com.xx.xx-main-module:1.0-SNAPSHOT > com.xx.xx:sub-module:1.0-SNAPSHOT
> Could not find org.springframework.boot:spring-boot-starter-data-solr:.
Searched in the following locations:
https://jcenter.bintray.com/org/springframework/boot/spring-boot-starter-data-solr//spring-boot-starter-data-solr-.pom
https://jcenter.bintray.com/org/springframework/boot/spring-boot-starter-data-solr//spring-boot-starter-data-solr-.jar
Required by:
com.xx.xx:main-module:1.0-SNAPSHOT > com.xx.xx:sub-module:1.0-SNAPSHOT
The above error implies that I need to apply spring-boot in main-module as well and have all the buildScript dependencies to spring repos in the main-module build.gradle.
Just seems wrong that main-module has to declare plugins and dependencies that it is least bothered about.
Gradle version : 2.10
Spring-boot version : 1.3.2.RELEASE

Resources