6

Build failed:

thufir@dur:~/NetBeansProjects/kotlin_dsl$ 
thufir@dur:~/NetBeansProjects/kotlin_dsl$ gradle clean run 

> Configure project : 
e: /home/thufir/NetBeansProjects/kotlin_dsl/build.gradle.kts:4:12: Unresolved reference: github


FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'kotlin_dsl'.
> Could not open cache directory 74ykawxta6db3b2bfk9grjikp (/home/thufir/.gradle/caches/4.3.1/gradle-kotlin-dsl/74ykawxta6db3b2bfk9grjikp).
   > Internal error: unable to compile script, see log for details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
thufir@dur:~/NetBeansProjects/kotlin_dsl$ 

problematic import statement:

thufir@dur:~/NetBeansProjects/kotlin_dsl$ 
thufir@dur:~/NetBeansProjects/kotlin_dsl$ cat build.gradle.kts 

import org.gradle.api.JavaVersion
import org.gradle.kotlin.dsl.*
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar


plugins {
    application
    kotlin("jvm") version "1.1.51"
}

application {
    mainClassName = "samples.HelloWorldKt"
}

dependencies {
    compile(kotlin("stdlib"))
}

repositories {
    jcenter()
}


thufir@dur:~/NetBeansProjects/kotlin_dsl$ 

removing the import for Shadow as described gives a clean build and run. How is the Shadow plugin JAR made available for Kotlin to import?

Gradle itself, using the DSL, creates Shadow JAR's fine.

Coming at this from a different angle:

Kotlin meets Gradle: gradle.build.kt with ShadowJar

with a working build file.

eskatos
  • 4,174
  • 2
  • 40
  • 42
Thufir
  • 8,216
  • 28
  • 125
  • 273
  • 1
    Have you tried the build script from [this answer](https://stackoverflow.com/a/41795221/2196460)? – hotkey Feb 07 '18 at 11:27
  • 1
    In your build script, the Shadow plugin seems not to be applied in the `plugins { ... }` block, that might be the problem. – hotkey Feb 07 '18 at 11:30
  • the build fails because it can't compile the `build.gradle.kts` file -- but the error messages aren't that great. see also: https://stackoverflow.com/q/48663427/262852 – Thufir Feb 07 '18 at 12:09

1 Answers1

16

Like so:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

plugins {
    kotlin("jvm") version "1.5.21"
    id("com.github.johnrengelman.shadow") version "7.0.0"
}

group = "xxx.yyy"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib-jdk8"))
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "11"
}

tasks.withType<ShadowJar> {
    archiveFileName.set("app.jar")
}
Mikael Gueck
  • 5,511
  • 1
  • 27
  • 25
  • 1
    Just to give an update for this thread... For the latest version of gradle with Kotlin DSL and johnrengelman.shadow you will need to make some changes to avoid deprecation warnings. From the issue answered by Louis Jacomet: getArchiveFileName() returns a Property which is a mutable object, on which you can invoke Property.set(value) https://github.com/gradle/gradle/issues/10506#issuecomment-529333401 I used this code: tasks.withType() { archiveBaseName.set("app") archiveClassifier.set("...") archiveVersion.set("...") } – Jesse Lima Aug 08 '21 at 12:58