spring_reference/VIII. Build tool plugins/59.8. Repackage with custom...

29 lines
1.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 59.8. 使用Gradle自定义配置进行Repackage
有时候不打包解析自compileruntime和provided作用域的默认依赖可能更合适些。如果创建的可执行jar被原样运行你需要将所有的依赖内嵌进该jar中然而如果目的是explode一个jar文件并手动运行main类你可能在CLASSPATH下已经有一些可用的库了。在这种情况下你可以使用不同的依赖集重新打包repackage你的jar。
使用自定义的配置将自动禁用来自compileruntime和provided作用域的依赖解析。自定义配置即可以定义为全局的处于springBoot部分内也可以定义为任务级的。
```gradle
task clientJar(type: Jar) {
appendix = 'client'
from sourceSets.main.output
exclude('**/*Something*')
}
task clientBoot(type: BootRepackage, dependsOn: clientJar) {
withJarTask = clientJar
customConfiguration = "mycustomconfiguration"
}
```
在以上示例中我们创建了一个新的clientJar Jar任务从你编译后的源中打包一个自定义文件集。然后我们创建一个新的clientBoot BootRepackage任务并让它使用clientJar任务和mycustomconfiguration。
```gradle
configurations {
mycustomconfiguration.exclude group: 'log4j'
}
dependencies {
mycustomconfiguration configurations.runtime
}
```
在BootRepackage中引用的配置是一个正常的[Gradle配置](http://www.gradle.org/docs/current/dsl/org.gradle.api.artifacts.Configuration.html)。在上面的示例中我们创建了一个新的名叫mycustomconfiguration的配置指示它来自一个runtime并排除对log4j的依赖。如果clientBoot任务被执行重新打包的jar将含有所有来自runtime作用域的依赖除了log4j jars。