如何在Gradle中的zip依赖项中打包文件 [英] How to pack files inside zip dependency in Gradle

查看:1926
本文介绍了如何在Gradle中的zip依赖项中打包文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Gradle项目,它具有从Nexus存储库下载的依赖关系。
这个依赖关系之一我想得到jar和一个zip文件。



jar(name-version.jar)很容易获得:

 依赖关系{
compile'group:name:version'
}

但是我想要可用的其他工件名称如下:
name-version-config.zip / p>

如何从存储库中获取这个工件?
我需要这个工件来获取一些文件,以在我的包中使用/分发任务。



我已经尝试创建一个新的配置并配置如下:

 配置{
zipConfig
}

依赖关系{
...
编译组:< group>,name:< ; name>,version:< version>
zipConfig组:< group>,name:< name>,version:< version> +'-config',ext:'zip'
}

但是,将尝试使用错误的路径找到正确的文件名:group / name / version-config / name-version-config.zip,我需要它在group / name / version / name-version-config中搜索文件.zip

解决方案

在Maven中,我们可以使用分类器生成artifact。为了让Gradle下载一个具有分类器的特定文件,我定义了一个名为zipConfig的新配置,并声明依赖关系如下:

  
zipConfig
}

依赖关系{
...
zipConfig组:'< group>',名称:< name>,版本: < version>,ext:'zip',classifier:'config'
}



<

要提取zip工件的文件并将其复制到我的包中,我定义了一个解压缩文件并将文件复制到build / resources文件夹中的任务。

  task unzipConfig(type:Copy){
def sdkServerPattern =。*+ sdkServer +。* config。*
def pattern = 〜/ $ sdkServerPattern /
def configZipTree = zipTree(configurations.zipConfig.filter {it.name.matches(pattern)}。singleFile)

from configZipTree
include'** / * filename1'
包含'** / * filename2'
into(project.buildDir.p ath +'/ resources')
}



在项目的包任务(其中所有内容都打包成.tar.gz文件)资源文件夹中的文件被引用以获得打包在一起。

 任务包(类型:tar,dependsOn:unzipConfig){
dependsOn build

compression = Compression.GZIP
extension ='tar.gz'
$($)
$($ / $)

$ b从(configurations.runtime){
into('/ lib')
}

def sdkConfigFolderName ='build / resources /'+ sdkServer +' - '+ sdkServerVersion +'/ config /'

from sdkConfigFolderName +'filename1'
from sdkConfigFolderName +'filename2'

baseName = project.name +'-distribution'
}


I have a Gradle project with dependencies that are downloaded from a Nexus repository. One of this dependency I want to get the jar and a zip file.

The jar ("name-version.jar") is easily available through:

dependency {
    compile 'group:name:version'
}

But the other artifact I want to be available has name as follows: "name-version-config.zip"

How can I get this artifact from the repository? I need this artifact to get some files inside it to use during my package/distribution task.

I already tried to create a new configuration and configure like this:

configurations {
    zipConfig
}

dependencies {
    ...
    compile group: <group>, name: <name>, version: <version>
    zipConfig group: <group>, name: <name>, version: <version> + '-config', ext: 'zip'
}

But this fails because gradle will try to find the right filename using the wrong path: "group/name/version-config/name-version-config.zip" and I need it to search the file in "group/name/version/name-version-config.zip"

解决方案

In Maven we can generate artifact's with classifiers. To get Gradle to download one specific file with classifier I defined a new configuration called zipConfig and declared the dependency as follows:

configurations {
  zipConfig
}

dependencies {
  ...
  zipConfig group:'<group>', name:<name>, version:<version>, ext:'zip', classifier:'config'
}


To extract the zip artifact's files and copy them into my package I defined a task that unzip's the file and copy the files inside it into the build/resources folder.

task unzipConfig(type: Copy) {
  def sdkServerPattern = ".*" + sdkServer + ".*config.*"
  def pattern = ~/$sdkServerPattern/
  def configZipTree = zipTree(configurations.zipConfig.filter{it.name.matches(pattern)}.singleFile)

  from configZipTree
  include '**/*filename1'
  include '**/*filename2'
  into(project.buildDir.path + '/resources')
}


In the project's "pack" task (where everything is packed into a .tar.gz file) the files on resources folder are referenced to get packed together.

task pack(type: Tar, dependsOn: unzipConfig) {
  dependsOn build

  compression = Compression.GZIP
  extension = 'tar.gz'

  from(configurations.runtime.allArtifacts.files) {
    into('/lib')
  }

  from(configurations.runtime) {
    into('/lib')
  }

  def sdkConfigFolderName = 'build/resources/' + sdkServer + '-' + sdkServerVersion + '/config/'

  from sdkConfigFolderName + 'filename1'
  from sdkConfigFolderName + 'filename2'

  baseName = project.name + '-distribution'
}

这篇关于如何在Gradle中的zip依赖项中打包文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