GithubHelp home page GithubHelp logo

spring-boot-make-jar's Introduction

Embedded Tomcat JAR packaged application

On this maven project we are going to see how to configure a spring boot 1.4.3 application, that is being deployed on an embedded tomcat, is packaged as jar and uses Freemarker 2.3.25 as templeating software.

Why jar and why not to use war?

Today many projects follow, or try to follow, a microservice oriented architecture, where every one of them are applications that communicates with the other microservices through, for example, a REST API. In this context, the challenge is to make every module as much independent as can be so is easily integrable with the enviroment, this includes that it cant deploy itself without the need of having a external servlet container.

When we create an Spring Boot Application that uses an embedded Tomcat for the deployment, on the jar compiling process, Spring is going to autoconfigure some applications aspects so we only have to focus on the product development, instead of the war packagaging, where we have to do that job ourselves.

Another advantage is that, on this way, we avoid the problem of having to sync between the versions of the application and the server configurations, that means, How is the properly server configuration for a determinated version of the code? Because all is now being manage from the application.

We also have to highlight that, on this way we simplify a lot the deployment on cloud enviroments such as Heroku, Microsft Azure or AWS, making easier the continuous integration, because of the facilities that brings those platform of their adaptation to this philosophy of independency of each application.

Maven project configuration

JAR packaging

<packaging>jar</packaging>

Spring boot 1.4.3

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

Dependencies

  • Embedded Tomcat
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

With this dependency by default we are going to be able to use the embedded tomcat, and the Tomcat Servlet container version is, by default, 8.5.6, in case of needing to use another one such as Jetty or Undertown, Tomcat should be excluded of the dependency.

  • Freemarker
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

Freemarker is a templating framework that we chose for the frontend. Another alternatives are Mustache or Thymeleaf. (Velocity was deprecated since Spring Boot 1.4) When we add this dependency, the MVC view resolver will take, in the time of returning views from the controllers. the ".ftl" suffix. The ".ftl" files must be on the templates folder, automatically created.

The decision of using templates instead of JSP is because when we package the application as JAR, this last ones have some limitations. You can see mor info on those links:

Maven Spring Boot Plugin

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

This plugin will allow us to package our application as a executable jar, and run it "in-situ". Also, if we don't specify a principal class, the plugin is going to look after the class that has the following method:

public static void main(String[] args)

In the case of this example we are going to have the following:

@SpringBootApplication
public class MakejarApplication {

    public static void main(String[] args) {
        SpringApplication.run(MakejarApplication.class, args);
    }

}

Application properties

On the project application.properties we can configure the Embedded Tomcat properties. On this example we are going to see only two, that are the port and the context path where the application is going to be deployed.

server.port={PORT}
server.context-path={PATH}

If we don't configure this properties, the default port is 8080 and the path is "/".

spring-boot-make-jar's People

Contributors

fmiras avatar frandisalvo90 avatar onreadydesarrollo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.