gradle kotlin_我对Gradle Kotlin DSL的第一印象

gradle kotlin

by Adam Arold

亚当·阿罗德(Adam Arold)

我对Gradle Kotlin DSL的第一印象 (My first impressions of Gradle Kotlin DSL)

The Kotlin DSL for writing Gradle build scripts has been around for some time. In this article, we’ll take a look at it and see how useful it is.
用于编写Gradle构建脚本的Kotlin DSL已经存在了一段时间。 在本文中,我们将对其进行研究,并了解它的用处。

The first question you might ask when someone adds a new supported language to their tool is why they introduced it in the first place. I think this is a perfectly valid question in this case as well, so let’s take a look at some of the pain points with Groovy.

当有人向其工具添加新的支持语言时,您可能会问的第一个问题是为什么他们首先引入了该语言。 在这种情况下,我认为这也是一个完全正确的问题,因此让我们看一下Groovy的一些痛点。

If you are a Java developer and you have worked with Maven before, it is probable that Gradle will be a little weird. The first problem is that we don’t just write some xml, but we write actual code. This in itself can be a huge burden for someone who is used to the declarative configuration Maven gives us.

如果您是Java开发人员,并且以前曾经使用过Maven ,那么Gradle可能会有些奇怪。 第一个问题是我们不只是编写一些xml ,而是编写实际的代码。 对于习惯了Maven为我们提供声明式配置的人来说,这本身可能是一个巨大的负担。

On top of this, the default language to use with Gradle is Groovy, which is a dynamic language. So Gradle not only takes away the declarative nature of xml, but the static nature as well.

最重要的是,与Gradle一起使用的默认语言是Groovy,它是一种动态语言。 因此Gradle不仅消除了xml的声明性,而且消除了静态性。

The IDE support for editing Groovy files is also a bit lacking, so we don’t get features like refactoring. This is especially bad when we mistype a name somewhere and it is not visible in the IDE.

还缺少用于编辑Groovy文件的IDE支持,因此我们没有诸如重构之类的功能。 当我们在某个地方输入错误的名称并且在IDE中不可见时,这尤其糟糕。

Groovy never took off, so finding developers who know the language is very hard. Therefore, most of us need to learn at least a bit of Groovy along the way, which leads to a lot of half-versed developers who tend to fumble when some language issue arises.

Groovy从未取得成功,因此要找到了解该语言的开发人员非常困难。 因此,我们大多数人在此过程中至少需要学习一些Groovy,这导致许多半熟练的开发人员在出现某种语言问题时往往会感到困惑。

Enter kotlin-dsl, which adds Kotlin support for Gradle. The rationale behind this project is that we already have superb IDE support for Kotlin, so let’s just use it instead of Groovy. Then we’ll get auto-completion, refactoring, and a statically-typed language which is suited for building DSLs. Let’s take a look at how it works.

输入kotlin-dsl ,它添加了Kotlin对Gradle的支持。 该项目背后的理由是,我们已经对Kotlin提供了出色的IDE支持,因此我们仅使用它代替Groovy。 然后,我们将获得自动完成,重构和适合构建DSL的静态类型语言。 让我们看看它是如何工作的。

快速浏览 (A quick glance)

If you check the samples in the kotlin-dsl repository, you’ll see some very good, but rather simplistic examples:

如果您在kotlin-dsl信息库中检查样本 ,您将看到一些非常好的但非常简单的示例:

Looks familiar? On the surface, kotlin-dsl is rather similar to the old Groovy config, but there are some minor differences. Let’s take a look at some of them.

看起来很熟悉? 从表面上看, kotlin-dsl与旧的Groovy配置非常相似,但是有一些细微的差异。 让我们看看其中的一些。

应用内置插件 (Applying built-in plugins)

Anything which is in the official Gradle plugin repository can be applied with the plugins block:

官方Gradle插件存储库中的任何内容都可以通过plugins块应用:

The same with kotlin-dsl looks like this:

kotlin-dsl相同,如下所示:

Wait, what happens here? Where does java and application come from? If we take a look at the source, it becomes clear:

等等,这是怎么回事? javaapplication从何而来? 如果我们看一下源代码,就会很清楚:

This clever trick lets us configure built-in plugins in a very simple way, and it is also compile-checked. Mistyped strings won’t be a problem anymore.

这个巧妙的技巧使我们能够以非常简单的方式配置内置插件,并且还对其进行了编译检查。 字符串输入错误将不再是问题。

What if I want to use a non-built-in plugin? Well, there is still the old way:

如果我想使用非内置插件怎么办? 好吧,仍然有旧方法:

and in Kotlin:

在Kotlin:

依存关系 (Dependencies)

Declaring dependencies is a very important part of a Gradle script, so let’s take a look at the differences:

声明依赖关系是Gradle脚本中非常重要的一部分,因此让我们看一下区别:

and with Kotlin:

和Kotlin:

Again, very similar, but a little more readable.

同样,非常相似,但可读性更高。

任务 (Tasks)

After plugins and dependencies, tasks might be the next in the list of important things in a Gradle config file. This is simple enough in Groovy:

在插件和依赖项之后,任务可能是Gradle配置文件中重要事项列表中的下一个任务。 在Groovy中这很简单:

but with Kotlin there is a little more magic involved:

