如 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 设置。
编辑
当简单地将上述块添加到build.gradle.kts
时,IntelliJ 会抱怨:
- 未解析的引用:端口
- 未解决的参考:部署
从 cml 运行 Gradle 时,我得到:
无法打开缓存目录 azhqxsd1d4xoovq4o5dzec6iw (/Users/test/.gradle/caches/4.5/gradle-kotlin-dsl/azhqxsd1d4xoovq4o5dzec6iw). 内部错误:无法编译脚本,详见日志
编辑2
在下面添加了plugins
和buildscript
块:
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"
}
编辑3
我看到这是由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 {}
块而不是buildscript {}
块。buildscript {}
仍将使依赖项对脚本类路径可见,但您不会获得这些依赖项。
正如您所注意到的,插件的 Maven 坐标可能与插件 ID 不同。您可以使用pluginManagement
规范在settings.gradle
中处理此问题(此处是Android插件的示例。以下是我如何处理(并使用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)
的操作。
通过一些搜索,您可以在 GitHub 上找到AppEngineExtension
的源代码。不幸的是,它没有任何方法或字段。它基本上用作"扩展持有者"类,因为其他扩展被添加到它这里和这里(可能还有其他地方)。这意味着我们需要做一些类转换技巧才能配置这个对象。源代码是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 得到解决,所以我打开了一个问题
使用 appengine gradle 插件 2.0 版本的类型安全方法:
import com.google.cloud.tools.gradle.appengine.standard.AppEngineStandardExtension
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("com.google.cloud.tools:appengine-gradle-plugin:2.0.0-rc5")
}
}
plugins {
java
war
kotlin("jvm") version "..."
}
repositories {
jcenter()
}
apply {
plugin("com.google.cloud.tools.appengine")
}
configure<AppEngineStandardExtension> {
deploy {
projectId = "..."
version = "..."
stopPreviousVersion = true // etc
}
}