Blerta Dhimitri
Blerta Dhimitri

Reputation: 3473

Build single Spring Boot WAR project for deployment

I have three projects:

project-A
project-B 
project-C

In project-A I have defined the data model. Project-B is a Spring Boot project where I define operations for consuming rest API calls with RestTemplate. Project-C is also a Spring Boot project where I am exposing some REST services. The second project (B) requires the dependency for the first project (A). The third project (C) requires the dependencies for the first and second project (A, B). I have defined these dependencies in the respective projects (B, C) pom files. All three projects have pom packaging type.

Now I want to create a single Spring Boot WAR project for deployment on a tomcat server. My purpose is to have this single WAR that encapsulates all other projects (A, B, C). So I want to be able to make my rest services (project-C) accessible through this single WAR.

I tried creating a Spring Boot project with packaging type war and added the dependency for project-C (which includes the dependencies for project-A and project-B), but I am not able to deploy the generated WAR file because of a:

SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/mywarproject-war-0.0.1-SNAPSHOT]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:952)
        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1823)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.webresources.StandardRoot@af7b84d]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
        at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4842)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4974)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 10 more
Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [org.apache.catalina.webresources.JarResourceSet@517f5687]
        at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:112)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)
        at org.apache.catalina.webresources.StandardRoot.startInternal(StandardRoot.java:708)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 13 more
Caused by: java.lang.IllegalArgumentException: java.util.zip.ZipException: error in opening zip file
        at org.apache.catalina.webresources.AbstractSingleArchiveResourceSet.initInternal(AbstractSingleArchiveResourceSet.java:113)
        at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
        ... 16 more
Caused by: java.util.zip.ZipException: error in opening zip file
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(ZipFile.java:219)
        at java.util.zip.ZipFile.<init>(ZipFile.java:149)
        at java.util.jar.JarFile.<init>(JarFile.java:166)
        at java.util.jar.JarFile.<init>(JarFile.java:103)
        at org.apache.catalina.webresources.AbstractSingleArchiveResourceSet.initInternal(AbstractSingleArchiveResourceSet.java:110)

Is there a way how can I handle this approach?

Upvotes: 0

Views: 134

Answers (1)

user5327896
user5327896

Reputation:

You have to package your Spring-Boot project as a war in your pom and exclude the spring-boot-starter-tomcat dependency in the spring-boot-starter-web dependency.

<packaging>war</packaging>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

You have to do this on your REST-Services project, which is Project-C in your case.

Upvotes: 1

Related Questions