How to properly configure Kotlin plugin for Gradle? - intellij-idea

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

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.

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

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

How to make idea plugin in gradle generate proper project configuration for Kotlin?

I have configured my build.gradle to run gradle test and gradle run properly. However, IDEA does not show any run/test tasks after importing the configuration generated by gradle idea. It seems that these tasks are not included at ipr/iws at all.
Here is my build.gradle:
buildscript {
ext.kotlin_version = '1.2.0'
repositories {
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
}
}
apply plugin: 'kotlin'
apply plugin: 'org.jetbrains.dokka'
apply plugin: 'application'
apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: 'idea'
dokka {
outputFormat = 'html'
outputDirectory = "$buildDir/javadoc"
}
sourceSets {
test.kotlin.srcDirs += 'src/test/kotlin'
}
junitPlatform {
enableStandardTestTask true
}
defaultTasks 'run'
mainClassName = 'simpledb.server.Startup'
repositories {
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.2'
testRuntime (
'org.junit.jupiter:junit-jupiter-engine:5.0.2',
'org.junit.platform:junit-platform-launcher:1.0.2'
)
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}
I was wondering is there any way to make generated project configurations include these tasks(run/test)? I have also heard people saying Don't use gradle idea, so is it impossible to use gradle idea to implement this?
It is simply not necessary in your case. Just open the build.gradle file with IDEA and everything should be smooth. The idea Gradle plugin is somewhat deprecated. Not officially, but it was created by Gradle team and is not actively developed to adapt to new IDEA versions and features and so on. So if you don't need special customizations, just open the build.gradle with IDEA and you should be good to go.

Gradle DSL method not found: 'compileOnly()'

I'm trying to add new module to my application. I successfully added movie-api module (you can see in picture below), but when I try to add another module (client-app), I'm getting error as shown in picture.
I tried with different solutions including Gradle DSL method not found: 'compile()', but didn't work for me.
Appreciate your help!
Build.gradle file:
buildscript {
ext {
springBootVersion = '1.4.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'client-app'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
compileOnly was introduced in Gradle 2.12. Make sure you are using a new enough version, with both the command-line and the IDE.

How to build my xtext language with gradle for android

I've written a test-DSL in xtext and generated an IntelliJ plugin.
I have a small Android-test project and in Android Studio my DSL-editor shows up and also generates the output files as expected.
Now I try to configure gradle to also generate the files, but this fails with an error.
I found the Android Integration section for the xtext-builder. This seems out-dated (e.g. it refers to org.xtext.android, which does not exist - also the link there is broken).
Anyway, in the xtext-gradle-plugin github repo there is an org.xtext.android.builder plugin: I guess this is the correct one.
My project build.gradle file looks like this (relevant parts only):
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/" // needed for org.xtext:xtext-android-gradle-plugin
}
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath "org.xtext:xtext-android-gradle-plugin:1.0.14"
}
}
allprojects {
repositories {
jcenter()
mavenLocal() // This is required so that the mydsl language is found
}
}
when I activate the org.xtext.android.builder plugin in the build.gradle file of in my app module:
apply plugin: 'com.android.application'
apply plugin: "org.xtext.android.builder" // causes the Error!
I get this error:
Error:Unable to find method 'com.android.build.gradle.api.BaseVariant.getJavaCompiler()Lorg/gradle/api/tasks/compile/AbstractCompile;'...
I guess there's some version mismatch or something is not up-to-date.
Any ideas?
This was a bug (#73) in the xtext-gradle-plugin. It was fixed in Version 1.0.16.