如何使用Kotlin DSL配置AppEngine Gradle插件 [英] How to configure AppEngine Gradle plugin using Kotlin DSL

查看:223
本文介绍了如何使用Kotlin DSL配置AppEngine Gradle插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As documented on https://cloud.google.com/appengine/docs/standard/java/tools/gradle-reference the AppEngine Gradle plugin offers configuration like:

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时,这样的配置应如何?

How should such a configuration look like when using a build.gradlke.kts?

我一直在看AppEngine任务,但不知道将其连接到正确的Kotlin DSL设置.

I was looking at to see the AppEngine tasks but don't understand to connect this to a proper Kotlin DSL setup.

编辑

当将上述代码块简单地添加到build.gradle.kts IntelliJ时,抱怨如下:

When simply adding the above block to the build.gradle.kts IntelliJ complains with:

  • 未解决的参考:端口
  • 未解决的参考:部署

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

and when running Gradle from cml I get:

无法打开缓存目录azhqxsd1d4xoovq4o5dzec6iw(/Users/test/.gradle/caches/4.5/gradle-kotlin-dsl/azhqxsd1d4xoovq4o5dzec6iw). 内部错误:无法编译脚本,请参阅日志以获取详细信息

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块:

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)

但是说实话,我不知道这将如何进一步帮助我.

But honestly I have no idea how this could help me further.

推荐答案

为了使kotlin-dsl在为应用的插件编译之前生成静态访问器,必须使用plugins {}块而不是buildscript {}块. buildscript {}仍将使依赖项对脚本类路径可见,但是您将不会获得这些依赖关系.

In order to have kotlin-dsl generate static accessors before compile time for applied plugins you must use the plugins {} block and not the buildscript {} block. buildscript {} will still make the dependencies visible to the script classpath, but you will not get those.

您注意到,插件的Maven坐标可能与插件ID不同.您可以在settings.gradle中使用 规范(Android插件的示例为

As you noticed, the plugin's Maven coordinates that may be different than the plugin Id. You can handle this in the settings.gradle with the pluginManagement specification (an example for Android plugin is here. Here is how I handle that (and using war plugin for minimal application):

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将在脚本编译之前生成访问器.

Now, I have the plugin applied and kotlin-dsl will generate the accessors before script compilation.

运行./gradlew kotlinDslAccessorsReport并仔细阅读它,我在输出中看到以下内容:

Running ./gradlew kotlinDslAccessorsReport and perusing through it I see this in the output:

/**
 * 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)的操作.

Now, you can see that the appengine { ... } code block will work correctly in the top level. We just need to figure out what can go inside of it based on its type. Note that if we were using buildscript {} instead of plugins {}, you would have to either copy/paste these accessors yourself or do something like extensions.getByType(com.google.cloud.tools.gradle.appengine.core.AppEngineExtension::class) in your build script.

进行一些搜索,您可以找到AppEngineExtension

Doing some searching you can find the source code for AppEngineExtension on GitHub. Unfortunately, it does not have any methods or fields on it. It is basically used as an "extension holder" class, in that other extensions are added to it here and here (and probably other places). This means that we need to do some class cast tricks to be able to configure this object. The source code is IMO the only way to really figure out how to access these kinds of objects.

下面显示了如何配置deploy扩展名,该扩展名是

Below shows how we can configure the deploy extension, which is a DeployExtension and how we can configure the run extension, which is a 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 为止,所以我打开了一个问题

There are a few different ways to accomplish the above, but that is the approach I took. The plugin itself should offer friendlier methods for configuration until kotlin-dsl/457 is resolved, so I opened an issue

这篇关于如何使用Kotlin DSL配置AppEngine Gradle插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