Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot running a fully executable JAR and specify -D properties

Tags:

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?

like image 335
zhuguowei Avatar asked Nov 22 '15 14:11

zhuguowei


People also ask

How do you run a spring boot executable jar?

The Spring Boot Maven Pluginimplement a custom ClassLoader to locate and load all the external jar libraries now nested inside the package. automatically find the main() method and configure it in the manifest, so we don't have to specify the main class in our java -jar command.

What is spring boot fat jar?

Spring Boot is known for its “fat” JAR deployments, where a single executable artifact contains both the application code and all of its dependencies. Boot is also widely used to develop microservices.

What is gradle bootRun?

The Spring Boot gradle plugin provides the bootRun task that allows a developer to start the application in a “developer mode” without first building a JAR file and then starting this JAR file. Thus, it's a quick way to test the latest changes you made to the codebase.


2 Answers

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).

like image 199
dunni Avatar answered Sep 28 '22 09:09

dunni


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/

like image 25
zhuguowei Avatar answered Sep 28 '22 10:09

zhuguowei