Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

unable to apply gradle/kotlin script #814

Closed
abendt opened this issue Apr 9, 2018 · 16 comments
Closed

unable to apply gradle/kotlin script #814

abendt opened this issue Apr 9, 2018 · 16 comments

Comments

@abendt
Copy link

abendt commented Apr 9, 2018

Expected Behavior

I want to import (apply) a gradle/kotlin script that contains shared logic into a given gradle/kotlin script.

Current Behavior

works, if the imported script is groovy

fails after i migrated the imported script to kotlin
the imported script works standalone

Context

in a multimodule build I have some shared logic to configure/start/stop some docker containers.
some of the modules apply the shared script.

Steps to Reproduce (for bugs)

I setup a stripped down example
https://github.com/abendt/gradle-docker-modularity

cd module1; gw dockerVersion

module1 uses the groovy version of the script ../docker.gradle. the task dockerVersion comes from the shared script.

cd module2; gw dockerVersion

module2 uses the kotlin version of the script ../docker2.gradle.kts

module2 fails with:

> Configure project :
e: /Users/alphonse.bendt/Projects/gradle-docker-modularity/docker2.gradle.kts:1:12: Unresolved reference: bmuschko
e: /Users/alphonse.bendt/Projects/gradle-docker-modularity/docker2.gradle.kts:2:12: Unresolved reference: bmuschko
e: /Users/alphonse.bendt/Projects/gradle-docker-modularity/docker2.gradle.kts:13:11: Unresolved reference: bmuschko
e: /Users/alphonse.bendt/Projects/gradle-docker-modularity/docker2.gradle.kts:15:26: Unresolved reference: DockerInfo
e: /Users/alphonse.bendt/Projects/gradle-docker-modularity/docker2.gradle.kts:18:29: Unresolved reference: DockerVersion


FAILURE: Build failed with an exception.

* Where:
Script '/Users/alphonse.bendt/Projects/gradle-docker-modularity/docker2.gradle.kts' line: 1

