【问题标题】:BootJar didn't create BOOT-INF folder after spring boot migration from 1.5.8 to 2.1.14-RELEASE春季启动从 1.5.8 迁移到 2.1.14-RELEASE 后,BootJar 没有创建 BOOT-INF 文件夹
【发布时间】:2021-09-23 17:13:18
【问题描述】:

我已经完成了 Spring Boot 应用程序从 1.5.8 到 2.1.14-RELEASE 的迁移,并使用 gradle 作为构建脚本。我正在使用 spring-boot-gradle-plugin 和 spring-boot-dependency-management 插件。在我们的 Spring Boot 项目中,我们通过为每个 jar 创建任务来创建多个可执行 jar 文件,如下所示

// During Migration changed from Jar to BootJar
task eurekaAppJar(type: BootJar) {
    baseName = 'eurekaJar'
    version = '0.0.1'
    println sourceSets.main.output
    manifest {
        attributes 'Main-Class': "org.springframework.boot.loader.JarLauncher"
        attributes 'Start-Class': "com.abc.abcCompany.service.eurekaApp.EurekaApplication"
        attributes 'Implementation-Version': "001"
    }
    bootJar {
        mainClassName = "com.abc.abcCompany.service.eurekaApp.EurekaApplication"
    }
    from(sourceSets.main.output) {
    }
}
// During Migration changed from Jar to BootJar
task oAuthConfigJar(type: BootJar) {
    baseName = 'oAuthConfigJar'
    version = '0.0.1'

    manifest {
        attributes 'Main-Class': "org.springframework.boot.loader.JarLauncher"
        attributes 'Start-Class': "com.abc.abcCompany.service.authserver.AuthServerApplication"
        attributes 'Implementation-Version': "001"

    }
    springBoot {
        mainClassName = "com.abcCompany.service.authserver.AuthServerApplication"
    }
    from(sourceSets.main.output) {
    }
}
// During migration changed from BootRepackage to BootJar
task eurekaBoot(type: BootJar, dependsOn: eurekaAppJar) {
    mainClassName = 'com.abc.abcCompany.service.eurekaApp.EurekaApplication'
// During migration commented the below code
//        customConfiguration = "eurekaconfiguration"
//        withJarTask = eztrackerEurekaJar
}


// During migration changed from BootRepackage to BootJar
task oAuthConfigJarBoot(type: BootJar, dependsOn: oAuthConfigJar) {
    println " Executing eztrackerApiGatewayBoot task"
    mainClassName = 'com.abc.abcCompany.service.authserver.AuthServerApplication'
// During migration commented the below code
//        customConfiguration = "zuulconfiguration"
//        withJarTask = eztrackerApiGatewayJar
}


bootJar.dependsOn = [eurekaBoot, oAuthConfigJarBoot]

bootJar.enabled = false

上述代码中任一gradle assemble执行后,都创建了两个可执行jar文件eurekaJar-0.0.1.jar、oAuthConfigJar-0.0.1.jar。

这是我的问题:

在spring boot迁移之前,上述jar中的文件夹结构如下:

eurekaJar-0.0.1.jar -- 组织 -- 元信息 -- 引导-INT -- 库 -- 依赖项(罐子) -- 类 -- 应用程序类

下面是迁移后的文件夹结构

eurekaJar-0.0.1.jar -- 组织 -- 元信息 -- 应用程序类

所以迁移后没有 BOOT-INF 文件夹和依赖项(lib 文件夹)

由于上述问题,我的可执行 jar 没有运行。

欢迎评论。

【问题讨论】:

    标签: java spring-boot gradle spring-boot-gradle-plugin spring-boot-migration-1.5.x-2.1.x


    【解决方案1】:

    您应该使用BootJar 任务中的configure the classpath,而不是使用将文件直接添加到jar 的from。类路径上的jar文件会打包到BOOT-INF/lib,目录会打包到BOOT-INF/classes。

    作为参考,你可以在自己的插件here中看到Spring Boot如何配置默认bootJar任务的classpath。从上面显示的内容来看,您可能还想使用主源集的运行时类路径。

    这是您的eurekaAppJar 的完整示例:

    import org.springframework.boot.gradle.tasks.bundling.BootJar
    
    plugins {
        id 'org.springframework.boot' version '2.1.4.RELEASE'
        id 'io.spring.dependency-management' version '1.0.11.RELEASE'
        id 'java'
    }
    
    group = 'com.example'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '11'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }
    
    test {
        useJUnitPlatform()
    }
    
    task eurekaAppJar(type: BootJar) {
        baseName = 'eurekaJar'
        version = '0.0.1'
        manifest {
            attributes 'Main-Class': "org.springframework.boot.loader.JarLauncher"
            attributes 'Start-Class': "com.abc.abcCompany.service.eurekaApp.EurekaApplication"
            attributes 'Implementation-Version': "001"
        }
        mainClassName = "com.abc.abcCompany.service.eurekaApp.EurekaApplication"
        classpath sourceSets.main.runtimeClasspath
    }
    

    然后可以使用以下命令构建此 jar:

    $ ./gradlew eurekaAppJar
    

    它的类和依赖分别封装在BOOT-INF/classes和BOOT-INF/lib中:

    $ unzip -l build/libs/eurekaJar-0.0.1.jar 
    Archive:  build/libs/eurekaJar-0.0.1.jar
      Length      Date    Time    Name
    ---------  ---------- -----   ----
            0  04-04-2019 02:23   org/
            0  04-04-2019 02:23   org/springframework/
            0  04-04-2019 02:23   org/springframework/boot/
            0  04-04-2019 02:23   org/springframework/boot/loader/
            0  04-04-2019 02:23   org/springframework/boot/loader/data/
            0  04-04-2019 02:23   org/springframework/boot/loader/jar/
            0  04-04-2019 02:23   org/springframework/boot/loader/archive/
            0  04-04-2019 02:23   org/springframework/boot/loader/util/
         2688  04-04-2019 02:23   org/springframework/boot/loader/data/RandomAccessDataFile$DataInputStream.class
         4976  04-04-2019 02:23   org/springframework/boot/loader/jar/AsciiBytes.class
          540  04-04-2019 02:23   org/springframework/boot/loader/jar/CentralDirectoryVisitor.class
         3263  04-04-2019 02:23   org/springframework/boot/loader/data/RandomAccessDataFile$FileAccess.class
         4015  04-04-2019 02:23   org/springframework/boot/loader/data/RandomAccessDataFile.class
          945  04-04-2019 02:23   org/springframework/boot/loader/archive/Archive.class
          282  04-04-2019 02:23   org/springframework/boot/loader/data/RandomAccessDataFile$1.class
         1593  04-04-2019 02:23   org/springframework/boot/loader/jar/JarFileEntries$1.class
          299  04-04-2019 02:23   org/springframework/boot/loader/jar/JarEntryFilter.class
          485  04-04-2019 02:23   org/springframework/boot/loader/data/RandomAccessData.class
          616  04-04-2019 02:23   org/springframework/boot/loader/jar/Bytes.class
          702  04-04-2019 02:23   org/springframework/boot/loader/jar/JarURLConnection$1.class
         9854  04-04-2019 02:23   org/springframework/boot/loader/jar/JarURLConnection.class
         1233  04-04-2019 02:23   org/springframework/boot/loader/jar/JarFile$2.class
         1487  04-04-2019 02:23   org/springframework/boot/loader/archive/ExplodedArchive$FileEntryIterator$EntryComparator.class
         3837  04-04-2019 02:23   org/springframework/boot/loader/archive/ExplodedArchive$FileEntryIterator.class
         1102  04-04-2019 02:23   org/springframework/boot/loader/archive/ExplodedArchive$FileEntry.class
          273  04-04-2019 02:23   org/springframework/boot/loader/archive/ExplodedArchive$1.class
         5243  04-04-2019 02:23   org/springframework/boot/loader/archive/ExplodedArchive.class
         1779  04-04-2019 02:23   org/springframework/boot/loader/archive/JarFileArchive$EntryIterator.class
         1081  04-04-2019 02:23   org/springframework/boot/loader/archive/JarFileArchive$JarFileEntry.class
         7336  04-04-2019 02:23   org/springframework/boot/loader/archive/JarFileArchive.class
        19737  04-04-2019 02:23   org/springframework/boot/loader/PropertiesLauncher.class
         1535  04-04-2019 02:23   org/springframework/boot/loader/LaunchedURLClassLoader$UseFastConnectionExceptionsEnumeration.class
         4306  04-04-2019 02:23   org/springframework/boot/loader/jar/JarURLConnection$JarEntryName.class
         5699  04-04-2019 02:23   org/springframework/boot/loader/LaunchedURLClassLoader.class
         1374  04-04-2019 02:23   org/springframework/boot/loader/jar/JarFile$JarFileType.class
         2062  04-04-2019 02:23   org/springframework/boot/loader/jar/JarFile$1.class
         2046  04-04-2019 02:23   org/springframework/boot/loader/jar/JarFileEntries$EntryIterator.class
        14010  04-04-2019 02:23   org/springframework/boot/loader/jar/JarFileEntries.class
         3116  04-04-2019 02:23   org/springframework/boot/loader/jar/CentralDirectoryEndRecord.class
         1813  04-04-2019 02:23   org/springframework/boot/loader/jar/ZipInflaterInputStream.class
          302  04-04-2019 02:23   org/springframework/boot/loader/archive/Archive$Entry.class
          437  04-04-2019 02:23   org/springframework/boot/loader/archive/Archive$EntryFilter.class
         3662  04-04-2019 02:23   org/springframework/boot/loader/jar/JarEntry.class
         5267  04-04-2019 02:23   org/springframework/boot/loader/jar/CentralDirectoryFileHeader.class
         4624  04-04-2019 02:23   org/springframework/boot/loader/jar/CentralDirectoryParser.class
        11548  04-04-2019 02:23   org/springframework/boot/loader/jar/Handler.class
         3650  04-04-2019 02:23   org/springframework/boot/loader/jar/StringSequence.class
          345  04-04-2019 02:23   org/springframework/boot/loader/jar/FileHeader.class
        15076  04-04-2019 02:23   org/springframework/boot/loader/jar/JarFile.class
         1953  04-04-2019 02:23   org/springframework/boot/loader/PropertiesLauncher$PrefixMatchingArchiveFilter.class
         1484  04-04-2019 02:23   org/springframework/boot/loader/PropertiesLauncher$ArchiveEntryFilter.class
          266  04-04-2019 02:23   org/springframework/boot/loader/PropertiesLauncher$1.class
         4684  04-04-2019 02:23   org/springframework/boot/loader/Launcher.class
         1502  04-04-2019 02:23   org/springframework/boot/loader/MainMethodRunner.class
         3608  04-04-2019 02:23   org/springframework/boot/loader/ExecutableArchiveLauncher.class
         1721  04-04-2019 02:23   org/springframework/boot/loader/WarLauncher.class
         1585  04-04-2019 02:23   org/springframework/boot/loader/JarLauncher.class
         5203  04-04-2019 02:23   org/springframework/boot/loader/util/SystemPropertyUtils.class
            0  07-15-2021 13:15   META-INF/
          288  07-15-2021 13:15   META-INF/MANIFEST.MF
            0  07-15-2021 13:15   BOOT-INF/
            0  07-15-2021 13:15   BOOT-INF/classes/
            0  07-15-2021 13:15   BOOT-INF/classes/com/
            0  07-15-2021 13:15   BOOT-INF/classes/com/example/
            0  07-15-2021 13:15   BOOT-INF/classes/com/example/so68382900/
          745  07-15-2021 13:15   BOOT-INF/classes/com/example/so68382900/DemoApplication.class
            1  07-15-2021 13:14   BOOT-INF/classes/application.properties
            0  07-15-2021 13:15   BOOT-INF/lib/
          398  07-15-2021 13:15   BOOT-INF/lib/spring-boot-starter-2.1.4.RELEASE.jar
      1262787  07-15-2021 13:15   BOOT-INF/lib/spring-boot-autoconfigure-2.1.4.RELEASE.jar
       952263  07-15-2021 13:15   BOOT-INF/lib/spring-boot-2.1.4.RELEASE.jar
          407  07-15-2021 13:15   BOOT-INF/lib/spring-boot-starter-logging-2.1.4.RELEASE.jar
        26586  06-26-2020 11:02   BOOT-INF/lib/javax.annotation-api-1.3.2.jar
      1099880  07-15-2021 13:15   BOOT-INF/lib/spring-context-5.1.6.RELEASE.jar
       369018  07-15-2021 13:15   BOOT-INF/lib/spring-aop-5.1.6.RELEASE.jar
       673302  07-15-2021 13:15   BOOT-INF/lib/spring-beans-5.1.6.RELEASE.jar
       280482  07-15-2021 13:15   BOOT-INF/lib/spring-expression-5.1.6.RELEASE.jar
      1293481  07-15-2021 13:15   BOOT-INF/lib/spring-core-5.1.6.RELEASE.jar
       301298  07-15-2021 13:15   BOOT-INF/lib/snakeyaml-1.23.jar
       290339  06-26-2020 11:01   BOOT-INF/lib/logback-classic-1.2.3.jar
        17522  07-15-2021 13:15   BOOT-INF/lib/log4j-to-slf4j-2.11.2.jar
         4589  07-15-2021 13:15   BOOT-INF/lib/jul-to-slf4j-1.7.26.jar
        23762  07-15-2021 13:15   BOOT-INF/lib/spring-jcl-5.1.6.RELEASE.jar
       471901  06-26-2020 11:01   BOOT-INF/lib/logback-core-1.2.3.jar
        41139  06-26-2020 11:01   BOOT-INF/lib/slf4j-api-1.7.26.jar
       266283  07-15-2021 13:15   BOOT-INF/lib/log4j-api-2.11.2.jar
    ---------                     -------
      7552715                     86 files
    

    【讨论】:

    • 感谢您的回复@Andy Wilkinson。由于我是 gradle 的新手,请您帮我了解定义 bootJar 任务的 classPath 的语法。我使用了如下语法 bootJar.classPath(sourceSets.main.runtimeClasspath) 并删除了 from(sourceSets.main.output) 行。它仍然没有创建 BOOT-INF、libs、classes 文件夹。你能帮我解决我遗漏的语法吗?
    • 我已经为您的eurekaAppJar 任务添加了一个完整的示例以及来自unzip -l 的一些输出,显示了BOOT-INF/classes 和BOOT-INF/lib 中的预期包装。
    • 非常感谢@Andy。我的 spring boot 迁移成功了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 2021-05-04
    • 2018-12-17
    • 2019-07-21
    • 1970-01-01
    相关资源
    最近更新 更多