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配置:
![](https://img.haomeiwen.com/i3965414/d40a03efed8545ee.png)
在看下我的配置文件
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()
}
其他代码部分就不展示了,配置部分其实按照官方文档配置也是没问题的,就是有点杂。
![](https://img.haomeiwen.com/i3965414/a02f7b3b561c3388.png)
二、编写验证代码
-
首先写个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); } }
-
在编写一个测试的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!"; } }
-
直接使用idea运行
-
运行成功后浏览器输入: http://localhost:8080
-
正常情况下是可以看到
hellow spring boot!
输出的
三、构建thinJar
-
构建jar包,使用下面的命令可以构建一个只有十几K的Jar包,首次可能要花上比较长的时间,后续有缓存后就会比较快了。
使用 build下的thinJar生成的时候可能没有thin目录,建议使用这种方式
gradle脚本
-
完成后会在build目录下生成相关文件
相关文件
-
运行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
-
参数说明
--thin.archive 构建后的jar路径
--thin.debug 和 --thin.trace 输出thinjar的相关日志
--thin.root 下载的依赖包路径
-
官方参数说明
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 calledthin.properties
andthin-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 optionalfile://
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 manifestMain-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
中内容是空的,开始一直想的是怎么把内容给加上去,研究了一天也没有加上去。
如果感觉还有其他不明白的地方留言,我一般一天看一次。
网友评论