0

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

我的摇篮:

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 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 一起正常工作

如何在 src/test 下使用主类为 Spring 项目创建胖 jar?

4

2 回答 2

0

线程“主”java.lang.NoClassDefFoundError 中的异常:org/apache/commons/cli/CommandLineParser

根据上述错误,您的类路径中似乎缺少库(Apache Commons-CLI)。因此,添加该库并重试。

关于这个错误,

无法找到 XML 模式命名空间的 Spring NamespaceHandler [ http://www.springframework.org/schema/aop]

使用aop命名空间而类路径上没有必要的aopspring 库时会出现问题。

但我看到你已经导入了那个库。

另一个原因是您试图创建一个 fat.jar 文件,因此spring.handlers / spring.schemas在将多个 spring 依赖项合并到一个 jar 文件时会被覆盖。

所以我建议使用Apache Maven Shade Plugin并重建项目。这是Apache Maven 的 Shade Plugin的 Gradle 版本。

于 2019-09-04T04:41:48.177 回答
0

这个 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]
}
于 2019-09-04T17:45:00.460 回答