简体   繁体   中英

How to pass jvm arg to java app distribution created with gradle

Gradle provides a handy application plugin which you can use like this

apply plugin: 'application'
mainClassName = "foo.bar.Baz"

then invoke "*dist" tasks and get some scripts generated to run the app

bin/
  app
  app.bat
lib/
  ...

however when i run sh app -DmyConfig=myValue the application won't get a jvm argument by name myConfig.

So how do we pass arguments trough command line?

All parameters you give the default launch script generated by Gradle are passed on like they are to the main method of your program, but there are some environment variables you can set, or you can do the setting in your main method.

So either you parse your args in main and set the system properties from your code, or you set environment variables. The default Gradle-generated launcher scripts consider three environment variables that it adds together. DEFAULT_JVM_OPTS which is set within the launcher script, JAVA_OPTS which is meant for all Java programs that respect this property and one additional environment property that is specific to your program. Just look inside the launcher script to find out its name.

As third option you can of course also use an own launcher script that does special handling of parameters that start with -D and sets them as system properties instead of giving them to your programs main method.


As your question suggests that you are working in a linux shell, one way to set an environment variable just for one call of an app would be YOUR_APP_OPTS="-DyourProp=yourVal" ./yourApp . But any way to set an environment variable would be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM