编译
这里参考了:原文地址:weiku.co/article/135/
最后一种方式:这里我的项目遇到了一些坑。
buildscript {
ext {
springBootVersion = '2.1.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
// gradle 7.0之前的写法,如果使用的7.0之后的版本,可替换为
//plugins {
// ...
// id "io.spring.dependency-management"
//}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group 'co.weiku'
version '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '7.2.2.jre8'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-redis', version: '2.1.4.RELEASE'
compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.6.2'
compile group: 'com.alibaba', name: 'druid', version: '1.1.16'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.1.4.RELEASE'
compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '2.0.1'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '2.1.4.RELEASE'
compile group: 'org.apache.shiro', name: 'shiro-spring', version: '1.4.0'
compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.1.4.RELEASE'
compile project(':weiku-framework')
compile project(':weiku-common')
}
// 清除lib
task myClearLib(type: Delete) {
delete "$buildDir/libs/lib"
}
// 拷贝lib
task myCopyLib(type: Copy) {
from configurations.runtime
into "$buildDir/libs/lib"
}
bootJar {
//mainClassName = 'co.weiku.WeikuApplication'
// 指定jar包名称
baseName = 'weiku-web'
// 指定jar包版本名称
version = '1.0.0'
// 指定classifier名称
classifier = 'boot'
//以上名称指定完成后生成的jar包名称为weiku-web-1.0.0-boot.jar。可以不使用classifier
// 排除所有的依赖jar包。包括多模块项目中引用其他项目生成的jar。
excludes = ["*.jar"]
// lib目录的清除和复制任务
dependsOn myClearLib
dependsOn myCopyLib
// 指定依赖包的路径,运行时不再需要指定 java.ext.dir 或 loader.path 参数。
manifest {
attributes "Manifest-Version": 1.0,
'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ')
}
}
运行bootJar,生成了lib文件夹和jar文件。项目启动成功但是查看日志发现有ClassNotFoundException。😦
查看报错位置发现是
Class<?> clazz = Class.forName(className);
// 替换为
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
OK,解决!
部署
直接将jar包和生成的lib文件夹放到同级目录下,运行jar包即可。
这里需要注意的是:
- 如果没有依赖的改动(包括新增、升级、删除)之外,只需要发布jar包就可以了。
- 多模块项目如果涉及到其他模块代码的更改也是需要将lib中的jar替换掉。









