Sunday, March 22, 2015

How to hot deploy Spring MVC - Spring Boot Application in Intelij

  • Create your first Spring MVC / Spring Boot application by cloning the    sample project provided in Spring's web site 
  •  In IntelliJ open the Maven project
  • mvn clean install 

  • Right click the Application.java class and simply run it in Intellij.

  • Notice that once you access the application from http://localhost:8080/greeting  and if you do any changes  to the files in src/main/java/resources/templates/greeting.html; those changes do not get reflected unless you do a restart.

  • However if you create a webapp folder and have an index.jsp under i.e. src/main/webapp/index.jsp  and make changes to that file; then  you will be able to dynamically see any changes done to it with a simple page refresh in the browser. Note: Its simply the files under src/main/java/resources/ that do not reflect changes. To overcome this, and to achieve class level and resource level changes to hot deploy ..

  • We are going to add the springloaded plugin. Add the plugin dependency as follows:
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <dependencies>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>springloaded</artifactId>
    <version>1.2.0.RELEASE</version>
    </dependency>
    </dependencies>
    </plugin>

  • Do a mvn clean install and make sure the jar is downloaded to your m2 repository.

  •  Next copy the downloaded jar file from the repository to your  root folder of your application.
  • In Intelij -> Run configuration -> Specify VM arguments :
       -Djavaagent:springloaded-1.2.0.RELEASE.jar -noverify
       
        note: you can also give the path like c:\\myfolder\\springloaded.jar
       so it will be like
       -Djavaagent:c:\\myfolder\\springloaded.jar -noverify.
  •  inside src/main/java/resources folder create a new file application.properties.    Its contents should have two properties :
spring.template.cache=false
spring.thymeleaf.cache=false

 
  • shut down the servers and run the application again. Do class level changes and make changes to the /java/resources/templates/greeting.html . After doing the changes make sure to run MAKE to compile the classes.

  • Changes will be reflected immediately in the browser without having to rerun the Application.java.

4 comments:

  1. these instructions came useful to me, thanks a lot man.....

    ReplyDelete
  2. Hey Duleepa.

    Thanks for blogging about this issue. I also have been attempting to figure out Spring Boot is appropriate for my clients.

    I found the documentation on Spring Boot confusion. On the one hand, it recommends that developers do not use src/main/webapp and yet I cannot make src/main/resources/public or src/main/resources/static reloaded. Do you know anything about this issue?

    ReplyDelete
  3. BTW My spike project is here https://github.com/peterpilgrim/spring-boot-spike

    ReplyDelete
  4. Thanks man... It was very helpful. Cheers

    ReplyDelete