使用 Groovy 作为脚本语言
我更喜欢使用脚本语言来完成短期任务,例如非常简单的 http 机器人、向某处批量导入/导出数据等等......基本的一次性脚本和简单的东西。重点是,脚本语言只是编写快速程序的有效工具。至于我目前对 Groovy 的理解...
如果您要在 Groovy 中进行编程,并且您不想编写快速脚本,那么您是否会被迫回到常规 java 语法(我们知道如何做到这一点)与脚本语言相比可能会很复杂)以便做更复杂的事情?例如,如果我想做一些 http 脚本,我不是可以直接使用 java 语法来调用 Commons HttpClient 吗?对我来说,脚本语言的意义在于快速键入和较少强制的构造。还有一件事,当已经有这么多优秀的 java 库时,似乎没有任何动力去开发基于 groovy 的库,从而使 groovy 看起来是一种依赖于 Java 的语言,具有较小的脚本功能。
所以现在我想知道是否可以改用 Groovy 作为脚本语言,或者继续使用更常见的脚本语言,例如 Perl、Python 或 Ruby。
I prefer to use scripting languages for short tasks, anything such as a really simple http bot, bulk importing/exporting data to/from somewhere, etc etc... Basic throw-away scripts and simple stuff. The point being, that a scripting language is just an efficient tool to write quick programs with. As for my understanding of Groovy at this point...
If you were to program in Groovy, and you wan't to write a quick script, wouldn't you be forced to going back to regular java syntax (and we know how that can be convoluted compared to a scripting language) in order to do anything more complicated? For example, if I want to do some http scripting, wouldn't I just be right back at using java syntax to invoke Commons HttpClient? To me, the point of a scripting language is for quickly typed and less forced constructs. And here is another thing, it doesn't seem that there is any incentive for groovy based libraries to be developed when there are already so many good java one's out there, thus making groovy appear to be a Java dependent language with minor scripting features.
So right now I am wondering if I could switch to Groovy as a scripting language or continue to use a more common scripting language such as Perl, Python or Ruby.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
@Zombies,让我向您展示我最近编写的脚本中的一个简单示例:
它使用 commons-net API,但我认为您会同意它具有比同类 Java 程序更清晰的语法。因此,我不认为使用 Java API 违背了拥有脚本语言的目的。此外,它还可以帮助您利用现有的 Java API 知识,因此是一种非常务实的方法。
@Zombies, let me show you a quick example from a script I wrote recently:
It uses commons-net API, but I think you would agree that it has a much clearer syntax than comparable Java program. So I don't think using the Java APIs defeats the purpose of having a scripting language. Furthermore, it helps you leverage your existing knowledge of the Java APIs, so is a very pragmatic approach.
Groovy 的目标之一是与 Java 具有透明的互操作性。 Groovy 的设计是“一种具有脚本功能的 Java 相关语言”。然而,我不认为这些功能是次要的 - Groovy 有许多静态编程语言(例如 Java)中没有的功能。
总结一下:如果您根本不关心 Java,那么请使用更通用的脚本语言,例如 Python 或 Perl。如果您想以脚本方式使用 Java 代码库,Groovy 是一个不错的选择。
One goal of Groovy is to have transparent interoperability with Java. Groovy is by design "a Java dependent language with scripting features". However, I don't think that these features are minor - Groovy has many features that are not found in static programming languages (such as Java).
To summarize: If you don't care at all about Java, then use a more general-purpose scripting language, such as Python or Perl. If you want to use the Java code-base in a script-ish way, Groovy is a good option.
Groovy 对于脚本编写来说非常方便。最近,我需要一个脚本来将 Maven 依赖项提取到 lib 目录中,并最终得到了一个 groovy 脚本。此代码片段解析 pom 并为您提供 jar 列表。 XML 解析非常好!
Groovy can be quite handy for scripting. Recently I needed a script to fetch Maven dependencies into a
lib
directory and ended up with a groovy script. This snippet parses a pom and gives you a list of jars. Pretty sweet for XML parsing!您将“立即回到使用 Commons HttpClient”,但您将使用 Groovy 语法而不是 Java 语法来调用它。 Groovy 语法比 Java 语法更加紧凑,因此更适合脚本编写。换句话说,在 Groovy 中使用 Java 库比在 Java 中使用 Java 库所需的代码要少得多。
Groovy 库作者通常会向开发者提供“Groovier”API,而不是开发一个全新的库。现有的 Java 库。示例包括 Grails 提供的 Hibernate 构建器和 HTTP 构建器(委托给 Commons HttpClient) 。
这些 Groovy API 提供了一种比直接使用 Java API 更紧凑、更惯用的替代方法。
You would be "right back at using Commons HttpClient", but you would invoke it using Groovy syntax, not Java syntax. Groovy syntax is a lot more compact than Java syntax and therefore better suited for scripting. In other words, using Java libraries in Groovy takes a lot less code than using Java libraries in Java.
Rather than developing an entirely new library, a Groovy library author will often provide a "Groovier" API to an existing Java library. Examples include the Hibernate builder provided by Grails, and the HTTP Builder (which delegates to Commons HttpClient).
These Groovy APIs provide a more compact and idiomatic alternative to using the Java API directly.
Groovy “开箱即用”用 groovy 版本或语言构造替换了大量常见类,包括用于 XML、HTTP 请求、访问 SQL 数据库和正则表达式的类。对于大多数脚本任务,您根本不必使用 Java 库(尽管您仍然可以选择)。但是,如果您的脚本使用裸 Java 库,那么使用 Groovy 会比直接使用 Java 领先得多。 Groovy 的闪光点在于“粘合”代码,例如
设置数据结构和文件 I/O。
地图和列表允许您创建 Java 兼容的列表和地图;与 Java 类一起使用的常规 Java 对象。 Groovy 通常将带有变量声明和初始化的多行 Java 方法调用转换为单行调用。
考虑这个简短的片段将整个文件加载到字符串中:
与
异常处理通常不是脚本中的重要考虑因素,可以方便地忽略它。
Groovy 作为脚本语言的主要优势是可以直接、方便地访问现有的巨大 Java 代码库。如果这不是您的需要,groovy 仍然提供与其他语言(例如 perl、python 或 ruby)一样丰富的脚本环境。
Groovy "out of the box" replaces a large number of common classes with groovier versions or language constructs, including classes for XML, HTTP requests, accessing SQL databases and regular expressions. For most scripting tasks, you won't have to use Java libraries at all (although you'll still have that option). But if your script uses bare Java libraries, you'll be much farther ahead with Groovy over straight Java. Where Groovy shines is in the "glue" code, such as
setting up data structures and file I/O.
The map and list allow you to create Java compatible Lists and Maps; regular Java objects that work with Java classes. Groovy often turns a multi-line Java method invokation with variable declarations and initalization into a one-liner.
Consider this short snippet to load an entire file into a String:
versus
Exception handling is often not an important consideration in scripts and it can be conveniently ignored.
Groovy's main strength as a scripting language is accessing the enormous library of Java code that's out there directly and conveniently. If that's not your need, groovy still provides a scripting environment about as rich as other languages such as perl, python or ruby.
Groovy 太棒了,一旦你掌握了非常严格的语法,你就可以开始用它来做很多你刚刚“缓慢”完成的事情。
有一天,我参加了一次讨论,不知何故想知道我们的一个系统中的 GUID 有多少个字符。与其只是数数字,不如这样做更容易:
在本地或共享脚本目录中敲击它,将来它就会在那里。
Groovy rocks, once you get a hang of the very tight syntax, you start to use it for a lot of things you've just done "the slow way".
The other day I was in a discussion which somehow got to wondering how many chars we have in a GUID in one of our systems. Rather than just count the digits, it was easier to just do this:
Whack that in a local or shared scripts directory and it'll be there for the future.
仅使用:
Jenkins 脚本中的示例:
Only use:
Exemple in script Jenkins: