使用 Gradle 给Spring boot打 jar ,同时也把JSP打进去.

通常打war 包就简单了 。因为war 包是在Tomcat 里面运行, 这里就不说了。

脚本如下:

buildscript {
    ext {
        springBootVersion = 1.4.2.RELEASE
    }
    repositories {
        maven { url "http://repo.spring.io/libs-milestone" }
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    }
}

apply plugin: java
apply plugin: eclipse-wtp
apply plugin: idea
apply plugin: org.springframework.boot 
apply plugin: war

//打WAR
war {
    baseName = chexian
    version = 0.0.1-SNAPSHOT
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-milestone" }
}

configurations {
    providedRuntime
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("javax.servlet:jstl:1.2")
    runtime("mysql:mysql-connector-java")
    compile("org.springframework.boot:spring-boot-starter-jdbc")
    compile ("org.springframework.boot:spring-boot-starter-web")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    providedRuntime org.apache.tomcat.embed:tomcat-embed-jasper
}


eclipse {
    classpath {
         containers.remove(org.eclipse.jdt.launching.JRE_CONTAINER)
         containers org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8
    }
}


如果打的是jar ,因为jar 是独立运行,而不是在容器里面运行。 这jar 放到任何java 环境下,只要运行 java -jar xx.jar ,你的应用的可以运行起来。 而不需要在运行前安装容器tomcat 等. 是挺方便的。

但,用按打war 方式直接打jar 。jar 是没有办法运行的起来的。 主要的原因是JSP 问题: An executable jar will not work because of a hard coded file pattern in Tomcat.

所以在打jar 的时候,要把jsp 放在目录META-INF/resources下.

脚本如下:

buildscript {
    ext {
        springBootVersion = 1.4.2.RELEASE
    }
    repositories {
        maven { url "http://repo.spring.io/libs-milestone" }
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    }
}

apply plugin: java
apply plugin: eclipse-wtp
apply plugin: idea
apply plugin: org.springframework.boot 

//打jar 名称和版本

jar {
    baseName = chexian
    version = 0.0.1-SNAPSHOT
}

//在打jar的时候必须把webapp目录下的文件拷贝到META-INF/resources下
jar.into(META-INF/resources) {
    from(src/main/webapp)
}


sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-milestone" }
}

configurations {
    providedRuntime
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("javax.servlet:jstl:1.2")
    runtime("mysql:mysql-connector-java")
    compile("org.springframework.boot:spring-boot-starter-jdbc")
    compile ("org.springframework.boot:spring-boot-starter-web")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    providedRuntime org.apache.tomcat.embed:tomcat-embed-jasper
}


eclipse {
    classpath {
         containers.remove(org.eclipse.jdt.launching.JRE_CONTAINER)
         containers org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8
    }
}


经验分享 程序员 微信小程序 职场和发展