outofcoffee / imposter

Scriptable, multipurpose mock server. Run standalone mock servers, or embed mocks within your tests.
https://imposter.sh
Other
362 stars 59 forks source link

How to add make another jar available to the groovy scripts ? #381

Closed hilsonp closed 1 year ago

hilsonp commented 1 year ago

We are studying the migration of mocks from ReadyAPI to imposter.

From groovy, we would need XMLParser and XMLSlurper. I may ask you to add implementation "org.apache.groovy:groovy-xml:$version_groovy" to the graddle dependencies (as I think this would be really useful to all people dealing with SOAP services) but...

We will need ojdbc7.jar, com.jcraft.jsch.jar, and probably other ones as we go...

Therefore, I would prefer to have a way to add jar files as I can under C:\Program Files\SmartBear\ReadyAPI-3.45.0\bin\ext.

I searched quite a lot but couldn't find it.

Can you help ?

I'm currently launching imposter (on Win11+MS jdk11) with the following command: java -jar c:\Temp\imposter\imposter-all.jar --plugin soap --configDir my\service

When we get a work-around for #380, we will launch it with imposter up my\service -t jvm. Having a way to provide additional jar will be useful in this case.

Next step will probably to run it as a docker with imposter up my\service so the same question will apply ;-)

Thank you for the great work !

hilsonp commented 1 year ago

I found how to provide additional jars to groovy scripts launched by Imposter.

java -classpath C:\my\path\to\jar\groovy-xml-4.0.8.jar;C:\Users\me\.imposter\engines\imposter-3.17.2.jar io.gatehill.imposter.cmd.ImposterLauncher --plugin soap --configDir foo\barV1

This let us continue but I'm still interested by a better way to provide jar to imposter when launching it with a command such as imposter up my\service -t jvm and/or when running it as a docker.

Thank you for your great work !

outofcoffee commented 1 year ago

Hi @hilsonp thank you for taking the time to raise this and for your investigations so far.

One suggestion here is to make use of the plugin directory, which effectively acts as part of the classpath.

To do this, you could:

  1. create a directory containing the JAR file
  2. set the IMPOSTER_PLUGIN_DIR environment variable (or set plugin.dir in your .imposter.yaml file in the config directory) to the path above

Given this is Groovy, however, we might need to update the parent classloader it uses.

Would you mind letting us know how you get on?

hilsonp commented 1 year ago

Wonderful ! Thank you so much ! I tested both with the environment variable and the entry in the config file and the jar I put there are available to my groovy scripts.

This can be closed as this is a really clean solution.

Note for the record: The config file changed was: "C:\Users\me.imposter\config.yaml" and not .imposter.yaml

What is that .imposter.yaml ? Where is it located ?

outofcoffee commented 1 year ago

That's great news! Thanks for reporting back.

If you have an up-to-date version of the CLI, you can create a file named .imposter.yaml in the same directory as your imposter -config.yaml files. In here, you can set most of the CLI config values if you want to scope them to a single directory, instead of to your whole user.