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

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?

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

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/

Related

Pass Dynamic Port from Gradle boot:run of an application

I am having an application, which is running on some port(ex-8080) now when I start this application using gradlew I want to pass dynamic port to start the application?
./gradlew :testApplication:bootRun
is there anyway to pass the dynamic port here??
Add the following to build.gradle so that we can pass parameters to gradlew along to the underlying java command:
bootRun {
if (project.hasProperty('args')) {
args project.args.split(',')
}
}
Pass the arguments you would normally send to a java command (in this case, overriding the server.port) as -Pargs to gradlew:
/gradlew :testApplication:bootRun -Pargs="--server.port=8081"
What is here:
When you run java with arguments --server.port=8081, Spring Boot will override default property (e.g. Spring Boot will ignore your port in properties file, it will use value from command line
-Pargs is the way to ask bootRun to command line arguments. See details here.
See also the same question for maven.
I couldn't pass the port directly.
But if you want a workaround, do the following:
Build the application with gradle build.
Navigate in your project and open the directory build/libs
Now you have to see the jar of your project and then run this command java -jar yourJarProject.jar --server.port=8081.

Spring Boot : log4j2.xml next to app.jar not read in production environment

I'm new to Spring Boot.
Under 'resources' there are 2 files :
-- resources
-- application.properties
-- log4j2.xml
In development environment everything works fine.
In production environment, I copy both files and put them next to the app.jar :
-- app_folder
-- my-app.jar
-- application.properties
-- log4j2.xml
When I start the my-app.jar, :
application.properties is read from app_folder, as intended
log4j2.xml is read from 'resources', the one under app_folder is ignored
Shouldn't it work this way out of the box ? What am I doing wrong ?
All I had to do was putting a file named
name-of-my-spring-boot-jar-file.conf
in the same directory as the jar file itself.
Content of conf file :
JAVA_OPTS="-Dlog4j.configurationFile=/home/<user>/log4j2.xml"
Why do you assume it works that way?
It is true that according to its documentation Spring Boot will detect application.properties correctly if it is placed in the same directory as the jar file.
That being said log4j2.xml is not read by Spring Boot but by Log4J2 Framework and according to its documentation that framework oly looks for files on the classpath.
If the file is elsewhere you need to specify the path like this:
java -Dlog4j.configurationFile=path/to/log4j2.xml -jar my-app.jar
Edit:
my-app.jar is an executable, so I don't invoke the 'java' command when starting my-app.
Yes you do. Even if you're invoking it from a GUI (eg. double clicking in Explorer in Windows) it still runs java -jar my-app.jar under the hood.
Isn't the same folder the app.jar resides in considered classpath ?
Again, why would you assume that it is?
In the Java documentation (here for Java 8) in section Folders and Archive Files it clearly says that if the classes are stored in a jar, then the classpath includes only stuff from the jar (although in case of Spring Boot due to the custom classloader it also includes jars embedded in the jar, whch would normally not be the case - see Executable Jar Format).
You really should read the documentation (or at least relevant parts of it) before attempting to use any framework/library/programming language you have not used before - it will save you a lot of time in the long run.

Specifying application config file by name when Spring Boot starts up

Currently I can build my Gradle-based Spring Boot app like so:
./gradlew build && java -Dspring.config=. -jar build/libs/myapp.jar
And this works fine provided I have an application.yml in the root of my project directory.
However, I would now like to have both an application-local.yml as well as an application-dev.yml, and to specify which one to use when I build + run myapp.jar.
How can I specify either file at startup?
You can use Spring boot's capability of using Profile Specific property file.
You can specify the application yml inline with your profile name
application-[profile].yml. In your case, it would be
application-dev.yml
application-local.yml
Specify the profile you would want to use as a command line argument
-Dspring.profiles.active=dev

In Spring Boot, how do you build a JAR file for a different profile even if proper environment variables have not been set?

I'm new to Spring Boot, so bear with me. Currently, I'm working on a small app just for the purposes of learning Spring Boot. My goal is to deploy it using AWS (elastic beanstalk).
So far, I've created three application properties files:
application.properties: Properties which apply to all profiles.
application-dev.properties: Properties only for development. This includes localhost connection to DB, path to self signed key store, etc.
application-prod.properties: Properties used only for prod. This includes the prod DB details, etc.
Everything works fine when running the app locally using the dev profile since everything has been hard coded in the application-dev.properties.
However, the application-prod.properties file contains references which will be resolved through OS environment variables, such as:
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
I currently do not have the variables DB_USERNAME and DB_PASSWORD set up in my local OS, and I do not wish to do so. But when I run the following command, it obviously fails:
mvn package spring-boot:repackage -Dspring.profiles.active=prod
It fails because it's unable to find the above environment variables.
Is there any way to delay this check until I actually execute the JAR? My plan is to build the JAR locally and then copy it over to my prod server, and run it there. The prod server will definitely have these environment variables.
The workaround I found is this:
mvn package spring-boot:repackage -Dspring.profiles.active=dev
java -jar -Dspring.profiles.active=prod [jar-file-name].jar
However, this feels like a hack. And it may cause issues in the future that I can't think of right now.
You can use any value you want in those properties for prod profile. If the env varieble exists, Spring will take the value from there instead of the properties.
As explained here:
Spring Boot uses a very particular PropertySource order that is
designed to allow sensible overriding of values. Properties are
considered in the following order:
[...]
OS environment variables.
[...]
Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
So your application-prod.properties can look like this:
spring.datasource.username=willBeOverridenByEnvValue
spring.datasource.password=willBeOverridenByEnvValue
You have to make sure though that you set both SPRING_DATASOURCE_USERNAME and SPRING_DATASOURCE_PASSWORD env values in your prod server

How to specify JVM Parameters inside Spring Boot Project when executed with init.d

I have a Spring Boot project that I'm running as an executable jar started as an init.d service on Linux.
I understand I can create a .conf file with the same name as the jar file to specify JVM parameters. However, I would ideally like to "hard code" some parameters so there is no risk of the .conf file being missing. This is a real risk as each deployment will have a different jar name because of the version number.
Also I know I can set the environment variable JAVA_OPTS, but I'm not sure how to do this if I'm launching the Spring Boot as a init.d service using a symlink to the jar file.
Lastly I know I can replace the init.d script completely using the embeddedLaunchScript parameter, but I fell this overkill for what I want to accomplish and would want updates to the script in further releases.
Is there a way to specify JVM parameters in the Maven plugin or some other programmatic method?
Alternatively is there a way to hard code a static CONF file name? Looking at the embeddedLaunchScriptProperties it looks like confFolder can the folder, but not the name of the .conf file.
As of Spring boot 2.0, you can set the inlinedConfScript property of the build plugin. You can now reference a file that includes the appending or overwriting the JAVA_OPTS variable before the application starts. More details can be found in the Spring Documentation.

Resources