Spring Boot build and deploy the war to GAE Java 11

pravanjan palai
4 min readAug 19, 2020

I have noticed there are not enough articles on spring boot with war type deployment.

I have separated this article two parts

  1. Build Spring Boot war type with JSP templating
  2. Deploying the Spring Boot war to GAE Standard with java 11

The reason why I have written this article is most of the cases we do not see Spring Boot war type with JSP templating. The possible reason would be in the microservice world we want everything to be an executable jar and its obvious that your template or front end app would be a separate microservice. I agree with this however most of the existing applications may not want to move in this same direction.

Another reason I often see JSP is not an easy or readable templating method Spring recommends to use Thymeleaf.

As we can see this is debatable however This point when someone is getting started I think they would just need detail like can I have an example where I could build by view with JSP with Spring boot and deploy the war to my cloud or on-premise

let’s start with looking into our code here

https://github.com/pravanjan/springboot-appengine-java11

First, look in my Gradle build file

buildscript {
repositories {
mavenCentral()
maven{
url "https://plugins.gradle.org/m2/"
}
}

}

plugins {
id 'war'
id 'org.springframework.boot' version '2.2.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}


repositories {
mavenCentral()
jcenter()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
compile 'org.slf4j:jul-to-slf4j:1.7.25'
compile group: 'javax.servlet.jsp.jstl', name: 'jstl-api', version: '1.2'
providedCompile group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.2'
compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '9.0.36'

}
sourceCompatibility = 11
targetCompatibility = 11

As we see we would have added three dependencies additionally to enable JSP rendering on your application. I have listed them below and the JSP view does not work if we missed one of them in our build file. Adding detail with the list just to make sure we have them in our build file.

jstl-api >https://mvnrepository.com/artifact/org.glassfish.web/javax.servlet.jsp.jstljsp-api > https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-apitomcat-embed-jasper >https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper

Let's look at our Spring Boot main class

@SpringBootApplication

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

@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/jsp/");
bean.setSuffix(".jsp");
return bean;
}
}

In the Spring Boot main class additionally, we have @bean for resolving our view with internalResourceViewResolver

As we see the controller return “test” with intern resolve by our view resolver

That's all, with this configuration we would be able to serve the JSP page.

Now that we have an executable war lets look into the 2nd step that is deployed to GAE Standard Java 11. They require the following steps to be done.

Step 1 — Add AppEngine plugin to out build.gradle file and configure the AppEngine task

buildscript {
repositories {
mavenCentral()
maven{
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.google.cloud.tools:appengine-gradle-plugin:2.3.0'
}
}

plugins {
id 'war'
id 'org.springframework.boot' version '2.2.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}
apply plugin: "com.google.cloud.tools.appengine"


repositories {
mavenCentral()
jcenter()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
compile 'org.slf4j:jul-to-slf4j:1.7.25'
compile group: 'javax.servlet.jsp.jstl', name: 'jstl-api', version: '1.2'
providedCompile group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.2'
compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '9.0.36'

}

appengine { // App Engine tasks configuration
deploy { // deploy configuration\
stopPreviousVersion = false // default - stop the current version
promote = false // default - & make this the current version
version = 'pr'
projectId = ''
}
stage {
artifact = "${buildDir}/libs/${project.name}.war"
}

}
sourceCompatibility = 11
targetCompatibility = 11

The important thing to note here is. the artifact should have your correct war file location.

stage {
artifact = "${buildDir}/libs/${project.name}.war"
}

one last thing to add, create an app.yaml file to by adding appengine folder to your src/main directory.

The content of app.yaml

Now we are ready to deploy GAE standard with a Gradle task appengineDeploy

Cheers !!

--

--