23

The Spring Boot Maven and Gradle plugins can now generate full executable archives for Linux/Unix operating systems.Running a fully executable JAR is as easy as typing:

$ ./myapp.jar

My question is in this case how to set -D properties, e.g.

-Dspring.profiles.active=test

In addition, if server does not install jdk , could this fully executable jar still run?

zhuguowei
  • 8,401
  • 16
  • 70
  • 106

2 Answers2

47

There are two ways to configure properties like that:

1:

By specifying them in a separate configuration file. Spring Boot will look for a file named like JARfilename.conf which should be stored in the same folder like the JAR file. There you can add the environment variable JAVA_OPTS:

JAVA_OPTS="-Dpropertykey=propvalue"

2:

Or you can just specify the value for the environment variable in the shell before you execute the application:

JAVA_OPTS="-Dpropertykey=propvalue" ./myapp.jar

Have a look at the documentation for the complete list of available variables: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#deployment-service

Regarding your second question: To execute a JAR, you don't need a JDK, a JRE is sufficient (but you need at least that, if you don't have any java installed on the server, the application won't run).

dunni
  • 43,386
  • 10
  • 104
  • 99
  • Thanks! I tried `JAVA_OPTS="-Dspring.profiles.active=test" ./myapp.jar ` it works. But in my opinion set environment variable is only by this way: export spring_profiles_active=test. I first know use your way to set environment variables. Could you give me some documents which introduction this way? – zhuguowei Nov 22 '15 at 15:43
  • or you can add a config/application.properties file in the same directory as your jar and add the following line: 'spring.profiles.active=test' – NickGreen Jul 30 '18 at 11:19
26

By default SpringApplication will convert any command line option arguments (starting with ‘--’, e.g. --server.port=9000) to a property and add it to the Spring Environment. As mentioned above, command line properties always take precedence over other property sources.

e.g.

$ java -jar myapp.jar --spring.application.json='{"foo":"bar"}'

please see http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/

zhuguowei
  • 8,401
  • 16
  • 70
  • 106
  • `java -jar csdn-0.1.0.jar --spring.datasource.password=hldh214` perfect <3 – Jim Dec 19 '17 at 15:42
  • This was exactly what I was looking for! Since jar is executable can even run it on unix system with just `./csdn-0.1.0.jar --spring.datasource.password=hldh214`. In Spring Boot 2 the task is now `bootJar` and to add execute permission you modify the `bootJar` task in your `build.gradle` to add `launchScript()` – GameSalutes Mar 14 '18 at 18:35