This is a base/example script that will allow you to incorporate functionality from external jar files into a Groovy stage in the pipeline. Usually, third-party jars must be "wrapped" to be used in Aspire. This requires coding a wrapper stage. However, if you only want to use a small number of methods in the jar file, this Groovy stage method can be used. The script assumes that the jar files and dependencies have been copied into the "lib" folder as part of the Aspire distribution.
Example folder structure
//======================================================================================================= // Details of Third-Party Jar //======================================================================================================= // The third party jar def basePathLangDetect = "lib/langdetect.jar"; def basePathJSONIC = "lib/jsonic-1.3.0.jar"; def basePathCatCon = "lib/tgcatcon.jar"; def basePaths = [basePathLangDetect, basePathJSONIC, basePathCatCon] as String[]; // Classes to use def classDetectorFactory = "com.cybozu.labs.langdetect.DetectorFactory"; def classJSON = "net.arnx.jsonic.JSON"; def classJSONException = "net.arnx.jsonic.JSONException"; //======================================================================================================= // Base methods to load Third-Party Classes //======================================================================================================= //There should not be major reason to change this code. //Obtains the relative path def getRelativePath = { basePath -> def path = "file://" + new File(basePath).toURI().getPath(); } // Obtains the classloader consisting of the original class loader with the jar file added. // Use this if you only have a single 3rd Party jar file. // param: path - the path to the jar file def getClassLoader = { path -> def classLoader = ClassLoader.systemClassLoader while (classLoader.parent) { classLoader = classLoader.parent } def newClassLoader = new URLClassLoader([new File(path).toString().toURL()] as URL[], classLoader); return newClassLoader; } // Obtains the classloader consisting of the original class loader with the jar files added. // Use this if you have multiple 3rd Party jar files. // param: paths - an array of the paths to the jar file def getMultiClassLoader = { paths -> def classLoader = ClassLoader.systemClassLoader while (classLoader.parent) { classLoader = classLoader.parent } ArrayList<URL> urls = new ArrayList<URL>(paths.length); for (String path:paths) { urls.add(new File(getRelativePath(path)).toString().toURL()); } def newClassLoader = new URLClassLoader(urls.toArray([] as URL[]), classLoader); return newClassLoader; } // Obtains a new instance of a class using the classloader and a constructor with an empty argument list def getClass = { cName, cLoader -> // load the class Class clazz = cLoader.loadClass(cName) return clazz.newInstance() } // Obtains a new instance of a class using the classloader and a constructor with the specified array of arguments. // paramTypes is an array of classes representing the Constructor parameters. // paramValues is an array of Objects representing the Constructor parameter values. def getClassWithArgs = { cName, cLoader, paramTypes, paramValues -> // load the class Class clazz = cLoader.loadClass(cName) return clazz.getConstructor(paramTypes).newInstance(paramValues) } // Load the class using the classloader. // Use this if you don't need an instance of the class but the script needs to be aware of it. def loadClass = { cName, cLoader -> return cLoader.loadClass(cName) } //======================================================================================================= // End Base methods to load Third-Party Classes //======================================================================================================= //======================================================================================================= // Example Usage //======================================================================================================= // Get a Class Loader for all 3rd Party Jars. Need to use the same one for all classes. classLoader = getMultiClassLoader(basePaths); // Load the extra classes required by LangDetect jsonException = loadClass(classJSONException, classLoader); json = loadClass(classJSON, classLoader); // Get a DetectorFactory and load the files from the LangDetect profiles directory detectorFactory = loadClass(classDetectorFactory, classLoader); detectorFactory.loadProfile("C:/Profiles"); // Instantiate a class with no constructor parameters catHandle = getClass(classname, classLoader); // Call a method catHandle.addServer("127.0.0.1", 6500); // Example of instantiating a class with constructor parameters (equivalent to cal = new SimpleTimeZone(0, "en")) cal = getClassWithArgs("java.util.SimpleTimeZone", classLoader, [int.class, String.class] as Class[], [0, "en"] as Object[]);