Grails 2中具有相同包名称的不同依赖项

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

grails 2 different dependencies with same package names

问题

一些来自不同库的依赖项具有相同的包名称 javax.mail。并且它们内部的所有类都具有不同版本但相同的类名。

我希望能够在运行时选择要加载的类。更具体地说,我有一个用于电子邮件的 MimeMessage 类,但目前程序选择旧版本而不是新版本,因此这个字段 protected InternetHeaders headers; 没有正确填充,电子邮件和主题的字符集没有添加,因此我无法发送包含 HTML 正文的邮件。
jar 文件名称:

javax.mail-1.6.2.jar
geronimo-javamail_1.4_spec-1.2.jar
javax.mail-1.5.1.jar
javax.mail-api-1.5.1.jar

它们都包含 javax.mail.internet.MimeMessage

英文:

Some dependencies from various libraries have same package name javax.mail. And all the classes inside them are equal with different versions.

I want to be able to choose which class to load at runtime. To be more specific, I have MimeMessage class for email and right now the program is choosing the old version rather than the newer version so this field protected InternetHeaders headers; is not correctly filled and the charset for email and subject is not added so I am unable to send mail with html body.
jar file names :

javax.mail-1.6.2.jar
geronimo-javamail_1.4_spec-1.2.jar
javax.mail-1.5.1.jar
javax.mail-api-1.5.1.jar

all of them have javax.mail.internet.MimeMessage

答案1

得分: 1

你可以尝试以下操作:

  • 排除冲突的依赖项
dependencies {
    compile('org.example:conflict-library:1.0') {
        excludes 'geronimo-javamail_1.4_spec'
    }
}
  • 使用依赖管理
dependencyManagement {
    imports {
        mavenBom 'org.example:bom:1.0'
    }
    dependencies {
        dependency 'javax.mail:mail:1.6.2'
    }
}

强制所有依赖于 javax.mail:mail 的依赖项使用版本 1.6.2

PS:在修改后记得清理和重新构建你的项目。

英文:

You may wanna try to

  • Exclude the conflicting dependency
dependencies {
    compile('org.example:conflict-library:1.0') {
        excludes 'geronimo-javamail_1.4_spec'
    }
}
  • Use dependency management
dependencyManagement {
    imports {
        mavenBom 'org.example:bom:1.0'
    }
    dependencies {
        dependency 'javax.mail:mail:1.6.2'
    }
}

forcing all dependencies on javax.mail:mail to use version 1.6.2

PS: remember to clean and rebuild your project after modifying

huangapple
  • 本文由 发表于 2023年6月11日 21:04:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450595.html
匿名

发表评论

匿名网友

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

确定