Spring boot 无法运行重新打包的spring引导jar,原因是;无法打开嵌套条目";

Spring boot 无法运行重新打包的spring引导jar,原因是;无法打开嵌套条目";,spring-boot,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 (replacin

我正在为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)
我不想为不同的环境从头开始构建jar,因为它浪费时间,并且可能存在构建不一致工件的风险。 对于传统的war项目,我只是提取war,替换配置文件并重新打包。但这次使用spring boot,不知何故它不起作用。当我运行重新打包的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)
我提取了原始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")
}
有人有主意吗


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

在查找spring boot参考之后,我有了一个解决方案

  • 关闭默认的spring boot重新打包,因为我仍然需要重新打包

  • 提取传统jar并复制配置文件

  • 使用jar类型任务重新打包它
  • 使用BootRepackage类型任务组装spring引导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
    }
    

    我发现,如果您有一个名为'resources/lib'的目录,spring boot可执行JAR将假定内容已压缩,并抛出上述异常。重命名为“resources/static”对我来说很有效。

    您应该只将-0添加到存储中;不使用压缩

    $jar -cvf0m yourproject.jar META-INF/MANIFEST.MF .
    
    还有一个解决方案:


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

    您在这里到底想做什么?“配置jar”是什么意思?为不同的环境替换jar中的配置文件。例如,配置文件(如datasource.properties etcI)总是将属性文件外部化。这不是对你的主要问题的回答,但我会这么做。谢谢你,@ci_。这确实是一个很好的解决方案,但这个项目中存在一些限制因素,使我们无法采用这种方法。这与我遇到的问题完全无关:我将CD放入spring boot分解的文件夹中,然后
    jar cMvf0 attachment-module-fat.jar*
    Thank you@razvang。你说的对我有用。
    $java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar