gpt4 book ai didi

google-app-engine - 如何使用 Kotlin DSL 配置 AppEngine Gradle 插件

转载 作者:IT老高 更新时间:2023-10-28 13:47:42 24 4
gpt4 key购买 nike

https://cloud.google.com/appengine/docs/standard/java/tools/gradle-reference 中所述AppEngine Gradle 插件提供如下配置:

appengine {  // App Engine tasks configuration
run { // local (dev_appserver) configuration (standard environments only)
port = 8080 // default
}

deploy { // deploy configuration
stopPreviousVersion = true // default - stop the current version
promote = true // default - & make this the current version
}
}

使用 build.gradlke.kts 时这样的配置应该是什么样子?

我正在查看 AppEngine 任务,但不明白如何将其连接到正确的 Kotlin DSL 设置。

编辑

当简单地将上述 block 添加到 build.gradle.kts IntelliJ 提示:

  • 未解析的引用:端口
  • Unresolved reference :部署

当从 cml 运行 Gradle 时,我得到:

Could not open cache directory azhqxsd1d4xoovq4o5dzec6iw (/Users/test/.gradle/caches/4.5/gradle-kotlin-dsl/azhqxsd1d4xoovq4o5dzec6iw). Internal error: unable to compile script, see log for details

EDIT2

在下面添加了 pluginsbuildscript block :

val kotlinVersion                    = property("kotlin.version")
val javaVersion = "1.8"

buildscript {
repositories {
jcenter()
mavenCentral()
mavenLocal()
maven("https://plugins.gradle.org/m2/")
maven("https://repo.spring.io/milestone")
maven("https://repo.spring.io/snapshot")
}
dependencies {
classpath("com.google.cloud.tools:appengine-gradle-plugin:1.3.4")
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.0.BUILD-SNAPSHOT")
}
}

apply {
plugin("com.google.cloud.tools.appengine")
plugin("org.springframework.boot")
}

plugins {
val kotlinVersion = "1.2.0"
`war`
`idea`
id("org.jetbrains.kotlin.jvm") version kotlinVersion
id("org.jetbrains.kotlin.kapt") version kotlinVersion
id("org.jetbrains.kotlin.plugin.jpa") version kotlinVersion
id("org.jetbrains.kotlin.plugin.noarg") version kotlinVersion
id("org.jetbrains.kotlin.plugin.spring") version kotlinVersion
id("com.ewerk.gradle.plugins.querydsl") version "1.0.9"
id("io.spring.dependency-management") version "1.0.3.RELEASE"
}

EDIT3

我看到这是由 kotlinDslAccessorsReport 生成的:

/**
* Retrieves the [appengine][com.google.cloud.tools.gradle.appengine.core.AppEngineExtension] project extension.
*/
val Project.`appengine`: com.google.cloud.tools.gradle.appengine.core.AppEngineExtension get() =
extensions.getByName("appengine") as com.google.cloud.tools.gradle.appengine.core.AppEngineExtension

/**
* Configures the [appengine][com.google.cloud.tools.gradle.appengine.core.AppEngineExtension] project extension.
*/
fun Project.`appengine`(configure: com.google.cloud.tools.gradle.appengine.core.AppEngineExtension.() -> Unit): Unit =
extensions.configure("appengine", configure)

但老实说,我不知道这对我有什么帮助。

最佳答案

为了让 kotlin-dsl 在应用插件的编译时间之前生成静态访问器,您必须使用 plugins {} block 而不是 buildscript {} block 。 buildscript {} 仍然会使依赖项对脚本类路径可见,但您不会得到这些。

如您所见,插件的 Maven 坐标可能与插件 ID 不同。您可以在 settings.gradle 中使用 pluginManagement 处理此问题规范(Android 插件的示例是 here 。这是我的处理方式(并使用 war 插件进行最小应用程序):

build.gradle,kts

plugins {
id("com.google.cloud.tools.appengine") version "1.3.4"
`war`
}

settings.gradle

pluginManagement {
repositories {
gradlePluginPortal()
google()
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == "com.google.cloud.tools.appengine") {
useModule("com.google.cloud.tools:appengine-gradle-plugin:${requested.version}")
}
}
}
}

现在,我已经应用了插件,kotlin-dsl 将在脚本编译之前生成访问器。

运行 ./gradlew kotlinDslAccessorsReport 并仔细阅读它,我在输出中看到了这一点:

/**
* Retrieves the [appengine][com.google.cloud.tools.gradle.appengine.core.AppEngineExtension] project extension.
*/
val Project.`appengine`: com.google.cloud.tools.gradle.appengine.core.AppEngineExtension get() =
extensions.getByName("appengine") as com.google.cloud.tools.gradle.appengine.core.AppEngineExtension

/**
* Configures the [appengine][com.google.cloud.tools.gradle.appengine.core.AppEngineExtension] project extension.
*/
fun Project.`appengine`(configure: com.google.cloud.tools.gradle.appengine.core.AppEngineExtension.() -> Unit): Unit =
extensions.configure("appengine", configure)

现在,您可以看到 appengine { ... } 代码块将在顶层正常工作。我们只需要根据它的类型弄清楚它里面可以有什么。请注意,如果我们使用 buildscript {} 而不是 plugins {},您将不得不自己复制/粘贴这些访问器或执行类似 extensions.getByType (com.google.cloud.tools.gradle.appengine.core.AppEngineExtension::class) 在您的构建脚本中。

进行一些搜索,您可以找到 AppEngineExtension on GitHub 的源代码.不幸的是,它没有任何方法或字段。它基本上用作“扩展持有者”类,因为它添加了其他扩展herehere (可能还有其他地方)。这意味着我们需要做一些类转换技巧来配置这个对象。源代码是 IMO 真正弄清楚如何访问这些类型的对象的唯一方法。

下面展示了我们如何配置 deploy 扩展,它是 DeployExtension以及我们如何配置 run 扩展,即 RunExtension .

import com.google.cloud.tools.gradle.appengine.core.DeployExtension
import com.google.cloud.tools.gradle.appengine.standard.RunExtension

appengine {
((this as org.gradle.api.plugins.ExtensionAware).extensions.getByName("run") as RunExtension).apply {
port = 8080
}
((this as org.gradle.api.plugins.ExtensionAware).extensions.getByName("deploy") as DeployExtension).apply {
stopPreviousVersion = true // default - stop the current version
promote = true
}
}

有几种不同的方法可以完成上述操作,但这是我采用的方法。插件本身应该提供更友好的配置方法,直到 kotlin-dsl/457已解决,于是我打开an issue

关于google-app-engine - 如何使用 Kotlin DSL 配置 AppEngine Gradle 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48502220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com