taciosd
taciosd

Reputation: 312

How to pack files inside zip dependency in Gradle

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"

Upvotes: 2

Views: 3780

Answers (2)

taciosd
taciosd

Reputation: 312

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'
}

Upvotes: 1

RaGe
RaGe

Reputation: 23677

You can write a gradle task to download the zip file to a local directory and then use the downloaded file as a dependency by name.

Or alternatively you can use a custom ivy pattern and treat the URL as a custom layout ivy repository to fetch the zip file. See here

Upvotes: 0

Related Questions