英文:
how to relocate package inside jar for gradle dependencies
问题
Sure, here's the translated code portion:
implementation a.jar:1.0
implementation (b.jar:2.0) {
   rename 'com.example.b' to 'com.example.standalone.b'
}
If you have any further questions or need assistance, feel free to ask.
英文:
currently I am using a package call a.jar. Unfortunately that this jar includes a com.example.b package with some customized changes to code base inside com.example.b.
Now I want to have some latest cool features from com.example.b package in b.jar from github.
I think the best solution (not sure how it can be done) is to relocate latest com.example.b to com.example.standalone.b , so that the a.jar can still use its customized com.exampl.b source, while inside project I can use com.example.standalone.b package.
I did research for shadow plugin, but seems it rename package by package name globally, so that the package (com.example.b) in both jars (a.jar and b.jar) would be also renamed and have conflict.
May I know how to do this for specific jar, like below example?
implementation a.jar:1.0
implementation (b.jar:2.0) {
   rename 'com.example.b' to 'com.example.standalone.b'
}
答案1
得分: 4
Here's the translated code portion:
终于,我成功地通过以下 Groovy Gradle 配置解决了它。
```你的模块的 Gradle 配置
configurations {
    relocateB // 只需定义一个独立的配置
}
task relocateB (type: ShadowJar) {
    def pkg = 'com.example.b' // 要迁移的库
    relocate pkg, "com.example.standalone.b" // 我们想要迁移上面的包
    configurations = [project.configurations.relocateB] // 上述配置
    dependencies {
        // 你必须在 'kotlin_module' 和 'kotlin_builtins' 扩展中排除下面的文件,
        // 否则你将无法在 Kotlin 文件中导入 `com.example.standalone.b`。
        // 这是 Kotlin 的一个错误,我花了两天时间来解决。
        // (https://youtrack.jetbrains.com/issue/KT-25709)
        exclude '**/*.kotlin_metadata'
        exclude '**/*.kotlin_module'
        exclude '**/*.kotlin_builtins'
    }
}
tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn relocateB
}
dependencies {
    ...
    relocateB ('com.example.b:2.2.3')
    api tasks.relocateB.outputs.files
    api 'com.example.a' // 原始的包,不会受到污染
    ...
}
<details>
<summary>英文:</summary>
finally i managed to solve it by below configuration in goovy gradle.
```your module gradle
configurations {
    relocateB // just define a separate configuration
}
task relocateB (type: ShadowJar) {
    def pkg = 'com.example.b' // lib to relocate
    relocate pkg, "com.example.standalone.b" // we want to relocate the above package
    configurations = [project.configurations.relocateB] // our configuration from above
    dependencies {
        // you must exclude below files in 'kotlin_module' and 'kotlin_builtins' extension, 
        // otherwise you won't be able to import `com.example.standalone.b` in kotlin files. 
        // This is a bug from Kotlin, consumed two days for me to solve. 
        // (https://youtrack.jetbrains.com/issue/KT-25709)
        exclude '**/*.kotlin_metadata'
        exclude '**/*.kotlin_module'
        exclude '**/*.kotlin_builtins'
    }
}
tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn relocateB
}
dependencies {
    ...
    relocateB ('com.example.b:2.2.3')
    api tasks.relocateB.outputs.files
    api 'com.example.a' // original package that won't be polluted
    ...
}
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论