由于“无法打开嵌套条目”而导致无法运行重新包装的spring boot jar

Yug*_*hou 5 spring-boot

我正在为Spring Boot项目设置构建管道。

到目前为止,它分为三个阶段:

build: compile-->unit test-->archive the jar
deploy acceptance test: repack the jar for acc environment (replacing datasource.properties etc)
deploy uat test: repack the jar for uat environment (replacing datasource.properties etc)
Run Code Online (Sandbox Code Playgroud)

我不想为不同的环境从头开始构建jar,因为这样会浪费时间,并且可能会生成不一致的工件。对于传统战争项目,我只提取战争内容,替换配置文件并重新打包。但是这次用弹簧靴,以某种方式它不起作用。当我运行重新包装的罐子时,报告

java.lang.IllegalStateException: Unable to open nested entry 'lib/antlr-2.7.7.jar'. It has been compressed and nested jar files must be stored without compression. Please check the mechanism used to create your executable jar file
    at org.springframework.boot.loader.jar.JarFile.createJarFileFromFileEntry(JarFile.java:378)
    at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:355)
    at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:341)
    at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:108)
    at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchives(JarFileArchive.java:92)
    at org.springframework.boot.loader.ExecutableArchiveLauncher.getClassPathArchives(ExecutableArchiveLauncher.java:68)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:60)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:45)
Run Code Online (Sandbox Code Playgroud)

我提取了原始jar和重新包装的jar,但与lib文件夹没有区别。

task extractArtifact() {
    doLast {

        def outputDirName = "${buildDir}/tmp/under_config"
        def outputDir = file(outputDirName)
        assert outputDir.deleteDir()  // cleanup workspace

        def zipFile = file("${buildDir}/libs/${getArtifactName()}")

        copy {
            from zipTree(zipFile)
            into outputDir
        }

        copy {
            from file("${buildDir}/env")
            into file("${buildDir}/tmp/under_config")
        }

    }
}

task repackConfiguredArtifact(type: Zip, dependsOn: extractArtifact)  {
    archiveName = "${getArtifactName()}"
    destinationDir = file("${buildDir}/libs/${getEnv()}")
    from file("${buildDir}/tmp/under_config")
}
Run Code Online (Sandbox Code Playgroud)

有人有主意吗?

或者你们如何针对不同的环境配置jar(无需重新编译二进制文件)。

lih*_*gxu 7

您应该添加-0仅用于存储;不使用ZIP压缩

$jar -cvf0m yourproject.jar META-INF/MANIFEST.MF .
Run Code Online (Sandbox Code Playgroud)

还有另一种解决方案: 设置活动的Spring配置文件

$java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)

您可以使用application-$ {profile} .properties来指定特定于配置文件的值。

  • 这对我有用:我将 CD 放入 Spring Boot 分解的文件夹中,然后“jar cMvf0 Attachment-module-fat.jar *” (3认同)

Yug*_*hou 2

查找 spring-boot 参考后我有一个解决方案。

  1. 关闭默认的 spring boot 重新打包,因为无论如何我都需要重新打包它。

  2. 提取传统的 jar 并复制配置文件

  3. 使用jar类型任务重新打包
  4. 使用 BootRepackage 类型任务来组装 spring-boot jar。

这是代码:

bootRepackage {
    enabled = false
}

task extractArtifact() {
    doLast {

        def outputDirName = "${buildDir}/tmp/under_config"
        def outputDir = file(outputDirName)
        assert outputDir.deleteDir()  // cleanup workspace

        def zipFile = file("${buildDir}/libs/${getArtifactName()}")

        copy {
            from zipTree(zipFile)
            into outputDir
        }

        copy {
            from file("${buildDir}/env")
            into file("${buildDir}/tmp/under_config")
        }

        assert zipFile.delete()
    }
}

task clientJar(type: Jar, dependsOn: extractArtifact) {
    archiveName = "${getArtifactName()}"
    from file("${buildDir}/tmp/under_config")
}

task repackConfiguredArtifact(type: BootRepackage, dependsOn: clientJar) {
    withJarTask = clientJar
}
Run Code Online (Sandbox Code Playgroud)