为Spring Boot应用程序创建胖jar时发生异常,这取决于其他项目

时间:2019-05-22 11:55:27

标签: 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个单独的jars,我必须创建一个应该运行它的fat jars。

我的代码安排如下

    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,它按预期创建了一个胖子罐。

但是当我尝试运行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

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

1 个答案:

答案 0 :(得分:1)

正如@ M.Deinum和我在链接文章中所建议的那样,只需让spring boot gradle插件完成这项工作即可。在您的根文件夹中,保存settings.gradle文件,然后删除build.gradle。运行

gradle clean build

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