如何从BOOT-INF /类中排除依赖项?

时间:2018-10-30 15:17:39

标签: java spring spring-boot gradle build.gradle

我有一个spring boot项目,尝试构建它时出现此错误:

> gradle build
:processResources
:compileJava
:classes
:jar FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':jar'.
> archive contains more than 65535 entries.

  To build this archive, please enable the zip64 extension.
  See: https://docs.gradle.org/3.5.1/dsl/org.gradle.api.tasks.bundling.Zip.html#org.gradle.api.tasks.bundling.Zip:zip64

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

好吧,我在gradle配置的jar任务中添加了zip64 = true选项。 现在它可以成功构建jar,但是当我尝试执行jar时,出现以下异常:

Exception in thread "main" java.lang.IllegalStateException: java.lang.IndexOutOfBoundsException
    at org.springframework.boot.loader.ExecutableArchiveLauncher.<init>(ExecutableArchiveLauncher.java:43)
    at org.springframework.boot.loader.JarLauncher.<init>(JarLauncher.java:37)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:58)
Caused by: java.lang.IndexOutOfBoundsException
    at org.springframework.boot.loader.jar.AsciiBytes.<init>(AsciiBytes.java:69)
    at org.springframework.boot.loader.jar.CentralDirectoryFileHeader.load(CentralDirectoryFileHeader.java:95)
    at org.springframework.boot.loader.jar.CentralDirectoryParser.parseEntries(CentralDirectoryParser.java:68)
    at org.springframework.boot.loader.jar.CentralDirectoryParser.parse(CentralDirectoryParser.java:57)
    at org.springframework.boot.loader.jar.JarFile.<init>(JarFile.java:118)
    at org.springframework.boot.loader.jar.JarFile.<init>(JarFile.java:106)
    at org.springframework.boot.loader.jar.JarFile.<init>(JarFile.java:92)
    at org.springframework.boot.loader.jar.JarFile.<init>(JarFile.java:83)
    at org.springframework.boot.loader.archive.JarFileArchive.<init>(JarFileArchive.java:61)
    at org.springframework.boot.loader.archive.JarFileArchive.<init>(JarFileArchive.java:57)
    at org.springframework.boot.loader.Launcher.createArchive(Launcher.java:129)
    at org.springframework.boot.loader.ExecutableArchiveLauncher.<init>(ExecutableArchiveLauncher.java:40)
    ... 2 more

事实证明,SpringBoot甚至不支持zip64格式,因此我不得不作进一步调查。 65535+条目来自何处?显然,这些来自依赖项,因为自 我在build.gradle文件中添加了新的依赖项。在检查zip64 jar时,我发现 所有的依赖类!在BOOT-INF / classes文件夹下。

据我了解,结构必须看起来像

BOOT-INF/
    classes/
        <only this application's compiled classes>
    libs/
        <all the dependency jars>

但是我的classes文件夹具有“提取”到它的所有依赖项jar。 (As you can see

我解压缩了jar,从该文件夹中删除了所有依赖项类,然后将其重新压缩。 (Like this这样,它可以毫无问题地运行,因此,我确定这些文件不是必需的。

有人可以帮助我,如何从结构中排除这些依赖项类?预先谢谢您!

这是我相关的gradle配置:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
apply plugin: 'org.springframework.boot'

ext.springBootVersion = '1.4.2.RELEASE'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE")
    }
}

configurations {
    provided.all*.exclude group: 'javax.servlet'
}

mainClassName = 'com.path.to.my.MainClass'

repositories { 
    mavenCentral() 
    maven { 
        url "https://repository.jboss.org/nexus/content/repositories/releases" 
    }
    maven {
        url "https://repo.eclipse.org/content/groups/releases/"
    }
}

jar {
//zip64 = true
    from {
        configurations.compile.collect { 
            it.isDirectory() ? it : zipTree(it) 
        } 
    }
    manifest {
        attributes("Main-Class": mainClassName)
    }
    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile 'aopalliance:aopalliance:1.0'   
    compile 'com.google.code.gson:gson:2.7'
    compile 'com.google.gdata:core:1.47.1'
    compile 'com.google.guava:guava:19.0'
    compile 'commons-io:commons-io:2.4'
    compile 'javax.json:javax.json-api:1.0'
    compile 'mysql:mysql-connector-java:5.1.22'
    compile 'org.apache.commons:commons-csv:1.4'
    compile 'org.flywaydb:flyway-core:4.0.3'
    compile 'org.glassfish:javax.json:1.0.4'
    ......
    testCompile 'org.springframework.boot:spring-boot-starter-test'
}

bootRun {
    addResources = true
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

0 个答案:

没有答案