如何在Gradle依赖项中将包重新定位到JAR内部。

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

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 = &#39;com.example.b&#39; // lib to relocate
    relocate pkg, &quot;com.example.standalone.b&quot; // we want to relocate the above package
    configurations = [project.configurations.relocateB] // our configuration from above
    dependencies {
        // you must exclude below files in &#39;kotlin_module&#39; and &#39;kotlin_builtins&#39; extension, 
        // otherwise you won&#39;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 &#39;**/*.kotlin_metadata&#39;
        exclude &#39;**/*.kotlin_module&#39;
        exclude &#39;**/*.kotlin_builtins&#39;
    }
}


tasks.withType(JavaCompile) {
    compileTask -&gt; compileTask.dependsOn relocateB
}



dependencies {
    ...
    relocateB (&#39;com.example.b:2.2.3&#39;)
    api tasks.relocateB.outputs.files
    api &#39;com.example.a&#39; // original package that won&#39;t be polluted
    ...
}

huangapple
  • 本文由 发表于 2020年7月31日 14:43:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63186983.html
匿名

发表评论

匿名网友

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

确定