Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

java - Create war file from Spring:boot project in Eclipse

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...