【问题标题】:Creating Jar for SpringBoot Application with JSP pages using Gradle使用 Gradle 为带有 JSP 页面的 Spring Boot 应用程序创建 Jar
【发布时间】:2018-04-04 02:44:55
【问题描述】:

我有一个 SpringBoot 应用程序,其中包含几个 JSP 页面。当我从 Eclipse 启动主类时,它运行良好。但同时我打包成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,结构如下所示。

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

【问题讨论】:

    标签: spring-boot gradle build.gradle


    【解决方案1】:

    我认为对于使用 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
    

    【讨论】:

      【解决方案2】:

      您需要在应用程序的 resources 文件夹下创建 application.properties 并定义以下属性:

      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

      【讨论】:

      • 我的问题与创建 JSP 文件无关。我的问题与使用 gradle 构建 Jar 文件有关,该文件还将在构建中容纳 JSP 文件
      猜你喜欢
      • 2019-03-15
      • 2017-08-10
      • 1970-01-01
      • 2018-09-12
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      相关资源
      最近更新 更多