Gradle在编译时包含JAR文件但在运行时排除。

huangapple go评论49阅读模式
英文:

Gradle include jar during compile time but exclude runtime

问题

I'm using local jars and I need javaee-api-6.0.jar during compile time but I want to exclude it during runtime.

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

I tried to exclude it during testImplementation but it doesn't help. runtime I got below error.

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

How can I include jar during compile time but exclude it at runtime?

英文:

I'm using local jars and I need javaee-api-6.0.jar during compile time but I want to exclude it during runtime.

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

I tried to exclude it during testImplementation but it doesn't help. runtime I got below error.

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

How can I include jar during compile time but exclude runtime

答案1

得分: 1

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

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

英文:

compileOnly — for dependencies that are necessary to compile your production code but shouldn’t be part of the runtime classpath

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

答案2

得分: 0

我使用了以下技巧。

dependencies {

compile fileTree(dir: '../lib', exclude: ['/**/javaee-api-*.jar'], include: ['/**/*.jar'])
    
compileOnly fileTree(dir: '../lib', include: ['/**/javaee-api-*.jar'])
}
英文:

I used below trick.

dependencies {

compile fileTree(dir: '../lib', exclude: ['/**/javaee-api-*.jar'], include: ['/**/*.jar'])
    
compileOnly fileTree(dir: '../lib', include: ['/**/javaee-api-*.jar'])
}

huangapple
  • 本文由 发表于 2020年8月3日 16:54:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63226436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定