但是与Kotlin相比,还有更多的魔力:

What happens here is that kotlin-dsl provides some delegates for us:

这里发生的是kotlin-dsl为我们提供了一些委托:

If you are interested in more examples, check here.

如果您对更多示例感兴趣,请在此处查看

So it seems that kotlin-dsl is the next big thing, right? We can finally refactor the configuration, we have code completion, everything works, so we can ditch Groovy. Well, not quite.

因此,看来kotlin-dsl是下一件大事,对吧? 我们终于可以重构配置了,我们完成了代码,一切正常,因此我们可以放弃Groovy。 好吧,不完全是。

While the samples in the kotlin-dsl project are very good and most of the articles on the internet praise kotlin-dsl, there are some very real downsides to it in practice.

尽管kotlin-dsl项目中的示例非常好,并且互联网上的大多数文章都对kotlin-dsl表示赞赏,但实际上它存在一些非常实际的缺点。

kotlin-dsl的问题 (Issues with kotlin-dsl)

缓慢的IDE支持 (Slow IDE support)

The first one I bumped into when I started using kotlin-dsl is the relative slow speed with which the IDE picks up the configuration changes. I added a buildSrc to my multiplatform project along with some objects to hold the versions and the library dependencies. For some reason it took IDEA 3 minutes to recognize the changes. I tried it on 3 different computers on 2 different platforms, but I had the same experience on all of them.

当我开始使用kotlin-dsl时遇到的第一个问题是IDE拾取配置更改的相对较慢的速度。 我将buildSrc和一些用于添加版本和库依赖项的object添加到了我的多平台项目中。 由于某种原因,IDEA花了3分钟才能识别出更改。 我在2个不同平台上的3台不同计算机上进行了尝试,但是在所有这些平台上我都有相同的经验。

Plugin configurations are equally slow. If we add a plugin using the plugins block, the plugin configuration is available in the local scope so we can configure it like in Groovy:

插件配置同样缓慢。 如果我们使用plugins块添加插件,则插件配置在本地范围内可用,因此我们可以像在Groovy中那样配置它:

In practice, the IDE support is a bit wonky. Sometimes it needs to be restarted in order for the configuration options to be visible, but it always takes minutes.

实际上,IDE支持有点奇怪。 有时需要重新启动才能使配置选项可见,但始终需要几分钟。

缺少代码示例 (Lack of code examples)

Gradle has a ton of Groovy samples which we can just copy-paste and customize to get things done. With kotlin-dsl, there are not a lot of them around, and this is worse with custom plugins. Most projects have Groovy-only examples of how to use them.

Gradle有大量的Groovy样本,我们可以将它们复制粘贴并定制以完成任务。 使用kotlin-dsl时,周围没有很多东西,而使用自定义插件则更糟。 大多数项目仅具有Groovy用法示例。

I checked dozens of project pages and I am yet to find a single one which has kotlin-dsl examples. This leads to the next problem:

我检查了数十个项目页面,但尚未找到一个包含kotlin-dsl示例的页面。 这导致下一个问题:

自定义任务类型问题 (Custom task type problems)

Sometimes when you try to use a plugin which has custom tasks, you need to take a look at the source code of the plugin to figure out the actual types it uses. For example, to convert this Groovy snippet to Kotlin:

有时,当您尝试使用具有自定义任务的插件时,需要查看该插件的源代码以找出其使用的实际类型。 例如,要将这个Groovy片段转换为Kotlin:

we need to specify the class of the task:

我们需要指定任务的类别:

and this means digging into the source code of it.

这意味着要研究它的源代码。

This might not look like a big problem, but it takes time and adds up quickly.

这看似不是一个大问题,但需要花费时间并Swift累积。

这值得么? (Is it worth it?)

We’ve seen that kotlin-dsl brings some powerful tools to the table, but they are not inherently Kotlin advantages. For example, we could have refactor support for Groovy as well, but it is just not there.

我们已经看到kotlin-dsl为该表带来了一些强大的工具,但是它们并不是Kotlin固有的优势。 例如,我们也可以对Groovy进行重构支持,但是还不存在。

On the flip side, there are some big disadvantages, which in practice will lead to a lot of head scratching when trying to configure custom plugins.

另一方面,有一些很大的缺点,实际上,在尝试配置自定义插件时,这会导致很多问题。

Overall kotlin-dsl is a very useful tool, but it is just not mature enough. I would not recommend using it until it gets adopted by more people. Most of its downsides come from this, and there is no reason to think that it won’t get better in time. We are just not there yet.

总体而言, kotlin-dsl是一个非常有用的工具,但还不够成熟。 我不建议使用它,直到更多的人采用它为止。 它的大部分缺点都来自于此,因此没有理由认为它不会及时变得更好。 我们还不在那里。

Of course if you are a pioneer type, or just hate Groovy, then by all means go forth and use kotlin-dsl with impunity!

当然,如果您是先驱者,或者只是讨厌Groovy,则一定要继续使用kotlin-dsl ,而不受惩罚!

Thanks for reading! You can read more of my articles on my blog.

谢谢阅读! 您可以在我的博客上阅读更多我的文章。

翻译自: https://www.freecodecamp.org/news/gradle-kotlin-dsl-first-impressions-38b3586f2faf/

gradle kotlin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值