Skip to main content

Create WAR file in Spring Boot

This is just a three step simple procedure to package your application into war.
  1. Extending Main Class

    First, we extend our main class to SpringBootServletInitializer. This tells spring that your main class will be the entry point to initialize your project in server.
    1
    2
    3
    4
    5
    6
    7
    @SpringBootApplication
    public class Application extends SpringBootServletInitializer{
     
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
  2. Overriding configure method

    Next, we overload the configure method of SpringBootServletInitializer. We tell spring to build the sources from our Main class. Your final Main class should look like this:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @SpringBootApplication
    public class Application extends SpringBootServletInitializer{
     
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
     
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            return builder.sources(Application.class);
        }
    }
  3. Configure Packaging to WAR

    Finally, we tell maven to package the project into WAR. In your pom.xml, change the attribute value for packaging from jar to war
    1
    <packaging>war</packaging>
    Then try to build your project using:
    1
    mvn package
    For Gradle Project, please see this Packaging executable jar and war files in the Gradle link to configure .gradle file to build war file.
With this, you can now package your application with jar or war file. Just change the packaging to either jar or war. And also you can still run your main method in your IDE to develop the application.

Comments

Popular posts from this blog

Spring Security with JWT for REST API

Spring is considered a trusted framework in the Java ecosystem and is widely used. It’s no longer valid to refer to Spring as a framework, as it’s more of an umbrella term that covers various frameworks. One of these frameworks is Spring Security , which is a powerful and customizable authentication and authorization framework. It is considered the de facto standard for securing Spring-based applications. Despite its popularity, I must admit that when it comes to single-page applications , it’s not simple and straightforward to configure. I suspect the reason is that it started more as an MVC application -oriented framework, where webpage rendering happens on the server-side and communication is session-based. If the back end is based on Java and Spring, it makes sense to use Spring Security for authentication/authorization and configure it for stateless communication. While there are a lot of articles explaining how this is done, for me, it was still frustrating to set it up for the f...

How to answer conflict-resolution interview questions

QUESTION 1: How do you deal with conflict? People aren’t going to get along with each other all the time. It’s just a fact. Employers want to know that you can respond to conflict diplomatically, says job-search and interview coach Thea Kelley. If you’re a my-way-or-the-highway type of personality, you’re not going to get very far in the interview. Start off by emphasizing communication and respectfulness as a means to conflict resolution. For example, “I always take the person aside and discuss the issue privately. I listen actively to make sure I understand the other person’s point of view, and I work with the person to develop a solution together.” Stress that even if you both don’t completely agree on the end result, you tried to at least meet each other halfway. Pro tip: “Don’t non verbally communicate resentment when telling a story,” Kelley advises. “Aggressive body language and tone of voice can show that you harbor bad feelings.” QUESTION 2: Tell me about a time when you had ...

Programming C : Hello world

 1. Hello world #Hello World #include<stdio.h> //This line describe about to include header file of standard input and output.  void main(void) //main is a function with void datatype   { printf("Hello World"); }  Output : Hello World How "Hello, World!" program works? The  #include <stdio.h>  is a preprocessor command. This command tells compiler to include the contents of  stdio.h  (standard input and output) file in the program. The  stdio.h  file contains functions such as  scanf()  and  print()  to take input and display output respectively. If you use  printf()  function without writing  #include <stdio.h> , the program will not be compiled. The execution of a C program starts from the  main()  function. The  printf()  is a library function to send formatted output to the screen. In this program, the  printf()  displays  Hello, World!  text...