简体   繁体   English

将Groovy文件从一个包导入到另一个包中的另一个Groovy文件

[英]Importing a groovy file from one package to another groovy file in another package

I have a child java project which has groovy files added in classpath using eclipse. 我有一个子Java项目,使用eclipse在classpath中添加了Groovy文件。 Parent java project triggers some functionality in child which uses Groovy library to run the scripts. 父级java项目触发子级中的某些功能,该功能使用Groovy库运行脚本。 So import works fine in eclipse environment with opened child project but if I run it from command line or if I close child project then I get groovy compilation error at import statement. 因此,在带有打开的子项目的Eclipse环境中, import可以正常工作,但是如果我从命令行运行它,或者如果关闭子项目,则在导入语句时会出现常规编译错误。 How can I resolve this ? 我该如何解决? I want to avoid using evaluate() method. 我想避免使用evaluate()方法。

Following is my master groovy: 以下是我的主要技巧:

package strides_business_script
abstract class Business_Script extends Script {
//some stuff
}

Following is the another groovy: 以下是另一个常见问题:

import static strides_business_script.StridesBusiness_Script.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

String Key = Part_Product_PartDetails
boolean containsData = checkIncomingMessage(Key)
if(containsData) {

    def edgeKeyList = [PPR]
    JSONArray partDetails = appendEdgeValueToMsg(edgeKeyList,Key,vertex,messageIterator);
    //deleteMessages(Key);
    JSONObject jsonObject = constructInfoWithPropertyJSON("NAME,PRODUCTTYPE,FGTYPE,UOM,ITEMCLASSIFICATIONBYMARKET");
    jsonObject.put("PARTS",partDetails);
    send(Product_AggPO_ProductDetails,convertJSONToString(jsonObject));

}

Edit: My master script Business_Script.groovy resides in scripts/strides_business_script/ folder. 编辑:我的主脚本Business_Script.groovy驻留在scripts/strides_business_script/文件夹中。 All other scripts are in scripts/StridesComputationScripts/ folder and they import the Business_Script.groovy . 所有其他脚本都在scripts/StridesComputationScripts/文件夹中,并且它们导入Business_Script.groovy I run the application with remote debugging enabled like this: 我运行启用了远程调试的应用程序,如下所示:

java -cp "./lib/*:./scripts/strides_business_script/Business_Script.groovy" -Xdebug -Xrunjdwp:transport=dt_socket,address=6969,server=y -Dhibernate.cfg.xml.path=./conf/hibernate.cfg.xml -Dlog4j.configuration=file:./conf/log4j.properties com.biglabs.dataExtractor.dataDump.DataDumpDriver 7

and here I am trying to parse all computation scripts. 在这里,我尝试解析所有计算脚本。

for (String scriptName : files) {
                        Script script = groovyShell.parse(new File(
                                SCRIPT_PLACED_AT + Constants.SLASH
                                        + SCRIPT_FILE_FOLDER + Constants.SLASH
                                        + scriptName));
                        scriptMping.put(scriptName, script);
                    }

It throws following exception while parsing using groovy shell: 使用groovy shell进行解析时,它将引发以下异常:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/home/manoj/strides/release/strides/scripts/StridesComputationScripts/PRODUCT-script.groovy: 2: unable to resolve class strides_business_script.StridesBusiness_Script
 @ line 2, column 1.
   import static strides_business_script.Business_Script.*;
   ^

/home/manoj/strides/release/strides/scripts/StridesComputationScripts/PRODUCT-script.groovy: 2: unable to resolve class strides_business_script.StridesBusiness_Script
 @ line 2, column 1.
   import static strides_business_script.Business_Script.*;
   ^

2 errors

Fixed it by adding script path in comiler configuration: 通过在编译器配置中添加脚本路径来修复此问题:

CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
String path = SCRIPT_PLACED_AT;
if(!SCRIPT_PLACED_AT.endsWith("/")){
                        path = path+ "/";
                    }
compilerConfiguration.setClasspath(path);
GroovyShell groovyShell = new GroovyShell(
                            compilerConfiguration);
for (String scriptName : files) {
                        Script script = groovyShell.parse(new File(
                                SCRIPT_PLACED_AT + Constants.SLASH
                                        + SCRIPT_FILE_FOLDER + Constants.SLASH
                                        + scriptName));
                        scriptMping.put(scriptName, script);
                    }

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

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