gradle - unpack zip in distribution - gradle

My gradle build is creating a distribution for me:
apply plugin: 'distribution'
distributions {
main {
contents {
from ('src/resources/test.zip')
// other stuff
}
}
}
This results with test.zip and all the other stuff inside my created distribution. My question is how is it possible to unpack test.zip into the new distribution?

Use a zipTree to get the contents of the zip file:
contents {
from (zipTree('src/resources/test.zip'))
// other stuff
}

Related

How to import code from outside into gradle build file?

My build files are large and messy, making them difficult to read. like below:
plugins {
...
id "com.google.protobuf" version "0.8.17"
}
dependencies {
implementation "androidx.datastore:datastore-core:1.0.0"
implementation "com.google.protobuf:protobuf-javalite:3.18.0"
...
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.14.0"
}
// Generates the java Protobuf-lite code for the Protobufs in this project. See
// https://github.com/google/protobuf-gradle-plugin#customizing-protobuf-compilation
// for more information.
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option 'lite'
}
}
}
}
}
I want to define the above code into an external file, and then introduce it into the build file, how should I do it?
According to the Gradle documentation, as of now it is not possible to move the plugin block to other file than the project’s build script or settings.gradle file.
For the other sections, let's say dependencies or protobuf, then you can move these sections on a separate gradle files and import them by using the following statement:
apply from: "${project.rootDir}/your-gradle-file"
Of course the path of your-gradle-file should be adjusted according to the project's folder structure you decide.
If you want to split the dependencies into multiple gradle file you can do the following:
on your main gradle file:
dependencies {
apply from: "${project.rootDir}/depsGroup1.gradle"
apply from: "${project.rootDir}/depsGroup2.gradle"
}
and within each depsGroup file:
dependencies {
implementation xyz
}

Change name of zip when creating archive with distribution plugin in gradle

Is there any way to configure distribution file name when creating archive using distribution plugin in gradle?
Here is my build.gradle (I'm using gradle 2.10) file:
apply plugin: 'distribution'
version '1.2'
distributions {
custom {
baseName = 'someName'
contents {...}
}
}
And calling the task customDistZip creates file: $buildDir/distributions/someName-1.2.zip I would like to have a file name configurd to: someName.zip (without version number).
My workaround for this is to create new task to rename the zip file after creation and use gradle finalizedBy feature:
task renameDist {
doLast {
new File("$buildDir/distributions/someName-${project.version}.zip")
.renameTo("$buildDir/distributions/someName.zip")
}
}
customDistZip.finalizedBy renameDist
But this don't look like elegant and nice solution.
You can either do:
customDistZip.setVersion('')
to set the version string used with baseName to empty or
customDistZip.setArchiveName('someName.zip')
to set the full archive filename, which skips basename alltogether.
Kotlin/Groovy:
distributions {
->distribution<- {
distributionBaseName.set(->name<-)
}
}

How to change destination folder in zip for gradle distribution plugin [duplicate]

I created a simple Gradle build that exports the contents of ./src/main/groovy to a zip file. The zip file contains a folder with the exact same name as the zip file. I cannot figure out how to get the files into the root of the zip file using the distribution plugin.
i.e. gradlew clean distZip produces:
helloDistribution-1.0.zip -> helloDistribution-1.0 -> files
what I would like:
helloDistribution-1.0.zip -> files
My build.gradle file:
apply plugin: 'groovy'
apply plugin: 'distribution'
version = '1.0'
distributions {
main {
contents {
from {
'src/main/groovy'
}
}
}
}
I have attempted to fix the problem by adding into { 'dir' } but to no avail.
Using into '/' seems to do the trick:
contents {
from {
'src/main/groovy'
}
into '/'
}
Unfortunately, penfold's answer did not work for me. Here is the solution I came up with:
task Package(type: Zip) {
from {
def rootScriptFiles = [] // collection of script files at the root of the src folder
new File('src/main/groovy/').eachFile { if (it.name.endsWith('.groovy')) rootScriptFiles.add(it) }
['build/libs/', // build binaries
'src/res/', // resources
rootScriptFiles, // groovy root source files
]
}
baseName = pluginName
}
To copy files into the root of helloDistribution-1.0.zip -> helloDistribution-1.0 -> use
contents {
from {
'some/file'
}
into ''
}

How can I define two different 'distribution' tasks in gradle?

The normal behavior of the distTar and distZip tasks from the application plugin in gradle seems to be to copy the contents of src/dist into the zip and tar files, but I have a subfolder in src/dist that I want to exclude from the default distribution, and include it for a new (extended) task, possibly to be called distZipWithJRE.
I have been able to exclude this folder in the default task as follows:
distributions.main {
contents {
from('build/config/main') {
into('config')
}
from('../src/dist') {
exclude('jre')
}
}
}
How can I define a second task that behaves just like the original (unmodified) task?
Using Gradle 4.8 I had to tweak the answer to use 'with' from CopySpec instead
distributions {
zipWithJRE {
baseName = 'zipWithJRE'
contents {
with distributions.main.contents
}
}
}
It seems that what you're looking for is in the docs. You need to leave current settings as is and for zipWithJRE create and configure custom distribution:
distributions {
zipWithJRE {
baseName = 'zipWithJRE'
contents {
from { distributions.main.contents }
}
}
}

Gradle Distribution Task Output Files Not at Root of ZIP

I created a simple Gradle build that exports the contents of ./src/main/groovy to a zip file. The zip file contains a folder with the exact same name as the zip file. I cannot figure out how to get the files into the root of the zip file using the distribution plugin.
i.e. gradlew clean distZip produces:
helloDistribution-1.0.zip -> helloDistribution-1.0 -> files
what I would like:
helloDistribution-1.0.zip -> files
My build.gradle file:
apply plugin: 'groovy'
apply plugin: 'distribution'
version = '1.0'
distributions {
main {
contents {
from {
'src/main/groovy'
}
}
}
}
I have attempted to fix the problem by adding into { 'dir' } but to no avail.
Using into '/' seems to do the trick:
contents {
from {
'src/main/groovy'
}
into '/'
}
Unfortunately, penfold's answer did not work for me. Here is the solution I came up with:
task Package(type: Zip) {
from {
def rootScriptFiles = [] // collection of script files at the root of the src folder
new File('src/main/groovy/').eachFile { if (it.name.endsWith('.groovy')) rootScriptFiles.add(it) }
['build/libs/', // build binaries
'src/res/', // resources
rootScriptFiles, // groovy root source files
]
}
baseName = pluginName
}
To copy files into the root of helloDistribution-1.0.zip -> helloDistribution-1.0 -> use
contents {
from {
'some/file'
}
into ''
}

Resources