java - 带有测试 Spring Boot 的 Gradle fat jar 的 NoClassDefFoundError

标签 java spring-boot build.gradle fatjar

我需要在 Gradle 项目中创建 fat jar 并进行测试,其中 Main 方法位于 src/test 下。 我现在有 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/CommandLineParser当我运行由“gradle bootJar”创建的 fat jar 时。

我的 Gradle :

plugins {
    id 'org.springframework.boot' version '2.1.7.RELEASE'
}

apply plugin: 'io.spring.dependency-management'

dependencies {
    compile group: 'org.springframework', name: 'spring-context', version: '5.1.9.RELEASE'
    compile group: 'org.springframework', name: 'spring-web', version: '5.1.9.RELEASE'
    compile group: 'org.springframework', name: 'spring-aop', version: '5.1.9.RELEASE'
    compile group: 'org.springframework', name: 'spring-aspects', version: '5.1.9.RELEASE'
    compile group: 'org.springframework', name: 'spring-test', version: '5.0.5.RELEASE'

    compile group: 'commons-cli', name: 'commons-cli', version: '1.4'
}

bootJar {
    manifest {
        attributes 'Start-Class': 'com.company.MainRunner'
    }
    from sourceSets.test.output.classesDirs
}

我的 spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-4.3.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <aop:aspectj-autoproxy/>

<bean> ... </bean>
<bean> ... </bean>

</beans>

异常(exception):

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/CommandLineParser
<小时/>

之前遵循的“gradle ShadowJar”代码运行良好,直到我将 AspectJ 添加到项目中

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.3'
    }
}

apply plugin: 'com.github.johnrengelman.shadow'

shadowJar {
    manifest {
        attributes 'Main-Class': 'com.company.MainRunner'
    }
    from sourceSets.test.output.classesDirs

    configurations = [project.configurations.testRuntime]
}

运行“gradle ShadowJar”时出现错误

Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/aop]

我发现 ShadowJar 无法与 Spring 正常工作

如何为 Spring 项目创建 fat jar,其主类位于 src/test 下?

最佳答案

这个 Gradle ShadowJar 适合我

import com.github.jengelman.gradle.plugins.shadow.transformers.AppendingTransformer

shadowJar {
    mergeServiceFiles()
    transform(AppendingTransformer) { resource = 'META-INF/spring.handlers' }
    transform(AppendingTransformer) { resource = 'META-INF/spring.schemas' }

    manifest {
        attributes 'Main-Class': 'com.company.MainRunner'
    }
    from sourceSets.test.output.classesDirs

    configurations = [project.configurations.testRuntime]
}

关于java - 带有测试 Spring Boot 的 Gradle fat jar 的 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57779739/

相关文章:

ssl - OpenJDK 抛出 "certificate chaining errors"

java - 是否不允许忽略 '.dockerignore' 中的 Dockerfile?

java - 更新 JList 模型导致 StackOverflowError

java - 迁移到 Spring Boot 2 - 安全编码密码看起来不像 BCrypt

具有基于映射的属性的 Spring-boot ConditionalOnProperty

gradle - Gradle无法从 Artifactory 中获取最新的库/版本

logging - 如何在Gradle构建期间显示来自第三方的某些日志

java - 无法添加 CSS 文件

java - 如何修复 "More than one file was found with OS independent path ' project.properties'"Android Studio 3.0 Beta-6 中的错误

java - Angular html5 模式和 HAL 浏览器