简体   繁体   English

带有@ 指令的 Groovy 脚本“意外令牌”

[英]Groovy script "unexpected token" with @ directive

This has me totally baffled.这让我完全困惑。

Specs: Linux Mint 18.3, Java 11, Groovy 2.5.9.规格:Linux Mint 18.3、Java 11、Groovy 2.5.9。

It may be of interest that these files are on an NTFS-formatted partition.这些文件位于 NTFS 格式的分区上可能很有趣。

I make a simple Groovy script file with a simple @Grab:我用一个简单的@Grab 制作了一个简单的 Groovy 脚本文件:

package test;
@Grab(group='org.apache.commons', module='commons-lang3', version='3.7')
println "bye"

... fails: ...失败:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/media/chris/W10 D drive/My Documents/software projects/EclipseWorkspace/GroovyExp2020-03-08/src/test/testscript.groovy: 5: unexpected token: println @ line 5, column 1.
   println "bye"
   ^

1 error

If I comment out the @Grab line it runs OK.如果我注释掉@Grab行,它运行正常。

If I make the script like this:如果我这样编写脚本:

package test
def bosh = "jellow world"
bosh = "bash"
@Grab(group='org.apache.commons', module='commons-lang3', version='3.7')
bosh = "bish"

... the complaint, flagged for the last line there, changes to "Groovy:The current scope already contains a variable of the name bosh" (!). ...投诉,标记为最后一行,更改为“Groovy:当前范围已经包含名称为 bosh 的变量”(!)。

If I put a semicolon at the end of the Grab line:如果我在 Grab 行的末尾放一个分号:

package test;
@Grab(group='org.apache.commons', module='commons-lang3', version='3.7');
println "bye"

... the complaint becomes "Unexpected token:;" ...投诉变成“意外的令牌:;”

So far so bafflingly inexplicable.至今莫名其妙莫名其妙。 Even stranger is the fact, however, that some of my existing scripts still work OK: I can insert a @Grab line like the above, with no complaints about "unexpected token".然而,更奇怪的是,我现有的一些脚本仍然可以正常工作:我可以像上面那样插入@Grab行,而不会抱怨“意外令牌”。

This is not Eclipse-generated nonsense: I get the same issues at the Linux CLI.这不是 Eclipse 生成的废话:我在 Linux CLI 上遇到了同样的问题。

You can't just stick annotations anywhere in groovy (or Java)您不能只是在 groovy(或 Java)中的任何地方粘贴注释

You need to annotate a class, method or field您需要注释一个类、方法或字段

In your examples, you're annotating a variable assignment (which won't work), and a method call (which also won't work)在您的示例中,您正在注释变量赋值(不起作用)和方法调用(也不起作用)

Move the annotation to the top of the file, or to a class definition将注解移至文件顶部或类定义

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

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