简体   繁体   中英

how to relocate package inside jar for gradle dependencies

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'
}

finally i managed to solve it by below configuration in goovy 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
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM