在Ubuntu上使用groovy:
$ groovy --version
Groovy Version: 2.1.9 JVM: 11.0.10 Vendor: Oracle Corporation OS: Linux
我有这个脚本:
myScript.groovy
import org.apache.commons.lang.RandomStringUtils
String charset = (('A'..'Z') + ('0'..'9')).join()
Integer length = 9
String randomString1 = RandomStringUtils.random(length, charset.toCharArray())
String randomString2 = RandomStringUtils.random(length, charset.toCharArray())
String randomString3 = RandomStringUtils.random(length, charset.toCharArray())
从终端我运行它:
$ groovy sampleScript.groovy
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass$3$1 (file:/home/user/.sdkman/candidates/groovy/current/lib/groovy-2.1.9.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass$3$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/home/samples/myScript.groovy: 3: unable to resolve class org.apache.commons.lang.RandomStringUtils
@ line 3, column 1.
import org.apache.commons.lang.RandomStringUtils
^
如果我在maven中心搜索,我会得到:
https://search.maven.org/search?q=org.apache.commons.lang.RandomStringUtils
<dependency>
<groupId>io.github.showthat</groupId>
<artifactId>DHStringUtils</artifactId>
<version>0.1.5</version>
<type>aar</type>
</dependency>
然后我尝试用更新我的脚本
@Grab(group='io.github.showthat', module='DHStringUtils', version='0.1.5')
import org.apache.commons.lang.RandomStringUtils
String charset = (('A'..'Z') + ('0'..'9')).join()
Integer length = 9
String randomString1 = RandomStringUtils.random(length, charset.toCharArray())
String randomString2 = RandomStringUtils.random(length, charset.toCharArray())
String randomString3 = RandomStringUtils.random(length, charset.toCharArray())
但这给出了:
General error during conversion: Error grabbing Grapes -- [unresolved dependency: io.github.showthat#DHStringUtils;0.1.5: not found]
java.lang.RuntimeException: Error grabbing Grapes -- [unresolved dependency: io.github.showthat#DHStringUtils;0.1.5: not found]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAc
基于:https://search.maven.org/search?q=org.apache.commons.lang
我也尝试过:
@Grab(group='org.apache.directory.studio', module='org.apache.commons.lang', version='2.6')
但这给出了:
General error during conversion: Error grabbing Grapes -- [unresolved dependency: org.apache.directory.studio#org.apache.commons.lang;2.6: not found]
java.lang.RuntimeException: Error grabbing Grapes -- [unresolved dependency: org.apache.directory.studio#org.apache.commons.lang;2.6: not found]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
有什么建议吗?
猜测您从非常旧的Groovy版本继承了一些旧的脚本。随着时间的推移,apache-commons
的软件包发生了变化。这个RandomStringUtils
现在是的一部分
@Grab(group='org.apache.commons', module='commons-lang3', version='3.12.0')
包装改为:
import org.apache.commons.lang3.RandomStringUtils
(注意lang
末尾的3
(。
我建议更新deps并调整代码。如果你必须要坚持使用旧代码,您可能必须通过findjar或其他站点,并且只接受明确声明的组/工件"apache commons";。其他任何东西都可能只是复制了它,并且可能甚至不完整。所以我会远离任何非apache公共空间的东西。