简体   繁体   English

如何在 Netbeans 中运行 Groovy 脚本?

[英]how to run Groovy script in Netbeans?

I'm trying out NetBeans for editing Groovy code.我正在尝试使用 NetBeans 来编辑 Groovy 代码。 (I'm new to Groovy, and it's been a long time since I did any Java development). (我是Groovy新手,好久没做Java开发了)。

At some point I installed the Groovy plugin via the "plugins" tool.在某些时候,我通过“插件”工具安装了 Groovy 插件。 But it does not have the green checkmark under "active", and choosing the Groovy plugin does not make the Activate/Deactivate/Uninstall buttons available.但它在“活动”下没有绿色复选标记,并且选择 Groovy 插件不会使激活/停用/卸载按钮可用。 Oh well...那好吧...

So let's create a project...那么让我们创建一个项目...

  1. Choose "new project".选择“新项目”。 I'm choosing "Java with Maven" just because it's the first one.我选择“Java with Maven”只是因为它是第一个。
  2. Hit "Finish"...and voila, I have a project.点击“完成”...瞧,我有一个项目。

Let's create a file...让我们创建一个文件...

  1. Right click the project package in the left pane, choose "new"右键单击左窗格中的项目 package,选择“新建”
  2. Choose "Groovy Script" for a type, hit "finish"选择“Groovy Script”作为类型,点击“finish”
  3. Hey, I have a very simple script.嘿,我有一个非常简单的脚本。 Looks like it should say "Hello chris."看起来应该说“你好克里斯”。 when done.完成后。
  4. Hit the "play" button on the toolbar....things appear in the output window.点击工具栏上的“播放”按钮....事情出现在 output window 中。

But wait a minute...the output is "Hello World,".但是等一下……output 是“Hello World”。 and it should be "Hello chris."应该是“你好克里斯”。 It looks like "HelloWorld" is coming out of "Mavenproject1.java".看起来“HelloWorld”来自“Mavenproject1.java”。

How can I (or can I) just run my script from within NetBeans?我如何(或我可以)只从 NetBeans 中运行我的脚本?

UPDATE : per @andrewJames suggestion, I tried working that tutorial.更新:根据@andrewJames 的建议,我尝试使用该教程。 (It looks like the tutorial may be a bit out of date.) (看起来教程可能有点过时了。)

  1. I created a "Java Ant" project, as that was the only one that offered me the option of not creating a "Main Class File".我创建了一个“Java Ant”项目,因为这是唯一一个让我可以选择不创建“Main Class File”的项目。
  2. I created the Java form, and the Groovy class, as directed.我按照指示创建了 Java 表格和 Groovy class。
  3. When I run the project, I get an error message saying "Error running forked groovyc".当我运行该项目时,我收到一条错误消息,提示“运行分叉 groovyc 时出错”。

I get that same error whether I'm running on a machine with a groovyc executable or not, so I suspect that there's something about the configuration that I need to change in order to point to the groovyc executable.无论我是否在带有groovyc可执行文件的机器上运行,我都会遇到同样的错误,所以我怀疑我需要更改配置以指向groovyc可执行文件。

Probably something to do with the build.xml file...but I can't seem to figure out how to edit that to change the search path for the groovyc step.可能与build.xml文件有关......但我似乎无法弄清楚如何编辑它以更改groovyc步骤的搜索路径。

I had the same problem that NB doesn't run a Groovy script out of the IDE like Eclipse does.我遇到了同样的问题,NB 没有像 Eclipse 那样从 IDE 运行 Groovy 脚本。 I logged an issue and spent more than a year on the.netbeans-dev mailing list advocating for it.我记录了一个问题,并在 .netbeans-dev 邮件列表上花费了一年多的时间来提倡它。 Often replied to with "we'll be happy to review your pull request" type of stuff.经常回复“我们很乐意审查您的拉取请求”之类的东西。 Eventually, when I rage quit the mailing list with a scathing message, Geertjan replied to the issue with a solution .最终,当我用严厉的消息愤怒地退出邮件列表时,Geertjan 用解决方案回复了这个问题。

So, in short, use the old plugin to create a Groovy project and change line 26 in groovy-build.xml to因此,简而言之,使用旧插件创建一个 Groovy 项目并将groovy-build.xml中的第 26 行更改为

<groovyc srcdir="@{srcdir}" sourcepath="@{sourcepath}" destdir="@{destdir}" encoding="${source.encoding}" excludes="@{excludes}" includeAntRuntime="true" fork="false">

You can then run your script with Shift+F6.然后您可以使用 Shift+F6 运行您的脚本。

The project needs the Groovy jars added.该项目需要添加Groovy jars。 It works for me on NB 11, 12 & 13 with Groovy 2.x and Java 8, 11 & 14 so far.到目前为止,它适用于 Groovy 2.x 和 Java 8、11 和 14 的 NB 11、12 和 13。 I haven't tried it with Groovy 3.x as the groovy-all.jar is deprecated so you'll have to maven or manually manage the Groovy jars.我还没有尝试使用 Groovy 3.x,因为 groovy-all.jar 已弃用,因此您必须使用 maven 或手动管理 Groovy jars。

Also, I collaborated a bit with someone on a new plugin .此外,我还与某人合作开发了一个新插件 It works as is but new development has stalled.它按原样工作,但新的开发已经停滞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM