Difficulty moving Gradle configuration to an external script - kotlin

I am trying to move some parts of my Gradle build script to an external configuration file that can be shared among projects. Here is an example with the Detekt plugin:
Current Code
build.gradle.kts (condensed to only the relevant parts)
plugins{
id("io.gitlab.arturbosch.detekt").version("1.19.0-RC1")
}
...
detekt{
...
}
What I'm trying to do
build.gradle.kts
apply(File("common.gradle.kts"))
common.gradle.kts
plugins {
id("io.gitlab.arturbosch.detekt").version("1.19.0-RC1")
}
detekt{
...
}
but when I do this I get this error:
<my_project>\common.gradle.kts:7:1: Unresolved reference: detekt
So the plugin section doesn't appear to be doing anything. And just to be clear, this plugin does not need anything in dependencies section, it works fine inside build.gradle.kts with only the plugin declaration.
Why doesn't this work?

I am using it with normal Groovy Gradle scripts like this:
# detekt.gradle
apply plugin: 'io.gitlab.arturbosch.detekt'
tasks.detekt.jvmTarget = "1.8"
detekt {
buildUponDefaultConfig = true
input = files("${rootProject.projectDir}/app/src")
config = files("${rootProject.projectDir}/detekt.yml")
reports {
html.enabled = true
xml.enabled = true
}
}
# build.gradle
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath libs.android.gradle
classpath libs.kotlin.gradle
classpath libs.detekt
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
subprojects {
apply from: rootProject.file('detekt.gradle')
apply from: rootProject.file('ktlint.gradle')
apply from: rootProject.file('spotless.gradle')
...
}

Related

Kotlin NoSuchMethodError after installing a library that enables a plugin

I want to experience a library called arrow analysis
My build.gradle.kts file looks as follows:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.7.10"
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation(kotlin("test"))
implementation("io.arrow-kt:arrow-core:1.1.2")
}
buildscript {
dependencies {
classpath("io.arrow-kt.analysis.kotlin:io.arrow-kt.analysis.kotlin.gradle.plugin:2.0")
}
}
apply(plugin = "io.arrow-kt.analysis.kotlin")
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
after an attempt to build the project, I get the following error:
java.lang.NoSuchMethodError: 'org.jetbrains.kotlin.psi.KtExpression org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt.getReceiverExpression(org.jetbrains.kotlin.resolve.calls.model.ResolvedCall)'
at arrow.meta.plugins.analysis.phases.analysis.solver.ast.kotlin.KotlinResolvedCall.getReceiverExpression(KotlinResolvedCall.kt:37)
at arrow.meta.plugins.analysis.phases.analysis.solver.ResolvedCallUtilsKt.allArgumentExpressions(ResolvedCallUtils.kt:75)
at arrow.meta.plugins.analysis.phases.analysis.solver.ResolvedCallUtilsKt.arg(ResolvedCallUtils.kt:119)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.controlFlowAnyFunction(Expressions.kt:438)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.checkCallExpression(Expressions.kt:397)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.fallThrough(Expressions.kt:260)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.access$fallThrough(Expressions.kt:1)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt$checkExpressionConstraints$2.invoke(Expressions.kt:244)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt$checkExpressionConstraints$2.invoke(Expressions.kt:157)
at arrow.meta.continuations.ContSeq$flatMap$1.invokeSuspend(ContSeq.kt:60)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
I checked the source code, and everything seems to be in place. Did I something wrong as per the instructions to install this library which is denoted as follows:
buildscript {
dependencies {
classpath("io.arrow-kt.analysis.kotlin:io.arrow-kt.analysis.kotlin.gradle.plugin:2.0")
}
}
apply(plugin = "io.arrow-kt.analysis.kotlin")
You need to apply the plugin inside the plugins block and in the same fashion you're applying the Kotlin JVM plugin.
They mention to use it like this on their official docs
plugins {
kotlin("multiplatform") version "1.6.21"
// other plugins
id("io.arrow-kt.analysis.kotlin") version "2.0.2"
}
buildscript {
repositories {
mavenCentral()
}
}

kotlin-compiler-embeddable missing within gradle build

