GroovyShell和类路径

huangapple go评论73阅读模式
英文:

GroovyShell and the classpath

问题

我开始使用Groovy进行编程,我想知道当我使用GroovyShell(new File("script.groovy").text)。evaluate()时,脚本类是否可以在类路径中看到依赖项,我尝试了这个例子,但产生了以下错误。

// main.groovy

@Grapes([
    @Grab(group='org.slf4j', module='slf4j-api', version='1.6.1'),
    @Grab(group='ch.qos.logback', module='logback-classic', version='0.9.28')
])

import groovy.lang.GroovyShell;
import java.io.File;

new GroovyShell().evaluate(new File("C:\\Users\\Dev\\Desktop\\logger.groovy").text);
// logger.groovy
import org.slf4j.*
import groovy.util.logging.Slf4j
 
@Slf4j
class HelloWorldSlf4j {
    def execute() {
        log.debug 'Execute HelloWorld.'
        log.info 'Simple sample to show log field is injected.'
    }
}
 
def helloWorld = new HelloWorldSlf4j()
helloWorld.execute()
Caused by: java.lang.ClassNotFoundException: org.slf4j.Logger
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:869)
	at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:979)
	at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:967)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:348)
	at org.codehaus.groovy.transform.LogASTTransformation$AbstractLoggingStrategy.classNode(LogASTTransformation.java:346)
	... 38 more
英文:

I'm starting to program with groovy and I wanted to know when I use GroovyShell (new File ("script.groovy"). txt) .evaluate (),
do script classes see dependencies in classpath or not, i try this example and it generate the following errors.

// main.groovy

@Grapes([
    @Grab(group='org.slf4j', module='slf4j-api', version='1.6.1'),
    @Grab(group='ch.qos.logback', module='logback-classic', version='0.9.28')
])

import groovy.lang.GroovyShell;
import java.io.File;

new GroovyShell().evaluate(new File("C:\\Users\\Dev\\Desktop\\logger.groovy").text);
// logger.groovy
import org.slf4j.*
import groovy.util.logging.Slf4j
 
@Slf4j
class HelloWorldSlf4j {
    def execute() {
        log.debug 'Execute HelloWorld.'
        log.info 'Simple sample to show log field is injected.'
    }
}
 
def helloWorld = new HelloWorldSlf4j()
helloWorld.execute()
Caused by: java.lang.ClassNotFoundException: org.slf4j.Logger
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:869)
	at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:979)
	at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:967)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:348)
	at org.codehaus.groovy.transform.LogASTTransformation$AbstractLoggingStrategy.classNode(LogASTTransformation.java:346)
	... 38 more

答案1

得分: 2

如果您希望 `logger.groovy` 文件与 `main.groovy` 脚本使用相同的类加载器,您需要通过 `GroovyShell` 构造函数将其传递。您可以通过调用 `this.class.classLoader` 来获取当前的类加载器。

<!-- language: lang-groovy -->

    @Grapes([
        @Grab(group='org.slf4j', module='slf4j-api', version='1.6.1'),
        @Grab(group='ch.qos.logback', module='logback-classic', version='0.9.28')
    ])
    
    import groovy.lang.GroovyShell
    import java.io.File
    
    new GroovyShell(this.class.classLoader).evaluate(new File("logger.groovy"))

输出:

$ groovy main.groovy
16:17:11.325 [main] DEBUG HelloWorldSlf4j - 执行 HelloWorld。
16:17:11.327 [main] INFO HelloWorldSlf4j - 简单示例,显示日志字段已注入。

英文:

If you want logger.groovy file to use the same classloader as main.groovy script, you need to pass it with the GroovyShell constructor. You can get the current classloader by calling this.class.classLoader.

<!-- language: lang-groovy -->

@Grapes([
    @Grab(group=&#39;org.slf4j&#39;, module=&#39;slf4j-api&#39;, version=&#39;1.6.1&#39;),
    @Grab(group=&#39;ch.qos.logback&#39;, module=&#39;logback-classic&#39;, version=&#39;0.9.28&#39;)
])

import groovy.lang.GroovyShell
import java.io.File

new GroovyShell(this.class.classLoader).evaluate(new File(&quot;logger.groovy&quot;))

Output:

$ groovy main.groovy
16:17:11.325 [main] DEBUG HelloWorldSlf4j - Execute HelloWorld.
16:17:11.327 [main] INFO  HelloWorldSlf4j - Simple sample to show log field is injected.

huangapple
  • 本文由 发表于 2020年3月16日 23:08:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/60708434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定