Spring Boot Devtools by Example - java

According to the Spring Boot Devtools docs, devtools won't run in "production mode"; that is, if you execute your Spring Boot app with java -jar .., then it won't use devtools' built-in JVM magic. However, the Spring Boot starter docs only show you one way of running your Spring Boot application...via java -jar....
So first I'm wondering: How do I run my Spring Boot app in non-production mode? I know you can run your app with Spring Boot CLI (e.g. spring run), but is that the only way?
Also, the same devtools docs mention you can explicitly remove the devtools jar from your production binary by using some excludeDevtools option, but they never explain where/how to use this. Any examples/ideas here?

1) One example of running a Spring Boot app in non-production mode (i.e. not from the JAR) is from an IDE (most specifically Eclipse or IntelliJ), where you can launch the application from the main method, or using the Spring Boot launch support. This is the most practical use of Dev Tools as it allows to continuously watch the changes in your application without restarting.
2) excludeDevTools is an option of the Spring Boot Maven Plugin http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/maven-plugin/repackage-mojo.html or the Gradle plugin - see http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html#build-tool-plugins-gradle-repackage-configuration.
So in your case it would be
bootRepackage {
mainClass = 'demo.Application'
excludeDevtools = true
}

To start with Spring boot and devtools made development easier. Spring boot uses embedded servlet container ( tomcat,undertow,jetty) with this you can have production ready code even in non-production environment.
Coming to your question how to run Spring-boot application in non-production environments, we can run application directly from Spring tool suite IDE by right click -> run as Spring boot if it is local environment.
If the build tool used is maven then from command prompt mvn boot:run , if it is gradle then gradle bootRun from root folder of project location in command prompt. If you are packing your application as uber jar then Java -jar .., also if your environment is Linux you can start as a process.
Regarding ignoring devtools in production environment, Spring boot intelligently ignore devtools jar if we specify Spring-boot-devtools as optional scope in dependencies of your build tool(maven, gradle) . In gradle Spring boot gradle plugin takes care of ignoring devtools for final deployed jar or war
Update ::
To debug a Spring Boot application run with Gradle, as seen at http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-remote-debug-gradle-run596
Add this to your build.gradle file:
applicationDefaultJvmArgs = [
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005" ]
In IntelliJ Idea IDE or STS, setup a new Debug configuration and in the options select "Remote". The defaults should be fine, but pay attention to the port number and transport type ("socket"). Have fun!

Related

spring boot project not hot reloaded when code has been updated, using mvn spring-boot:run

I'm learning java spring boot, and wonder can hot reload still work if using mvn spring-boot:run to start the application. If does, how to make it works?
Have tried to update and save the .java file, however application not restarted automatically.
initialized and run the application through:
spring init --build=maven -java-version=1.8 --dependencies=web ^
--group-id=com.foo --artifact-id=myspringboot --packaging=jar myspringboot
cd myspringboot
mvn spring-boot:run
OS: windows 10, IDE: notepad, JDK: 1.8
spring-boot-maven-plugin already included in pom.xml according to Using Boot Devtools

Using Spring profile and Maven Profile

I am now to Spring profile and I have a question, if I am doing a environment specific build using maven like mvn -Ptest then do I need to provide SPRING_PROFILES_ACTIVE parameter on execution.
If I understand Spring Profile correctly, giving SPRING_PROFILES_ACTIVE will direct my spring boot application to pick up the necessary application properties/#profile beans then why do I need to do mvn -Ptest.
One point I came accross is that mvn -Ptest allows us to package our properties file accordingly but in that case, isnt using Spring Profile a better solution.
It will be great if someone can point any scenario wherein we have to use mvn -Ptest even if we are using spring profile in Java application.
I think you are confusing maven profiles with spring profiles.
Maven profiles allow you to execute builds with different build configurations. It is only used during the maven build process.
Spring profiles can allow you to load different property files and is available at runtime to do whatever you want.
Now, you may have a maven profile that executes your spring application with a spring profile set but the difference is build vs execution time.

How to redeploy spring boot application on STS built-in server

I have a Spring Boot Gradle application with apply plugin: 'war'. I run the application on STS by right click > Run As > Spring Boot App. It runs fine. Now I make a change to the code and I want to redeploy (and better automatically). The only way I know now is to stop the server and run again. Is there any faster solution?
Spring-boot provide something called devtools which is a very neat feature.
Include this starter on your gradle build
compile("org.springframework.boot:spring-boot-devtools")
This will automatically restart whenever files on the classpath changes. So you don't have to manually stop and start server.
you can see more details here.

Spring Boot runtime configuration no longer running embedded Tomcat

I'm working on an application that utilizes the Spring Boot framework. I work exclusively out of IntelliJ, in which I've created a runtime configuration for Spring Boot. This configuration is IntelliJ-based and is capable of running and debugging the application utilizing the settings specified in both the project's application.properties and pom.xml files.
Up until about a week ago, my runtime configuration (upon debugging/running) launched the Spring Boot application, which in turn launched an embedded instance of Tomcat. Last week, without making any project-level setting changes, this configuration has started to run Jetty rather than Tomcat. Has anyone seen this?
Unfortunately, I'm not at liberty to share the properties or pom files in-depth, but I will point out that I have no mention of Jetty in either, and the following are listed as dependencies:
Thanks for taking a look!

How to run Maven on startup as a daemon or standalone

How to run Maven as soon as my server boots?
Is it OK to have a script executed using cron once the server run? or is this a bad practice?
I am using Jetty and Resteasy and running them on Maven to start my webserver.
What is the best solution for when my server starts, Maven automatically starts?
The command is use is:
mvn jetty:run -e
EDIT:
I am using Amazon EC2 to launch an instance and I would like to have the maven build to run as soon as an instance is launched.
Maven builds and runs Resteasy, Jetty and a bunch of other dependencies that are stated in the pom.xml file.
You can use your OS specific init scripts approach. A very good answer for Ubuntu OS can be found here.
Usually, you use Maven as a build tool, not a deployment tool.
What are you trying to accomplish? If you just want to boot Jetty, it comes with its own init scripts which will start it on boot if you enable them.
Unless there is a convincing reason to use Maven for this, I would have your Maven build process create an "executable unit" such as an executable JAR, a WAR, etc. and then boot that at startup instead.

Categories

Resources