I want to invoke groovy method from the given below class
package infa9
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Right...I created this groovy script LicenseInfo.groovy
inside a folder ./test/
:
package test
public class LicenseInfo {
StringBuffer licenseInformation
public LicenseInfo() {
licenseInformation = new StringBuffer()
}
public void fetchLicenseInformation( Map<String,String> params, Map env ) {
List<String> licenseList = fetchLicenses( params, env )
println "List is $licenseList"
}
public List<String> fetchLicenses( Map<String,String> params, Map env ) {
[ 'a', 'b', 'c' ]
}
}
inside the current folder ./
, I created this groovy script Test.groovy
:
// Make some params...
def params = [ name:'tim', value:'text' ]
// Fake an env Map
def env = [ something:'whatever' ]
// Load the class from the script
def liClass = new GroovyClassLoader().parseClass( new File( 'test/LicenseInfo.groovy' ) )
// Run the method
liClass.newInstance().fetchLicenseInformation( params, env )
When I execute the command
groovy Test.groovy
it prints out:
List is [a, b, c]
The positive
errors you are getting are due to the way the Groovy parser works... You cannot put the +
on the start of the next line when joining Strings, the +
has to be trailing on the previous line (as semi-colons are optional for the end of lines in groovy, there is no way for the parser to know you are adding on to the previous line)
This will work:
if (System.getProperty("os.name").contains("Win")) {
infacmdListLicensesCommand = env.get("INFA_HOME") + "/isp/bin/infacmd.bat ListLicenses -dn " +
params.get("dn") + " -un " + params.get("un") + " -pd " +
params.get("pd") + " -sdn " + params.get("sdn") + " -hp " +
params.get("dh") + ":" + params.get("dp")
}
else {
infacmdListLicensesCommand = env.get("INFA_HOME") + "/isp/bin/infacmd.sh ListLicenses -dn " +
params.get("dn") + " -un " + params.get("un") + " -pd " +
params.get("pd") + " -sdn " + params.get("sdn") + " -hp " +
params.get("dh") + ":" + params.get("dp")
}
And this would be a more Groovy way of doing the same thing:
boolean isWindows = System.getProperty("os.name").contains("Win")
// Do it as a list of 3 items for formatting purposes
infacmdListLicensesCommand = [
"$env.INFA_HOME/isp/bin/infacmd.${isWindows?'bat':'sh'} ListLicenses"
"-dn $params.dn -un $params.un -pd $params.pd -sdn $params.sdn"
"-hp $params.dh:$params.dp" ].join( ' ' ) // then join them back together
println infacmdListLicensesCommand // print it out to see it's the same as before