How to pass command line parameters to "vertx run"
Context
I'm using continuous delivery and need to checkout a git branch then launch the verticle with a parameter.For example:
- I chekcout the "demo" git branch
- I deploy it to the target
- I remotely run the verticle with "vertx run 'demo'" (this actually doesn't work, but it illustrates my needs)
The solution: use system environment variables
The documentation https://vertx.io/docs/vertx-config/java/ is mentionning it, but it was not straight to me, so here is precisely how I did:
package org.rktmb.vertx
// ...
import io.vertx.config.ConfigStoreOptions;
import io.vertx.config.ConfigRetriever;
import io.vertx.config.ConfigRetrieverOptions;
// ...
public class MainVerticle extends SyncVerticle {
@Override
public void start(Future startFuture) throws Exception {
// ...
ConfigStoreOptions jsonEnvConfig = new ConfigStoreOptions().setType("env")
.setConfig(new JsonObject().put("keys", new JsonArray().add("VXBRANCH").add("VXPORT")));
ConfigRetrieverOptions myOptions = new ConfigRetrieverOptions().addStore(jsonEnvConfig);
ConfigRetriever myConfigRetriver = ConfigRetriever.create(vertx, myOptions);
myConfigRetriver.getConfig(asyncResults -> System.out.println(asyncResults.result().encodePrettily()));
// ..
}
}
Then, when running the verticleVXPORT="123" VXBRANCH="prod" vertx run --classpath=target/test-1.0.0-SNAPSHOT-fat.jar org.rktmb.vertx.MainVerticleThis displays:
{ "VXBRANCH" : "prod", "VXPORT" : 123.0 }
If I ever change the environment variables: the rendered JSON changes accordingly.