[关闭]
@xtccc 2016-07-01T15:57:57.000000Z 字数 1744 阅读 2898

Distribution

给我写信
GitHub

此处输入图片的描述

Gradle


参考:


目录


1. 发布产品的结构


我们在发布产品时(称之为一个distribution),不仅仅是构建出一个Jar包就可以了,而是要包含其他的一些内容。最终发布的产品往往包含以下结构:

README.MD
conf
bin

其中, REDAME.MD是描述性文件,conf是配置文件,bin目录下则是一些构建出的Jar包,以及启动脚本startup.sh

如果我们用gradle build命令,则只能在build/libs下生成一个jar包,里面只含有src/main目录下的代码。如果我们希望将其他的文件也放入到最终生成的distribution中,则可以使用插件distribution




2. 将更多内容放入distribution中


假设我们的项目版本是1.0-RELEASE,构建后会生成一个名为akka-1.0-RELEASE.jar的JAR包。

我们的项目结构为:
image_1am3c1tj31dt61lfskprpl61n5713.png-40.7kB



我们希望在发布产品时,对外发布一个名为distribution-1.0-RELAEASE.zip的文件,该文件解压后的内容为:

  1. distribution-1.0-RELEASE
  2. ├── bin
  3.    ├── akka-1.0-RELEASE.jar
  4.    └── startup.sh
  5. ├── conf
  6.    └── log4j.properties
  7. ├── README.md
  8. └── startup.sh


那么,我们可以有两种方法:




3. 使用插件distribution


使用distribution插件时,build.gradle文件需要添加下面的内容:

  1. apply plugin: 'distribution'
  2. distributions {
  3. install { // install是 ${distribution.name}
  4. baseName = 'distribution'
  5. contents {
  6. into('conf') {
  7. from { 'src/main/resources' }
  8. }
  9. into('./') {
  10. from { 'src/dist/README.md' }
  11. }
  12. into('bin') {
  13. from { 'src/dist/startup.sh' }
  14. }
  15. into('bin') {
  16. from { jar }
  17. }
  18. }
  19. }
  20. }


在构建时,运行命令:

  1. ./gradlew clean build installDistZip

运行完成后,可以在目录build/distributions下面看到生成的zip文件。



解释一下我们命令中的installDistZip。这其实是我们定义的task name,形式为${distribution.name}DistZip。而build.gradle文件中的install是与此对应的。




4. 自定义Zip任务


如果不使用插件distribution,也可以自己定义一个Zip类型的任务。

  1. task buildZip(type: Zip, dependsOn: build) {
  2. from('src/main/resources') {
  3. into 'conf'
  4. }
  5. from('src/dist/README.md') {
  6. into './'
  7. }
  8. // 不能写成 from('src/dist/bin/*.sh'), 必须使用include
  9. from('src/dist/bin') {
  10. include '*.sh' // 只针对src/dist/*.sh文件
  11. fileMode 0755 // 要求对zip包解压后这些*.sh文件的权限是755, 不要忘记前面的0
  12. into 'bin'
  13. }
  14. from(jar) {
  15. into 'bin'
  16. }
  17. }



运行命令./gradlew clean buildZip即可。




5. 改变ZIP包内某个文件的权限


在上面的例子中,生成的ZIP包内有文件bin/startup.sh,如果我们希望将它的权限改为777,该怎么办?




添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注