Donnerstag, 16. April 2015

Simple setup: Spring with Boot, Maven and IntelliJ from scratch

There will never be enough tutorials about how to use Spring with an IDE. Here's another one in case of someone wants to know how to setup a development environment with IntelliJ. Especially the hot reloading features are very important and nobody wants to miss them. Here's how one can do it.


  1. Create a new maven project. Don't use archetypes.
  2. Edit the pom.xml file to use a parent from Spring Boot that does a lot of configuration for you.
       <parent>  
         <groupId>org.springframework.boot</groupId>  
         <artifactId>spring-boot-starter-parent</artifactId>  
         <version>1.1.5.RELEASE</version>  
       </parent>  
    
  3. Also, the spring boot dependency has to be added to the pom.xml.
       <dependencies>  
         <dependency>  
           <groupId>org.springframework.boot</groupId>  
           <artifactId>spring-boot-starter-web</artifactId>  
         </dependency>  
       </dependencies>  
    
  4. The main application class is configured to enable auto configuration. For further information, one should read one of the thousands of Spring tutorials.
     @Configuration  
     @ComponentScan  
     @EnableAutoConfiguration  
     public class Application {  
       public static void main(String[] args) {  
         ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);  
       }  
     }  
    
  5. Now you could add controllers and other stuff. And run the main class from your run configuration. In order to have an executable fat jar, you could use the maven assembly plugin (see one of my other posts). Class reloading with this run configuration should work out of the box.
  6. The convention seems to say one should place resources in src/main/resources/static. Placing an index.html in there will make it available via the applications root path. However, if you use src/main/webapp/ as your folder structure, you fulfill standard java web application convention and make Tomcat automatically recognizing your stuff. You then have to access your static content via /static/index.html or similar, or you can reconfigure your routes (not covered here).
  7. If you work on your static content, you want it to be reloaded automatically. However, this doesn't happen with the configuration so far. That's because of the fact that your static content will be copied into a working directory - changing the root files doesn't change their copies. There may be other ways, I successfully tried to use spring boot maven plugin.
           <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>  
    
  8. Executing the goal spring-boot:run in your IDE will now launch the application and automatically reload your content. Debugging with breakpoints and hot reloading your classes  seems to not work with this run configuration any more. But if you want to work on the backend of your application, you could run the main class like before.
If anyone knows a better way to setup a development environment, I would be curious about it, just tell me. Especially it would be nice to have only one run config for reloading static content and classes with breakpoints and stuff alltogether.

1 Kommentar:

  1. I just realised that manually reloading changed classes reloads them perfectly in the running application. Shortcut STRG-SHIFT-F9. Found the solution here
    http://stackoverflow.com/questions/23155244/spring-boot-hotswap-with-intellij-ide

    AntwortenLöschen