Manoj Suthar Manoj Suthar - 3 months ago 26
Groovy Question

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. Parent java project triggers some functionality in child which uses Groovy library to run the scripts. 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. How can I resolve this ? I want to avoid using
evaluate()
method.

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. All other scripts are in
scripts/StridesComputationScripts/
folder and they import the
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:

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

Answer

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);
                    }