4

org.gradle.kotlin.dsl与like android或etc一起出现的 Kotlin 扩展很少publishing。比如说,对于publishing,扩展定义如下

fun org.gradle.api.Project.`publishing`(configure: org.gradle.api.publish.PublishingExtension.() -> Unit): Unit =
    (this as org.gradle.api.plugins.ExtensionAware).extensions.configure("publishing", configure)

这意味着在我的范围内我build.gradle.kts可以打电话

publishing {
    // blablabla
}

就我而言,我有一个单独的文件来处理定义为random.gradle.kts并位于完全随机目录中的发布。

在这里,我想访问此publishing扩展程序但无法访问。

欢迎任何建议。

再举一个例子android也是不可取的。在我的build.gradle.kts我可以访问android,但在我的random.gradle.kts我无法做到。

android扩展定义如下

val org.gradle.api.Project.`android`: com.android.build.gradle.LibraryExtension get() =
    (this as org.gradle.api.plugins.ExtensionAware).extensions.getByName("android") as com.android.build.gradle.LibraryExtension

事实上,LibraryExtension 也是不可访问的。

那么,如何将这些扩展传递导入到我的random.gradle.kts

4

1 回答 1

0

    //build.gradle.kts tested with gradle 6.7
    
    subprojects {
      afterEvaluate {
        (extensions.findByType(com.android.build.gradle.LibraryExtension::class)
          ?: extensions.findByType(com.android.build.gradle.AppExtension::class))?.apply {
    
          println("Found android subproject $name")
          lintOptions.isAbortOnError = false
          compileSdkVersion(30)
          
          if (this is com.android.build.gradle.AppExtension) 
            println("$name is an application")
          
          if (this is com.android.build.gradle.LibraryExtension) {
    
            val publishing =
              extensions.findByType(PublishingExtension::class.java) ?: return@afterEvaluate
    
            val sourcesJar by tasks.registering(Jar::class) {
              archiveClassifier.set("sources")
              from(sourceSets.getByName("main").java.srcDirs)
            }
    
            afterEvaluate {
              publishing.apply {
                val projectName = name
                publications {
                  val release by registering(MavenPublication::class) {
                    components.forEach {
                      println("Publication component: ${it.name}")
                    }
                    from(components["release"])
                    artifact(sourcesJar.get())
                    artifactId = projectName
                    groupId = ProjectVersions.GROUP_ID
                    version = ProjectVersions.VERSION_NAME
                  }
                }
              }
            }
          }
        }
      }
    }

于 2020-10-22T23:27:42.583 回答