15

I am pretty new to Spring Boot and I have completed a application that works well on my localhost. As I have been told to deploy it outside my localhost on for example a webbhotel or simular I need to export the project as a war-file and not as a jar-file.

UPDATE!! I run the project as a Springproject generated in Spring Initialzr and using Eclipse as a IDE.

In Eclipse I have followed the steps

<packaging>war</packaging>

and

<dependencies>
    <!-- … -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- … -->
</dependencies>

from Spring Boot Referencepage https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file

In my project I use

<dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

Do I need to add the sprinng-boot-starter-tomcat dependency and add provided to that on aswell as tomcat-embed-jasper so that my dependency will be like this?

<dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
     <dependency>
    <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

When I try to export to war-file in Eclipse, Eclipse can't find my project. It can find it if I try to export Java>JAR FILE but not if I try Web>WAR FILE

Do anyone know what I am doing wrong and if it is neccesary to export to a WAR-file to deploy to a external server?

bajen micke
  • 309
  • 1
  • 7
  • 18
  • 1
    Don't use the export JAR/WAR functionality from your IDE... Use maven to build your archive. – M. Deinum Dec 20 '17 at 14:34
  • How do you export the project to WAR? Using `mvn package` or some other way? If you execute the Maven build, does it complete successfully? What do you end up seeing in the `target` folder after the Maven build is done? – Alex Savitsky Dec 20 '17 at 14:35
  • i have tried the Eclipse export function, If I use the maven function, should I use both dependencies as I mention abouve or is it enough with tomcat-embed-jasper – bajen micke Dec 20 '17 at 14:44
  • I have no knowledge on programming spring and maven from cmd. I have used Spring intlzr to create the project and eclipse for the coding and testing. All has worked fine on localhost, the program run as it should. When I try to export the project the only alternative in Eclipse working is to export as jar-file. So I have not used maven build or maven package from cmd – bajen micke Dec 20 '17 at 14:57
  • 1
    Well, try it and see what you end up with, and come back if you have any issues. You'll have to learn Maven anyway, for any serious Java development. A word of advice: edit your original post for any updates/questions/new issues (or create a new question), instead of putting it into comments – Alex Savitsky Dec 20 '17 at 15:01
  • Thanks, have updated the question. I am going to learn Maven but I don't have it at the moment. – bajen micke Dec 20 '17 at 15:21

7 Answers7

30

You need to extend ****SpringBootServletInitializer**** in your @SpringBootApplication

You don't need to add the ****sprinng-boot-starter-tomcat**** dependency for war file generation

enter image description here

Use below configuration in pom.xml

<packaging>war</packaging>

Configure Build Path for your project and select JDK

Right click on project > Run As > Maven Install

It will generate the war file inside target folder.

enter image description here

Copy this war and deploy to any web/application server (I have renamed it to demo.war).

You can access the app by using your host name followed by the port and app name.

enter image description here

Neeraj Benjwal
  • 1,271
  • 10
  • 10
  • 1
    SUPER!! Thanks for the help!! – bajen micke Dec 20 '17 at 19:39
  • The above steps all worked for me. But i added ojdbc8.jar as external jar at build path, It is not copied to lib folder of war file.So I am getting 'java.sql.SQLException: No suitable driver found for ' Exception. can any one help me here. – Gangs165700 May 21 '20 at 11:04
  • I have added ojdbc8.jar into tomcat9/lib folder. (tomcat9 is where tomcat was installed.) I do not know why but it worked for me. – Gangs165700 May 21 '20 at 11:41
1

To generate WAR file in STS (Spring Tools Suite): Run as-> Maven Install

  • If you're going to recommend the Spring Tools Suite, you should start by suggesting the OP add it. Further, as noted in the accepted answer, the packaging should be "war"; just running `mvn install` won't automatically produce a WAR without that being configured. – DavidW Sep 13 '19 at 14:13
1

Step1:- Create ServletInitializer file by extending SpringBootServletInitializer

public class ServletInitializer extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(DemoApplication.class);
}
}

Step2:- Use @SpringBootApplication in main class

@SpringBootApplication
public class DemoApplication {

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

Step3:- Package type as war in pom.xml

<packaging>war</packaging>

Step4:- Give build filename as like this

<build>
 <finalName>demo</finalName>
</build>

Step5:- Add external tomcat dependency as provided scope

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

Note:- For reference purpose, I'm adding my completed pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.7</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
 <finalName>demo</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
dheeraj kumar
  • 419
  • 4
  • 6
0

After change to <packaging>war</packaging> in pom.xml.

utilize the command: mvn package

James Powis
  • 609
  • 4
  • 16
0

You can also proceed by selecting menus File -> Export -> Web -> War file

pheromix
  • 18,213
  • 29
  • 88
  • 158
0

I generated a war file by the following steps:

  1. Add above dependency.
  2. Open command prompt.
  3. cd into your project directory.
  4. Run the command: mvn clean install.

Then a war file will be generated in the target directory.

Ameer Taweel
  • 949
  • 1
  • 11
  • 37
0

If you are using maven, you can add tag to specify the type we want to create - jar or war. After that in sts you trigger a maven build and specify the goals - clean install and specify the number of threads you want to run for the build. In case you want to skip the test case you can give skipTest =true, or if you to run from command prommpt you can give -Dmaven.test.skip=true. Once done, war file will be generated under target folder. (unless we specify a specific deployment folder)

Bhanu
  • 21
  • 5