Gradle,shadowJar:在任务内部使用重定位 [英] Gradle, shadowJar: use relocate inside 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/**',
}
}
,我想将所有类从com/myCompany/project/event/**移至com/myCompany/relocated/project/event/**(以便某些使用我的jar并具有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?
推荐答案
您可以通过在build.gradle中添加以下插件来实现此目的
You can achieve this by adding below plugin to your build.gradle
apply plugin: 'com.github.johnrengelman.shadow'
添加此插件后,将以下代码添加到您的build.gradle文件中
After adding this plugin add below code to your build.gradle file
shadowJar {
relocate 'com.myCompany.project.event', 'com.myCompany.relocated.project.event'
}
添加此代码后,为确保ShadowJar任务在构建之前运行,请在末尾添加此行
After adding this, to ensure your ShadowJar task runs before build, add this line at the end
assemble.dependsOn shadowJar
这将确保在gradle构建期间在组装/构建任务之前触发阴影jar任务.
This will ensure that shadow jar task is triggered before assemble/build task during gradle build.
在完成Gradle构建时,您应该看到所有软件包及其对应的依赖项已从"com.myCompany.project.event"重新定位为"com.myCompany.relocated.project.event".
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'.
有关更多信息,您可以参考 ShadowJarUserGuide
For more info you can refer to ShadowJarUserGuide
这篇关于Gradle,shadowJar:在任务内部使用重定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!