Spring Boot - Exploded project structure

[Updated: Jan 15, 2017, Created: Jan 15, 2017]

A Boot application can run in an exploded form just like a plain application runs in IDE. We can run it using Exec Maven Plugin. We can also use Spring Boot Maven plugin's run goal which can quickly compile and run our application in exploded form.

In this tutorial we are going to demonstrate both uses to understand the project structure involved.



Using mvn exec:java to run boot application

Let's create a simple boot project:

@SpringBootApplication(exclude = {JmxAutoConfiguration.class})
public class Main {
    public static void main (String[] args) {
        SpringApplication sa = new SpringApplication(Main.class);
        sa.setBannerMode(Banner.Mode.OFF);
        sa.setLogStartupInfo(false);

        ApplicationContext c = sa.run(args);
        MyObject bean = c.getBean(MyObject.class);
        bean.doSomething();
    }

    @Component
    private static class MyObject {

        public void doSomething () {
            System.out.println("-------------");
            System.out.println("working ...");
            System.out.println("-------------");
        }
    }
}

Note that, we have excluded JmxAutoConfiguration.class to reduce the logging outputs.


pom.xml

<project ....>
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.logicbig.example</groupId>
 <artifactId>boot-project-structure</artifactId>
 <version>1.0-SNAPSHOT</version>

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
 </parent>

 <properties>
  <start-class>com.logicbig.example.Main</start-class>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
  </dependency>
 </dependencies>
</project>

Note that we added the property "start-class", that's because spring-boot-starter-parent's pom.xml, has the following plugin added:

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>exec-maven-plugin</artifactId>
 <configuration>
  <mainClass>${start-class}</mainClass>
 </configuration>
</plugin>

That means if we don't provide start-class property, our mvn exec:java won't work as expected.

Let's compile and run our project using mvn exec:java

Above we got the expected output from our main class.




Using spring-boot:run

Add mvn plugin in pom.xml:

<project ...>
 .....
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.4.2.RELEASE</version>
   </plugin>
  </plugins>
 </build>
</project>

We saw in above example there's nothing special about boot project structure when it comes to run the application in exploded form. Both exec:java and spring-boot:run work in the same way. What if we want to create an executable jar? Should we use maven-assembly-plugin to create a 'fat jar'? In the next tutorial we will attempt to do that and see how maven assembly and spring-boot plugins take the different approaches to create an executable jar.





Example Project

Dependencies and Technologies Used :

  • spring-boot-starter 1.4.2.RELEASE: Core starter, including auto-configuration support, logging and YAML.
    Corresponding Spring version: 4.3.4.RELEASE
  • JDK 1.8
  • Maven 3.3.9

Exploded Project Structure Select All Download
  • boot-project-structure
    • src
      • main
        • java
          • com
            • logicbig
              • example

See Also