1

我已经完成了 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 迁移之前,上述 jars 中的文件夹结构如下:

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

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

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

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

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

任何评论表示赞赏。

4

1 回答 1

2

您应该配置任务的类路径,而不是使用from将文件直接添加到 jar中。类路径上的 Jar 文件将被打包,目录将被打包在.BootJarBOOT-INF/libBOOT-INF/classes

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

这是您的完整示例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/classesBOOT-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
于 2021-07-14T22:14:10.060 回答