I am trying to setup a multiproject gradle/kotlin build and I am getting following error:
Could not determine the dependencies of task ':compileKotlin'.
> Could not resolve all files for configuration ':kotlinCompilerClasspath'.
> Cannot resolve external dependency org.jetbrains.kotlin:kotlin-compiler-embeddable:1.4.10 because no repositories are defined.
Required by:
project :
Strange thing is the empty project :.
My simplifiged build.gradle.kts looks like this:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
base
kotlin("jvm") version "1.4.10"
}
buildscript {
extra["kotlin_version"] = "1.4.10"
repositories {
jcenter()
mavenCentral()
}
}
subprojects {
apply(plugin = "kotlin")
repositories {
jcenter()
mavenCentral()
}
tasks.compileKotlin {
kotlinOptions.jvmTarget = "11"
}
tasks.compileTestKotlin {
kotlinOptions.jvmTarget = "11"
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:${rootProject.extra["kotlin_version"]}")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${rootProject.extra["kotlin_version"]}")
}
}
Do I need to duplicate the repositories within the buildscript section?
What is that is causing the error above?
It seems like your repositories { ... } configuration done in the subproject section is sufficient, but it is only applied to the supbrojects but not the root project itself (which has the Kotlin plugin applied, too, and whose task :compileKotlin is failing).
There are two ways to fix this.
First, you could move the repositories { ... } section from subprojects { ... } to a new block allprojects { ... } (thus applied to the root project as well).
Or, if you don't actually need the Kotlin plugin in the root project (i.e. you don't have Kotlin code there), you can add .apply(false) to your plugin declaration:
plugins {
kotlin("jvm").version("1.4.10").apply(false)
}

maven-publish missing artifact to repository

II'm trying to publish my lib but i'm having some problems related to its publication.
I'm using this library https://github.com/vanniktech/gradle-maven-publish-plugin to publish my project on maven central (oss), but, this for some reason only -sources.jar, -javadoc.jar and .module are being pushed to the repository, however file-butler-0.1.4-SNAPSHOT.jar is missing :(
Does someone have any idea?
build.gradle(module)
plugins {
id 'java-library'
id 'kotlin'
id 'kotlin-kapt'
}
dependencies {
def gson = "com.google.code.gson:gson:2.8.6"
def moshi = "com.squareup.moshi:moshi:1.11.0"
def moshCodeGen = "com.squareup.moshi:moshi-kotlin-codegen:1.11.0"
implementation(Deps.core.kotlin)
implementation(gson)
implementation(moshi)
kapt(moshCodeGen)
testImplementation(Deps.testing.truth)
testImplementation(Deps.testing.jUnit)
kaptTest(moshCodeGen)
}
sourceSets {
main.kotlin.srcDirs += 'src/main/kotlin'
main.java.srcDirs += 'src/main/java'
}
apply plugin: "com.vanniktech.maven.publish"
build.gradle.kts(project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:4.1.0")
classpath(kotlin("gradle-plugin", "1.4.10"))
classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.4.10")
classpath("com.vanniktech:gradle-maven-publish-plugin:0.13.0")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
mavenCentral()
google()
jcenter()
}
}
task<Delete>("clean") {
delete(rootProject.buildDir)
}
ps: publishToMavenLocal publish the expected jar to my local repository.

taskdef class com.sun.tools.xjc.XJCTask cannot be found

I am trying to generate classes from an XSD file in a Springboot project with multiple modules. I tried to follow the guide given here.
I have the below config in my root build.gradle
buildscript {
ext {
springBootVersion = '1.3.5.RELEASE'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/libs-release" }
maven { url "https://mvnrepository.com/artifact" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE")
//####################### XJC - JDK 1.7/1.8 ####################
classpath 'com.github.jacobono:gradle-jaxb-plugin:1.3.5'
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
//####################### XJC - JDK 1.7/1.8 ####################
apply plugin: 'com.github.jacobono.jaxb'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/libs-release" }
maven { url "https://mvnrepository.com/artifact" }
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
}
and below config in my module's build.gradle
dependencies {
compile project(':appCommon')
compile("org.springframework.boot:spring-boot-starter")
}
//####################### XJC - JDK 1.7/1.8 ####################
jaxb {
xjc {
xsdDir = "schemas/v1.1"
generatePackage = "com.test.domain.v1_1"
}
}
when I run the xjc task from my IntelliJ on my module, I am getting an exception as below
taskdef class com.sun.tools.xjc.XJCTask cannot be found
using the classloader AntClassLoader[]
Any help with what is going wrong is appreciated
Looks like you're missing the jaxb dependencies. In your module's build.gradle add the following:
dependencies {
jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.7-b41'
jaxb 'com.sun.xml.bind:jaxb-impl:2.2.7-b41'
jaxb 'javax.xml.bind:jaxb-api:2.2.7'
}

Can't find spock inside of intellij

I am pretty new to intellij and spock.
I am adding spock testing to my spring boot project using gradle. Here's my build.gradle:
buildscript {
ext {
springBootVersion = '1.4.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
jar {
baseName = 'theta-server'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.spockframework:spock-core:1.0-groovy-2.4')
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
And so I added a spock test such:
package com.heavyweightsoftware.theta.server.controller
/**
*
*/
class ThetaControllerTest extends spock.lang.Specification {
def "Theta"() {
}
}
But I can't run the test inside my intellij environment because it says "cannot resolve symbol 'spock'"
The build runs fine directly.
At times, IJ may not update the project classpath after some changes. It seems that this happens for both Maven POM files as well as Gradle build files. Usually, this is easily solved by forcing a sync with the Reimport all gradle/maven projects button from the appropriate tool window (maven, gradle):