Plugin [id: 'org.jetbrains.kotlin.plugin.spring'] not found error when defining plugin in .gradle.kts files under buildSrc directory - kotlin

I am trying to create plugins for a multi-module Kotlin application such as build configurations can be more easily maintained shared among several modules (core, model, lib, app) using Gradle 6.8.1.
At the root project directory, gradle init is executed and the following options are inputted.
option
selection
type
application
language
Kotlin
multiple subprojects
yes
build script DSL
Kotlin
The following .gradle.kts files are generated under buildSrc/src/main/kotlin directory.
my.package.kotlin-applications.gradle.kts
my.package.kotlin-common-conventions.gradle.kts
my.package.kotlin-library-conventions.gradle.kts
As I will be using Spring Boot features, I am trying to add several plugins in my.package.kotlin-library-conventions.gradle.kts which will be used by lib and app modules.
plugins {
id("my.package.kotlin-common-conventions")
// new plugins
kotlin("plugin.spring")
id("org.springframework.boot")
id("io.spring.dependency-management")
`java-library`
}
When I execute gradle build, the following error is returned.
Plugin [id: 'org.jetbrains.kotlin.plugin.spring'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)
I tried to edit the contents in below files but still cannot resolve the above error.
settings.gradle.kts
pluginManagement {
repositories {
jcenter()
gradlePluginPortal()
maven { url = uri("https://repo.spring.io/milestone") }
maven { url = uri("https://repo.spring.io/snapshot") }
}
plugins {
kotlin("jvm") version "1.4.21-2"
kotlin("plugin.spring") version "1.4.21-2"
id("org.springframework.boot") version "2.5.0-SNAPSHOT"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
}
}
buildSrc/build.gradle.kts
plugins {
`kotlin-dsl`
kotlin("plugin.spring")
id("org.springframework.boot")
id("io.spring.dependency-management")
}
repositories {
gradlePluginPortal()
jcenter()
maven { url = uri("https://repo.spring.io/milestone") }
maven { url = uri("https://repo.spring.io/snapshot") }
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin")
}

Related

"Could not find method wrapper()" in build.gradle file generated by openapi-generator kotlin client

I have a simple spring boot project using the kotlin gradle dsl. I want to generate an OpenApi client using the openapi client generator gradle Plugin. I have successfully done so, using this configuration. Until now, this was a single project build. But when i try to include it, i get an error message "Could not find method wrapper()".
This is how i generated the client and added it's file into my source sets:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "3.0.2"
id("io.spring.dependency-management") version "1.1.0"
kotlin("jvm") version "1.7.22"
kotlin("plugin.spring") version "1.7.22"
id("org.openapi.generator") version "6.3.0"
}
// other dependencies
openApiGenerate {
generatorName.set("kotlin")
inputSpec.set("src/main/openapi/my-api.yml")
outputDir.set("$buildDir/generated/my-api")
packageName.set("com.myapi")
}
kotlin.sourceSets["main"].kotlin.srcDir("$buildDir/generated/my-api/src/main/kotlin")
Now i want to use this generated client in my project. It comes with it's own build.gradle (in groovy) which loads the necessary dependencies etc.
I have modified my settings.gradle.kts file accordingly:
rootProject.name = "myapp"
include("build:generated:my-api")
When i reload gradle, i get the follwing error:
> Could not find method wrapper() for arguments [build_gdswinwcvulw9afq79kj4v6h$_run_closure1#582f32f7] on project ':build:generated:my-api' of type org.gradle.api.Project.
This is due to the build.gradle file generated by the generator looking like this:
group 'org.openapitools'
version '1.0.0'
wrapper {
gradleVersion = '7.5'
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
}
buildscript {
ext.kotlin_version = '1.7.21'
repositories {
maven { url "https://repo1.maven.org/maven2" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
apply plugin: 'maven-publish'
repositories {
maven { url "https://repo1.maven.org/maven2" }
}
test {
useJUnitPlatform()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.squareup.moshi:moshi-kotlin:1.13.0"
implementation "com.squareup.moshi:moshi-adapters:1.13.0"
implementation "com.squareup.okhttp3:okhttp:4.10.0"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
}
I am using Gradle 7.6 and i am a bit out of ideas here since i am pretty new to Gradle.

Specify kotlin version in buildSrc kotlin convention plugin

I am developing a Kotlin project with multiple subprojects as explained here
Building Kotlin Applications with libraries Sample (gradle.org)
Is it correct to specify the kotlin version as as shown below?
file: buildSrc/build.gradle.kts
dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:<kotlin-version>")
}
Also, is it possible to move this kotlin version to a central location for declaring project-wide dependency version (for example gradle.properties or buildSrc/**/Dependencies.kt or versions catalog)? None of the approaches mentioned seems to be supported in buildSrc/build.gradle.kts.
There are many approaches to specify the kotlin version (or versions of any dependency) at a centralized location in a Gradle Multimodule Project.
gradle.properties
buildSrc/**/Dependency.kt
versions catalog
However, if you are defining a Kotlin Convention Plugin in buildSrc, the kotlin version is not available in buildSrc/build.gradle.kts using any of the methods mentioned above so as to pick the right kotlin-gradle-plugin.
For a workaround solution, use the following.
# file:<root>/gradle.properties
kotlinVersion=1.5.31
// file:<root>/buildSrc/build.gradle.kts
import java.io.*
import java.util.*
// read gradle.properties programmatically
val props = Properties()
FileInputStream(file("../gradle.properties")).use {
props.load(it)
}
plugins {
`kotlin-dsl`
}
repositories {
gradlePluginPortal()
}
dependencies {
val kotlinVersion = props.getProperty("kotlinVersion")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
}
In all other <root>/**/<subproject>/build.gradle.kts files access the kotlinVersion like
val kotlinVersion = project.properties["kotlinVersion"]
In settings.gradle.kts, access the kotlinVersion like
val kotlinVersion:String by settings
If anyone from the Gradle team reading this, please provide an API to access gradle.properties consistently across any of the build script files.
in app gradle
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
and project gradle
ext.kotlin_version = "1.6.0"
I have solved this issue by specifying kotlinVersion in gradle.properties file as below.
kotlinVersion=1.6.10
Keep gradle.properties file at outermost project level.
You can access kotlinVersion in any submodules and project as
//top level declaration
val kotlinVersion by extra(project.property("kotlinVersion"))
//usage in dependency block
"org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}"
To verify whether you are able to read it, you can specify following
println(project.property("kotlinVersion"))
This should print kotlin version as
> Configure project :
1.6.10
build.gradle.kts file
val kotlinVersion by extra(project.property("kotlinVersion"))
println("${kotlinVersion}")
group = "com.company"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
mavenLocal()
maven { url = uri("https://repo.spring.io/milestone") }
maven { url = uri("https://repo.spring.io/snapshot") }
gradlePluginPortal()
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
}
gradle.properties file
kotlin.code.style=official
#kotlin.parallel.tasks.in.project=true
#kapt.incremental.apt=true
org.gradle.parallel=true
org.gradle.caching=false
org.gradle.daemon=true
org.gradle.jvmargs=-Dfile.encoding=UTF-8
kotlinVersion=1.6.10
Ref:
project_properties
extra_properties

Kotlin Gradle include runtime when building jar

I'm trying to include the kotlin runtime when building my jar with gradle. I cannot find an update to date thread anywhere online that shows how to do this.
Currently I have tried the following
plugins {
kotlin("jvm") version "1.5.10"
}
repositories {
mavenCentral()
}
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
freeCompilerArgs = freeCompilerArgs + "-include-runtime"
jvmTarget = "16"
}
}
This builds the jar fine but doesn't include the runtime like I would expect. How can I configure gradle to do this?
You can use the shadowJar plugin. Add
id("com.github.johnrengelman.shadow") version "7.0.0"
To your plugins, then run ./gradlew shadowJar to create a jar file in build/lib containing the kotlin runtime as well as any of your runtime dependencies

Kotlin + Gradle for JVM New Module Error in IntelliJ

My project is composed of Kotlin/JVM + Gradle
I'm trying to add a module
After creating the module it gives me a single files:
|--main-project
|----new-module
|------build.gradle.kts
|--<other-main-project-files>
module gradle:
plugins {
kotlin("jvm") version "1.4.32"
}
group = "com.example"
version = "0.0.1"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib"))
}
It gives me an error Unresolved reference implementation and I can't create kotlin/java files
I have no issue if I use java instead of kotlin/jvm
I fixed the problem after going through Gradle logs
kotlin("jvm") version "1.4.32"
should be changed to
kotlin("jvm")

How to properly configure Kotlin plugin for Gradle?

I'm trying to build a basic Gradle project. My build.gradle file is below. I keep getting the below error. I don't get it because you can find the plugin here... where I think I'm directing to: https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-gradle-plugin
Update: same thing happens to the spring-boot plugin if I comment out the Kotlin line. It's not just specific to the Kotlin plugin.
The error:
Error:(21, 0) Plugin [id: 'kotlin', version: '1.1.1'] was not found in any of the following sources:
- Gradle Core Plugins (not a core plugin, please see https://docs.gradle.org/3.3/userguide/standard_plugins.html for available core plugins)
- Gradle Central Plugin Repository (no 'kotlin' plugin available - see https://plugins.gradle.org for available plugins)
Build.gradle:
buildscript {
ext.kotlin_version = '1.1.1'
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE")
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
plugins {
id 'java'
id 'kotlin' version "1.1.1"
id 'spring-boot'
}
group 'si.dime.kotlin.tutorials.rest'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile("org.springframework.boot:spring-boot-starter-web:1.3.3.RELEASE")
}
For non-core Gradle plugins (those that are not from the org.gradle namespace), you have to use a fully qualified id as well as a version number.
The official Kotlin documentation contains the id to use:
plugins {
id "org.jetbrains.kotlin.jvm" version "<version to use>"
}
(You can also find these ids by searching for the plugin on the plugins.gradle.org site.)