首页 > 解决方案 > Gradle 在编译时包含 jar 但排除运行时

问题描述

我正在使用本地 jar,我javaee-api-6.0.jar在编译时需要,但我想在运行时排除它。

dependencies {
 
    // Add all the jar dependencies from the lib folder. 
    compile fileTree(dir: '../lib', include: ['/**/*.jar'])
    
   //testImplementation fileTree(dir: '../lib', excludes: ['/**/javaee-api-*.jar'])
}

我试图在此期间排除它,testImplementation但它没有帮助。运行时我得到以下错误。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'mycompany.persistence.HibernateBackwardsCompatibilifier#0' defined in class path resource 
[hibernateBeans.xml]: Instantiation of bean failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[mycompany.persistence.HibernateBackwardsCompatibilifier]: Constructor threw exception; nested 
exception is java.lang.ClassFormatError: Absent Code attribute in method that is not native or
 abstract in class file javax/persistence/PersistenceException

如何在编译时包含jar 但排除运行时

标签: javaspringgradle

解决方案


compileOnly— 对于编译生产代码所必需但不应成为运行时类路径的一部分的依赖项

https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_dependency_management_overview


推荐阅读