Kotlin-dsl-samples: "apply from/to" support in buildscript block

Created on 19 Sep 2017  路  21Comments  路  Source: gradle/kotlin-dsl-samples

I am trying to convert the follwing build.gradle into kotlin-dsl:

buildscript { 
  apply from: "gradle/buildscript.gradle", to: buildscript 
}
apply from: "gradle/project-footer.gradle"

The following build.gradle.kts gives me Unresolved reference: from:

import org.gradle.kotlin.dsl.apply

buildscript { 
  apply { 
    from("gradle/buildscript.gradle")
    to(buildscript) 
  } 
}
apply { 
  from("gradle/project-footer.gradle")
}

It seems that the "apply from" support in the buildscript block is missing.

Ref. https://kotlinlang.slack.com/archives/C19FD9681/p1505817768000291

blocked

All 21 comments

Update from the kotlinlang slack:

_Henrik [1:23 PM]_
What exactly is it that's not supported? apply within buildscript?

_Rodrigo B. de Oliveira [1:32 PM]_
Two things, apply within buildscript and to

The following syntax used to be supported in Gradle 4.1.

buildscript {
    applyFrom("repositoryConfig.gradle.kts")
}

Why this is an important feature

I want to be able to do this in my build files:

buildscript {
    applyFrom("repositoryConfig.gradle.kts")
    repositories {
        val buildScriptRepositoryConfig : (Any) -> Unit by extra
        buildScriptRepositoryConfig(this)
    }
}

That way, I can share my repository configuration between the build scripts and resolving source dependencies.

I also want to be able to use the same logic in repositoryConfig.gradle.kts in my buildSrc/build.gradle.kts file.

Example buildSrc/build.gradle.kts:

apply {
    from("../repositoryConfig.gradle.kts")
}
repositories {
    val buildScriptRepositoryConfig : (Any) -> Unit by extra
    buildScriptRepositoryConfig(this)
}

Workaround in Gradle 4.2:

buildscript {
    project.apply {
        from("repositoryConfig.gradle.kts")
    }
}

I have this build.gradle.kts:

buildscript {
    project.apply {
        from("repositoryConfig.gradle.kts")
    }
}

and this repositoryConfig.gradle.kts:

repositories {
    maven(url = "https://plugins.gradle.org/m2/")
}

dependencies {
    classpath("com.gradle:build-scan-plugin:1.9")
}

Now I get Unresolved reference: classpath. Any ideas?

(Yes, I know I should use the plugin directive, but I have some other plugins that I need to include using classpath)

Hi @henrik242,

Since the classpath configuration is not _statically_ known to exist at that point it needs to be specified via a string literal:

    "classpath"("com.gradle:build-scan-plugin:1.9")

Alternatively, if you want to do that then you could also potentially pass a function into the extra properties extension.
You can see my example of my repositoryConfig.gradle.kts here:
https://github.com/gradle/kotlin-dsl/issues/471#issuecomment-326069920

@bamboo Now I get Configuration with name 'classpath' not found instead.

@henrik242 that's expected since the script is being applied to the project. To make the applied script in your sample work as is will require support for apply { from("..."); to(buildscript); }

I think I have a fix for this problem. PR will be forthcoming!

Very cool!

@JLLeitschuh Does it support to as well? Otherwise the 'Closes' statement isn't really correct, I guess.

It should because to is on the ObjectConfigurationAction type.

@henrik242 @bamboo This is a duplicate of mine: https://github.com/gradle/kotlin-dsl/issues/485.

Switching from applyFrom(rootProject.file("gradle/versions.gradle.kts")) to rootProject.apply { from(rootProject.file("gradle/versions.gradle.kts")) } did the trick.

So, I'm trying to use the buildscript apply stuff, but I'm getting this:

A problem occurred configuring root project 'kts-test-project'.
> Unsupported target org.gradle.api.internal.initialization.DefaultScriptHandler: org.gradle.api.internal.initialization.DefaultScriptHandler@6727cd3a

Test repo is in https://github.com/henrik242/kts-test-project. I'm probably doing something very stupid :) Any ideas? @jaredsburrows @JLLeitschuh

Have a look at my solution up here:
https://github.com/gradle/kotlin-dsl/issues/497#issuecomment-330946768

@JLLeitschuh I tried replacing my current content with yours, but I still get the exact same error:

$ git diff
diff --git a/build.gradle.kts b/build.gradle.kts
index ef73e07..be0a6c7 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -1,6 +1,6 @@
 buildscript {
-    rootProject.apply {
-        from(rootProject.file("gradle/buildscript.gradle.kts"))
+    project.apply {
+        from("gradle/buildscript.gradle.kts")
         to(buildscript)
     }
 }

Don't use the to method. It's not currently supported I don't think.

@JLLeitschuh OK, earlier in this thread you stated the opposite: "It should because to is on the ObjectConfigurationAction type.": https://github.com/gradle/kotlin-dsl/issues/497#issuecomment-344760133

You're right. Sorry for the confusing information.

Blocked by #659

Superseded by #659 and #682.

Was this page helpful?
0 / 5 - 0 ratings