栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot Gradle thin jar 实践 ClassNotFoundException

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot Gradle thin jar 实践 ClassNotFoundException

编译

这里参考了:原文地址: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替换掉。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/732649.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号