Gradle, shadowJar: use relocate inside task

时光总嘲笑我的痴心妄想 提交于 2019-12-11 08:11:31

问题


I have the following task:

task myJar(type: Jar) {
    archiveName = 'myJar.jar'
    includeEmptyDirs = false
    destinationDir = rootProject.libsDir
    dependsOn compileJava

    manifest.attributes('Class-Path': '../lib/commons-lang-2.5.jar')

    into '/', {
        from compileJava.destinationDir
        include 'com/myCompany/project/util/order/**',
                'com/myCompany/project/event/**',
    }
}

and I would like to relocate all classes from com/myCompany/project/event/** to com/myCompany/relocated/project/event/** (so that some apps using my jar and having com.myCompany.project.event package defined will avoid any possible conflicts)

I discovered that it can be done using shadow plugin and I tried to add

relocate 'com.myCompany.project.event.', 'com.myCompany.relocated.project.event.'

under this task but it doesn't seem to work. Does anybody know where I should add this line?


回答1:


You can achieve this by adding below plugin to your build.gradle

apply plugin: 'com.github.johnrengelman.shadow'

After adding this plugin add below code to your build.gradle file

shadowJar {
    relocate 'com.myCompany.project.event', 'com.myCompany.relocated.project.event'
}

After adding this, to ensure your ShadowJar task runs before build, add this line at the end

assemble.dependsOn shadowJar

This will ensure that shadow jar task is triggered before assemble/build task during gradle build.

On doing the Gradle build, you should see all your packages and their corresponding dependencies relocated from 'com.myCompany.project.event' to 'com.myCompany.relocated.project.event'.

For more info you can refer to ShadowJarUserGuide



来源:https://stackoverflow.com/questions/53060258/gradle-shadowjar-use-relocate-inside-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!