使用Gradle使用JSP页面为SpringBoot应用程序创建Jar

时间:2017-10-23 14:00:46

标签: spring-boot gradle build.gradle

我有一个SpringBoot应用程序,其中包含很少的JSP页面。当我从我的日食启动主类时,它工作得很好。但与此同时,当我将其打包为jar时,WEB-INF/jsp文件夹未正确配置。我被困在这里。请求帮助

以下是我的Gradle脚本

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'
}

我展开了创建的Jar,结构如下所示。

enter image description here

所以当我运行java -jar SSOPage-0.0.1-SNAPSHOT.jar时,它无法找到JSP页面。我应该遵循什么样的文件夹结构以及如何将WEB-INF打包成gradle?

2 个答案:

答案 0 :(得分:0)

我认为必须对使用JSP的Spring Boot应用程序使用WAR包。但是,您仍然可以将其用作自包含的可执行JAR文件,如$ java -jar ./build/libs/hcro-pdi-1.0.0.war

这是我使用JSP的Spring Boot 2.0.1项目中的build.gradle文件。

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
}

而且,您必须将这两个属性添加到@Yogi共享的application.properties

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

答案 1 :(得分:-1)

您需要在应用中的 resources 文件夹下创建 application.properties ,并定义以下属性:< / p>

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

示例MVC示例:

  1. http://www.springboottutorial.com/creating-web-application-with-spring-boot
相关问题