spring - 为spring boot应用程序创建依赖于其他项目的胖jar时发生异常

标签 spring spring-boot gradle spring-boot-gradle-plugin

这是我之前的问题的跟进
Convert gradle multi project to springboot fat jar application

我有一个http servlet应用程序,它是一个多项目gradle版本,其中我的项目是一个包含gradle的HttpServlet项目,它依赖于其他两个gradle java项目。我将所有3个jar都部署在了tomcat webapps / Web-INF / lib目录中并运行它。

现在有一个要求,我必须将我的应用程序转换为Spring Boot应用程序,而不是3个单独的jar文件,我必须创建一个胖jar文件,我应该运行它。

我将代码安排如下

    Rootrepository
      - settings.gradle 
     - build.gradle
      - Springboot project
         - build.gradle
         -src….
    - OtherGardleProject1
        - Src…
    - OtherGardleProject2
        - Src…

Please see `root project’s build.gradle`

version = '1.0.0'
group = ‘myproj’
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'groovy'
apply plugin: 'eclipse'

subprojects {
    repositories {
        mavenCentral()
            }
    }

    apply plugin: 'java'

    sourceCompatibility = 1.8

    compileJava

    jar {
     from sourceSets.main.allSource
    }
}

subprojects.each { subproject ->
  evaluationDependsOn(subproject.path)
}


task mmJar(type: Jar, dependsOn: subprojects.jar) {
      manifest {
        attributes 'Main-Class': 'org.springframework.boot.loader.JarLauncher'
        attributes 'Start-Class': ‘mypackage.springboot.Application'
    }
  subprojects.each { subproject ->
    from subproject.configurations.archives.artifacts.files.collect {
      zipTree(it)
    }
  }
}


dependencies {
        compile 'junit:junit:4.12'
        compile 'org.slf4j:slf4j-api:1.7.7'
}

请参阅springboot projects’ build.gradle
buildscript {
    ext {
        spring_plugin_version = '2.1.1.RELEASE'
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$spring_plugin_version")
        classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"

    }
}

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'



dependencies {
    implementation('org.springframework.boot:spring-boot-starter-quartz')
    implementation('org.springframework.boot:spring-boot-starter-web')

    compile project(‘:otherproject1’)


    compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'

    compile group: 'commons-io', name: 'commons-io', version: '2.4'
    compile('org.springframework:spring-webmvc')

    runtimeOnly('org.springframework.boot:spring-boot-devtools')
    compileOnly('org.projectlombok:lombok:1.18.8')
    annotationProcessor('org.projectlombok:lombok:1.18.8')

    testCompile('org.springframework.boot:spring-boot-starter-test')
}

然后我运行gradle clean assemble mmJar,它按预期创建了一个胖子 jar 。

但是当我尝试运行java -jar build/libs/myproj-1.0.0.jar
Exception in thread "main" java.lang.IllegalStateException: Failed to get nested archive for entry BOOT-INF/lib/JavaEWAH-1.1.6.jar
    at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:108)
    at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchives(JarFileArchive.java:86)
    at org.springframework.boot.loader.ExecutableArchiveLauncher.getClassPathArchives(ExecutableArchiveLauncher.java:70)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:49)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.io.IOException: Unable to open nested jar file 'BOOT-INF/lib/JavaEWAH-1.1.6.jar'
    at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:256)
    at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:241)
    at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:103)
    ... 4 more
Caused by: java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/JavaEWAH-1.1.6.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:284)
    at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:264)
    at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:252)
    ... 6 more

有人可以建议我做错了什么吗?

最佳答案

正如@ M.Deinum和我在链接文章中所建议的那样,只需让spring boot gradle插件完成任务即可。在根文件夹中,保留settings.gradle文件并删除build.gradle。跑

gradle clean build

Rootrepository文件夹中。 Gradle将构建settings.gradle中包含的项目,并在Springboot project/build/libs文件夹中创建胖子

关于spring - 为spring boot应用程序创建依赖于其他项目的胖jar时发生异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56256266/

相关文章:

java - 划分服务或 JPARepositories 中的事务

spring-boot - 根据异常类型调用ContainerStoppingErrorHandler

python - 如何在发送到 Spring Boot 休息 Controller 的请求正文中保留换行符

gradle - gradle + gretty:找不到jai_core.jar

java - REST API - 客户端 session 状态与数据库 session 状态

java - Spring Hibernate,避免语句重复注册和关闭

java - Spring Data/Hibernate native 查询返回 null,即使实际查询返回结果

java - Spring存储库findBy join表

gradle - 面临gradle的问题。 Android Studio 也关闭了 Build bundle/apk

gradle - 如何指定 RoboVM Gradle 使用的临时配置文件?