Arun
Arun

Reputation: 3680

Creating Jar for SpringBoot Application with JSP pages using Gradle

I have an SpringBoot Application which has few JSP pages within it. When I boot the main class from my eclipse it is working perfectly. But at the same time, when I package it as jar, the WEB-INF/jsp folder is not configured properly. I am stuck here. Request your help

Below is my Gradle script

buildscript {
    ext {
        springBootVersion = '1.5.4.RELEASE'
    }
    repositories {

        maven {
            url "https://plugins.gradle.org/m2/"
        }
}
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.arun'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

task packSSO(type: Jar) {
    manifest {
        attributes(
            'Implementation-Title': 'Arun Spring Boot Application',
            'Implementation-Version': version,
            'Built-By': System.getProperty('user.name'),
            'Built-Date': new Date(),
            'Main-Class':  'com.arun.MainGate',
            'Built-JDK': System.getProperty('java.version')
        )
    }
    sourceSets {
    main {
        resources {
            srcDirs "src/main/resources"
        }
    }
}
    baseName = project.name + '-all'
    from { 
        configurations.compile.collect { 
            it.isDirectory() ? it : zipTree(it) 
        } 
    }
    with jar
}





repositories {

        maven {
            url "https://plugins.gradle.org/m2/"
        }
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-tomcat')
    compile('org.apache.tomcat.embed:tomcat-embed-jasper')
    compile ('javax.servlet:jstl:1.2')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testImplementation 'junit:junit:4.12'
}

I exploded the created Jar and the structure looks like below.

enter image description here

so when i run java -jar SSOPage-0.0.1-SNAPSHOT.jar it couldn't find the JSP pages. What exact folder structure should i need to follow and how to package the WEB-INF in gradle ?

Upvotes: 2

Views: 1792

Answers (2)

dugsmith
dugsmith

Reputation: 89

I think you must use WAR packaging for a Spring Boot application that uses JSPs. However, you can still use it like a self-contained executable JAR file, as in $ java -jar ./build/libs/hcro-pdi-1.0.0.war.

Here's my build.gradle file from a Spring Boot 2.0.1 project that uses JSPs.

plugins {
    id 'org.springframework.boot' version '2.0.1.RELEASE'
    id "io.spring.dependency-management" version "1.0.4.RELEASE"
}

apply plugin: 'org.springframework.boot'
apply plugin: 'war'

bootWar {
    baseName = 'hcro-pdi'
    version = '1.0.0'
}

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    compile('org.springframework.boot:spring-boot-starter-jdbc')    
    compile('javax.servlet:jstl')
    compile('org.apache.tomcat.embed:tomcat-embed-jasper')

    // Devtools enable hot-reloading of JSP and static content
    compile("org.springframework.boot:spring-boot-devtools")

    compile(group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.2.0')
    compile(group: 'org.apache.commons', name: 'commons-lang3', version: '3.7')
    compile(group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.5')
    compile(group: 'joda-time', name: 'joda-time')
    compile(group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-joda')

    compileOnly('org.projectlombok:lombok')
    testCompile('org.springframework.boot:spring-boot-starter-test')

}

// When running in bootRun task, automatically reload static resources when changed
// https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html#build-tool-plugins-gradle-running-applications
bootRun {
    jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
    sourceResources sourceSets.main
}

And, you do have to add the two properties to application.properties shared by @Yogi.

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Upvotes: 1

Yogi
Yogi

Reputation: 1895

You need to create application.properties under resources folder in your application and define below properties:

spring.mvc.view.prefix: /WEB-INF/jsp/ (your path for JSP files)
spring.mvc.view.suffix: .jsp

Sample MVC examples:

  1. http://www.springboottutorial.com/creating-web-application-with-spring-boot

Upvotes: 0

Related Questions