* What went wrong:
Script compilation errors:

  Line 01: import com.bmuschko.gradle.docker.tasks.container.*
                      ^ Unresolved reference: bmuschko

  Line 02: import com.bmuschko.gradle.docker.tasks.*
                      ^ Unresolved reference: bmuschko

  Line 13: apply<com.bmuschko.gradle.docker.DockerRemoteApiPlugin>()
                     ^ Unresolved reference: bmuschko

  Line 15: createTask("dockerInfo", DockerInfo::class) {
                                    ^ Unresolved reference: DockerInfo

  Line 18: createTask("dockerVersion", DockerVersion::class) {
                                       ^ Unresolved reference: DockerVersion

5 errors

However its possible to invoke the included script directly so it seems to be correct:

gw -b ../docker2.gradle.kts dockerVersion

> Task :dockerVersion
Retrieving Docker version.
...

Also the script is shown to be faulty in IntelliJ
bildschirmfoto 2018-04-09 um 10 49 05

Your Environment

Gradle:

gw --version

------------------------------------------------------------
Gradle 4.6
------------------------------------------------------------

Build time:   2018-02-28 13:36:36 UTC
Revision:     8fa6ce7945b640e6168488e4417f9bb96e4ab46c

Groovy:       2.4.12
Ant:          Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM:          1.8.0_152 (Oracle Corporation 25.152-b16)
OS:           Mac OS X 10.12.6 x86_64

IntelliJ:

IntelliJ IDEA 2018.1 (Ultimate Edition)
Build #IU-181.4203.550, built on March 26, 2018
Licensed to ...

JRE: 1.8.0_152-release-1136-b20 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.12.6
@Archinamon
Copy link

The same thing even without inter-modules file linking. Just have a main build.gradle.kts and some folder, let's name it scripts on the same level with build file:
• any kts file within the scripts folder won't see imports from outer plugins applied in main build file
• but all files will see imports from buildSrc

Any ETA or workaround on this?

@JLLeitschuh
Copy link
Contributor

Possibly related to #601

@Archinamon
Copy link

#601 more about syntax highlight and IDE support, in this case gradle not even builds these scripts :(

@JLLeitschuh
Copy link
Contributor

You might want to try this instead:

buildscript {
    dependencies {
        classpath("com.bmuschko:gradle-docker-plugin:3.2.6")
    }
}
apply {
    plugin("com.bmuschko.docker-remote-api")
}

@abendt
Copy link
Author

abendt commented Apr 16, 2018

IntelliJ marks the file as faulty:
bildschirmfoto 2018-04-16 um 15 32 01

I can use the file standalone using the shell: gw -b ../docker2.gradle.kts tasks

However when including it exception seems to be the same:

> Configure project :module1
e: /Users/alphonse.bendt/Projects/gradle-docker-modularity/docker2.gradle.kts:1:12: Unresolved reference: bmuschko
e: /Users/alphonse.bendt/Projects/gradle-docker-modularity/docker2.gradle.kts:2:12: Unresolved reference: bmuschko
e: /Users/alphonse.bendt/Projects/gradle-docker-modularity/docker2.gradle.kts:17:26: Unresolved reference: DockerInfo
e: /Users/alphonse.bendt/Projects/gradle-docker-modularity/docker2.gradle.kts:20:29: Unresolved reference: DockerVersion


FAILURE: Build failed with an exception.

* Where:
Script '/Users/alphonse.bendt/Projects/gradle-docker-modularity/docker2.gradle.kts' line: 1

* What went wrong:
Script compilation errors:

  Line 01: import com.bmuschko.gradle.docker.tasks.container.*
                      ^ Unresolved reference: bmuschko

  Line 02: import com.bmuschko.gradle.docker.tasks.*
                      ^ Unresolved reference: bmuschko

  Line 17: createTask("dockerInfo", DockerInfo::class) {
                                    ^ Unresolved reference: DockerInfo

  Line 20: createTask("dockerVersion", DockerVersion::class) {
                                       ^ Unresolved reference: DockerVersion

4 errors

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

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

BUILD FAILED in 1s

There is a note in the plugin documentation "If you would like to use this plugin from a build file which is not the main build file you must apply/use it like this:..." (https://github.com/bmuschko/gradle-docker-plugin)

I interpret it that I should use the first form to include the plugin (like in my example)

@JLLeitschuh
Copy link
Contributor

JLLeitschuh commented Apr 16, 2018

For sanity's sake, can you do me a favor and try with version 3.2.0 of the plugin?

I know that this works for me:

import com.bmuschko.gradle.docker.tasks.container.DockerCreateContainer
import com.bmuschko.gradle.docker.tasks.container.DockerRemoveContainer
import com.bmuschko.gradle.docker.tasks.container.DockerStartContainer
import com.bmuschko.gradle.docker.tasks.container.DockerStopContainer
import com.bmuschko.gradle.docker.tasks.image.DockerPullImage
import com.bmuschko.gradle.docker.tasks.DockerVersion
import java.net.Socket

buildscript {
    dependencies {
        classpath("com.bmuschko:gradle-docker-plugin:3.2.0")
    }
}
apply {
    plugin("com.bmuschko.docker-remote-api")
}

fun isPortInUse(hostName: String, portNumber: Int) = try {
    Socket(hostName, portNumber).close()
    true
} catch (exception: Exception) {
    false
}

val isRabbitmqRunning = isPortInUse("localhost", 5672)

fun Task.skipIfRabbitmqRunning() {
    onlyIf {
        !isRabbitmqRunning
    }
}

/**
 * Workaround for:
 * - https://github.com/gradle/kotlin-dsl/issues/593
 * - https://github.com/bmuschko/gradle-docker-plugin/issues/495
 */
class CustomKotlinClosure1<in T : Any?, V : Any>(
    val function: T.() -> V?,
    owner: Any? = null,
    thisObject: Any? = null
) : groovy.lang.Closure<V?>(owner, thisObject) {

    @Suppress("unused") // to be called dynamically by Groovy
    fun doCall(it: T): V? = it.function()
}

// skipDocker is not included in this example, it's my own code.
fun Task.skipIfSkipDockerSpecified() {
    onlyIf { !skipDocker }
}

val getDockerVersion = task<DockerVersion>("getDockerVersion") {
    onError = CustomKotlinClosure1<Exception?, Unit>({
        if (this == null) {
            // TODO: Fix after this is resolved: https://github.com/bmuschko/gradle-docker-plugin/issues/490
            return@CustomKotlinClosure1 Unit
        }
        val message = "Docker is not installed or is not running."
        if (skipDocker) {
            logger.error("$message Skipping Docker dependent tasks.")
        } else {
            throw GradleException("$message To skip Docker tasks run with -P$skipDockerPropertyName flag.")
        }
    })
}

val pullRabbitmqContainer = task<DockerPullImage>("pullRabbitmq") {
    tag = "latest"
    repository = "rabbitmq"
    dependsOn(getDockerVersion)
    skipIfRabbitmqRunning()
    skipIfSkipDockerSpecified()
}

val createRabbitmqContainer = task<DockerCreateContainer>("createRabbitmq") {
    dependsOn(pullRabbitmqContainer)
    portBindings = listOf("5672:5672")
    targetImageId {
        pullRabbitmqContainer.run { if (tag != null) "$repository:$tag" else repository }
    }
    skipIfRabbitmqRunning()
    skipIfSkipDockerSpecified()
}

val startRabbitmqContainer = task<DockerStartContainer>("startRabbitmq") {
    dependsOn(createRabbitmqContainer)
    targetContainerId { createRabbitmqContainer.containerId }
    skipIfRabbitmqRunning()
    skipIfSkipDockerSpecified()
}

val removeRabbitmqContainer = task<DockerRemoveContainer>("removeRabbitmq") {
    onlyIf { createRabbitmqContainer.containerId != null }
    targetContainerId { createRabbitmqContainer.containerId }
}

val stopRabbitmqContainer = task<DockerStopContainer>("stopRabbitmq") {
    onlyIf { createRabbitmqContainer.containerId != null }
    targetContainerId { createRabbitmqContainer.containerId }
    finalizedBy(removeRabbitmqContainer)
}

@abendt
Copy link
Author

abendt commented Apr 16, 2018

sure.

again the script works standalone but cannot be imported into another script ...

@JLLeitschuh
Copy link
Contributor

JLLeitschuh commented Apr 16, 2018

Ah. Okay, I understand now.
@bamboo & @eskatos
Does the following work in included scripts?

buildscript {
    dependencies {
        classpath("com.bmuschko:gradle-docker-plugin:3.2.0")
    }
}
apply {
    // some plugin
}

@JLLeitschuh
Copy link
Contributor

@abendt You may want to try putting this in your build.gradle.kts instead of in your docker2.gradle.kts file.

buildscript {
    dependencies {
        classpath("com.bmuschko:gradle-docker-plugin:3.2.0")
    }
}

@abendt
Copy link
Author

abendt commented Apr 16, 2018

I get the same 4 script compilation errors.

@JLLeitschuh
Copy link
Contributor

@abendt
In build.gradle.kts you have:

buildscript {
    dependencies {
        classpath("com.bmuschko:gradle-docker-plugin:3.2.0")
    }
}
apply {
    from("docker2.gradle.kts")
}

And in your docker2.gradle.kts you have:

// Some imports
apply {
    // some plugin
}

And you still get compilation issues?

@abendt
Copy link
Author

abendt commented Apr 16, 2018

@JLLeitschuh yes, i still get compilation issues.

I updated my demo project: https://github.com/abendt/gradle-docker-modularity

@bamboo
Copy link
Member

bamboo commented Apr 18, 2018

@JLLeitschuh, no, the buildscript block is not supported in applied scripts, see #180.

@JLLeitschuh
Copy link
Contributor

Thanks @bamboo I couldn't find the issue but I thought this was unsupported currently. Thanks.

@Archinamon
Copy link

Any ETA on this? Groovy scripts allows using imports from plugins that applies in main build.gradle, but kts not. That's causes a lot of problems migrating groovy scripts to kts.

@eskatos
Copy link
Member

eskatos commented Apr 25, 2018

The buildscript {} block in applied scripts is now supported, see #180, but IDE support is not there yet, see #110.

Closing as a duplicate

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants