spring-boot-thin-launcher(gradle版)配置介绍

spring-boot-thin-launcher github地址

  首先说明下,这是一篇基于 <b>SpringBoot 2.4.3</b> 版本的,并且使用<b>gradle</b>脚本构建的。Maven资料网上比较多,可能也是正确的,由于我在网上下载的Maven Demo研究了两个小时都没有运行起来,最终放弃了,因此并未对Maven版的做验证。

  在这吐槽下啊,网上的那些使用spring-boot-thin-launcher gradle版本的,我不知道是资料太老还是什么原因,和我在实际中操作的结果并不相同,也可能我查询资料的方式有问题,总之这个问题折腾我两天,最终才成功运行。

  废话少扯,下面进入正题!

一、项目基本配置

  项目的生成呢,目前有两个网站可以用,一个是官方的 https://start.spring.io 还有一个是阿里云提供的 https://start.aliyun.com ,阿里云的版本选择会少一些,也相对落后几个版本。这里我们直接看我按照自己的需要搭建一个项目。我使用的是<i>kotlin</i>版本gradle配置:

项目目录结构

在看下我的配置文件

settings.gradle.kts

rootProject.name = "karo"

include(":admin")

build.gradle.kts

// 主要为多模块做的准备工作

// 此段代码如果不使用 kotlin 可以不需要
buildscript {

    repositories {
        mavenLocal()
        maven("https://maven.aliyun.com/repository/gradle-plugin")
        mavenCentral()
        google()
        jcenter()
    }

    dependencies {
        classpath(kotlin("gradle-plugin", version = "1.4.30"))
    }
} // kotlin 配置结束

allprojects {

    group = "xin.hyin.admin"
    version = "1.0.0"

    repositories {
        mavenLocal()
        // 使用阿里云镜像加速
        maven("https://maven.aliyun.com/repository/public")
        mavenCentral()
        google()
        jcenter()
    }

}

subprojects {

    apply(plugin = "java")
    // 不需要 kotlin 也不需要此行
    apply(plugin = "kotlin")

    repositories {
        mavenLocal()
        mavenCentral()
    }

    dependencies {
        "implementation"(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))

        // 不使用kotin也不需要
        "implementation"(kotlin("stdlib"))
    }

    configure<JavaPluginConvention> {
        // java 版本号设置,大多数应该是1.8: JavaVersion.VERSION_1_8
        sourceCompatibility = JavaVersion.VERSION_11
    }

    // kotlin 独有的配置
    tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
        kotlinOptions {
            // 1.8 的就写 1.8
            jvmTarget = "11"
            allWarningsAsErrors = true
            javaParameters = true
            freeCompilerArgs = listOf("-Xjsr305=strict", "-Xno-param-assertions")
        }
    }

    tasks.withType<JavaCompile> {
        options.encoding = "UTF-8"
    }

}

admin目录下配置文件

build.gradle.kts

plugins {
    maven
    id("org.springframework.boot") version "2.4.3"
    id("io.spring.dependency-management") version "1.0.10.RELEASE"
    id("org.springframework.boot.experimental.thin-launcher") version "1.0.25.RELEASE"
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4")
    runtimeOnly("mysql:mysql-connector-java")

    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude("org.junit.vintage", "junit-vintage-engine")
    }
}

tasks.withType<Jar> {
    dependsOn("thinProperties")
}

tasks.withType<org.springframework.boot.gradle.tasks.bundling.BootJar> {
    // bootJar 配置,非必须
    archiveClassifier.set("boot")

    manifest {
        // 自定义配置,可不需要
        attributes("Marker" to "Boot")
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

其他代码部分就不展示了,配置部分其实按照官方文档配置也是没问题的,就是有点杂。

完整项目结构

二、编写验证代码

  1. 首先写个SpringBoot的启动类

    package xin.hyin.admin.karo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class StartApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(StartApplication.class, args);
        }
    }
    
  2. 在编写一个测试的Controller

    package xin.hyin.admin.karo.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/")
    public class IndexController {
        
        @RequestMapping
        public String index() {
            return "hellow spring boot!";
        }
    }
    
    
  1. 直接使用idea运行

  2. 运行成功后浏览器输入: http://localhost:8080

  3. 正常情况下是可以看到hellow spring boot!输出的

三、构建thinJar

  1. 构建jar包,使用下面的命令可以构建一个只有十几K的Jar包,首次可能要花上比较长的时间,后续有缓存后就会比较快了。

    使用 build下的thinJar生成的时候可能没有thin目录,建议使用这种方式


    gradle脚本
  1. 完成后会在build目录下生成相关文件

    相关文件
  1. 运行jar

    其实我主要在这这步就卡了很久,网上都是说直接运行 生成后的文件就行了,当然我不知道是不是因为插件升级修改了运行方式还是它们说的不清楚,但是我这可以明确的说,直接运行生成后的jar是不行的,比如我这里的 admin-1.0.0.jar ;admin-1.0.0-boot.jar 这个是完整的jar包,有二十多M;

    那么正确的运行方式是:

    cd thin
    java -jar spring-boot-thin-wrapper.jar --thin.archive=../libs/admin-1.0.0.jar --hin.main=xin.hyin.admin.karo.StartApplication  --thin.trace=true --thin.debug=true
    
  1. 参数说明

    --thin.archive 构建后的jar路径

    --thin.debug 和 --thin.trace 输出thinjar的相关日志

    --thin.root 下载的依赖包路径

  2. 官方参数说明

    Option Default Description
    thin.main Start-Class in MANIFEST.MF The main class to launch (for a Spring Boot app, usually the one with @SpringBootApplication)
    thin.dryrun false Only resolve and download the dependencies. Don't run any main class. N.B. any value other than "false" (even empty) is true.
    thin.offline false Switch to "offline" mode. All dependencies must be available locally (e.g. via a previous dry run) or there will be an exception.
    thin.force false Force dependency resolution to happen, even if dependencies have been computed, and marked as "computed" in thin.properties.
    thin.classpath false Only print the classpath. Don't run the main class. Two formats are supported: "path" and "properties". For backwards compatibility "true" or empty are equivalent to "path".
    thin.root ${user.home}/.m2 The location of the local jar cache, laid out as a maven repository. The launcher creates a new directory here called "repository" if it doesn't exist.
    thin.archive the same as the target archive The archive to launch. Can be used to launch a JAR file that was build with a different version of the thin launcher, for instance, or a fat jar built by Spring Boot without the thin launcher.
    thin.parent <empty> A parent archive to use for dependency management and common classpath entries. If you run two apps with the same parent, they will have a classpath that is the same, reading from left to right, until they actually differ.
    thin.location file:.,classpath:/ The path to directory containing thin properties files (as per thin.name), as a comma-separated list of resource locations (directories). These locations plus the same paths relative /META-INF will be searched.
    thin.name "thin" The name of the properties file to search for dependency specifications and overrides.
    thin.profile Comma-separated list of profiles to use to locate thin properties. E.g. if thin.profile=foo the launcher searches for files called thin.properties and thin-foo.properties.
    thin.library org.springframework.boot.experimental:spring-boot-thin-launcher:1.0.21.RELEASE A locator for the launcher library. Can be Maven coordinates (with optional maven:// prefix), or a file (with optional file:// prefix).
    thin.repo https://repo.spring.io/libs-snapshot (also contains GA releases) Base URL for the thin.library if it is in Maven form (the default).
    thin.launcher org.springframework.boot.thin.ThinJarLauncher The main class in the thin.library. If not specified it is discovered from the manifest Main-Class attribute.
    thin.parent.first true Flag to say that the class loader is "parent first" (i.e. the system class loader will be used as the default). This is the "standard" JDK class loader strategy. Setting it to false is similar to what is normally used in web containers and application servers.
    thin.parent.boot true Flag to say that the parent class loader should be the boot class loader not the "system" class loader. The boot loader normally includes the JDK classes, but not the target archive, nor any agent jars added on the command line.
    thin.debug false Flag to switch on some slightly verbose logging during the dependency resolution. Can also be switched on with debug (like in Spring Boot).
    thin.trace false Super verbose logging of all activity during the dependency resolution and launch process. Can also be switched on with trace.

    本人英文不好,翻译也就没做,但是一般只需要用到上面提到的3个就行了;这表格就很离谱了,预览是没问题的,但是发布后就成这样了,我也醉了,jianshu对markdown支持不友好,大家还是看官方下面的表格吧

以上就是使用spring-boot-thin-launcher的介绍了,主要就是启动这一步感觉官方没有说太详细,其实 https://stackoverflow.com/questions/61366152/spring-boot-thin-jar-does-nothing 这篇文章对我启发最大,也是能让项目跑起来的关键。因为构建后的jar其实MANIFEST.MF中内容是空的,开始一直想的是怎么把内容给加上去,研究了一天也没有加上去。

如果感觉还有其他不明白的地方留言,我一般一天看一次。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容