cosbor11
cosbor11

Reputation: 16024

How to deploy a SpringBoot webapp: war or jar?

A Spring Boot server application is fairly simple to set up using Maven. But I can't seem to figure out why I'm having trouble using it for a webapp and I can't find clear documentation on how to do what I am trying to. Perhaps, I'm trying to fit a square peg into a round hole.

Normally, one would build a war and deploy it into a standalone started Tomcat instance. My understanding is that SpringBoot let's you have an embedded tomcat instance. Then you start the server from the jar using the java -jar /path/to/target/springbootapp.jar

But when I add webapp contents, how do I deploy it? Do I have to change my <packaging>jar</packaging> to <packaging>war</packaging>? or can I still use the jar? If I do change it war, then how do I start it?

Upvotes: 1

Views: 2674

Answers (1)

Steve Park
Steve Park

Reputation: 2019

I guess you already found answer through above comments and by yourself. Here some summary

Packaging should be war on pom.xml to include static file under WEB-INF.

Below is just example

pom.xml

<packaging>war</packaging>

<dependencies>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery-ui-themes</artifactId>
        <version>1.11.3</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>datatables</artifactId>
        <version>1.10.5</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>datatables-plugins</artifactId>
        <version>ca6ec50</version>
    </dependency>

You can run from command line also with specifying launcher (JarLauncher / WarLauncher)

command line

java -cp your-target.war;some-dependency.jar org.springframework.boot.loader.WarLauncher

Also you can configure the path of resource like webjars by setting up resourceHandlerRegistry like below

WebConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.your.spring.controller")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!registry.hasMappingForPattern("/webjars/**")) {
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
        if (!registry.hasMappingForPattern("/images/**")) {
            registry.addResourceHandler("/images/**").addResourceLocations("classpath:/images/");
        }
        if (!registry.hasMappingForPattern("/css/**")) {
            registry.addResourceHandler("/css/**").addResourceLocations("classpath:/css/");
        }
        if (!registry.hasMappingForPattern("/js/**")) {
            registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
        }
    }

    @Bean
    public InternalResourceViewResolver internalViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setOrder(1);
        return viewResolver;
    }
}

Now your jsp file can have the path for the static content inside webjar like below

On jsp file

<link rel="stylesheet" type="text/css" href="webjars/jquery-ui-themes/1.11.3/smoothness/jquery-ui.css">
<link rel="stylesheet" type="text/css" href="webjars/datatables/1.10.5/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="webjars/datatables-plugins/ca6ec50/integration/foundation/dataTables.foundation.css">

<script type="text/javascript" src="webjars/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="webjars/jquery-ui/1.11.3/jquery-ui.js"></script>
<script type="text/javascript" src="webjars/datatables/1.10.5/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="webjars/datatables-plugins/ca6ec50/integration/foundation/dataTables.foundation.js"></script>

Upvotes: 1

Related Questions