How can I define two different 'distribution' tasks in gradle? - 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 }
}
}
}

Related

Gradle 7: How to create folders in ZIP task?

I try to create zip-task in Gradle 7 and I want to have different folders for different files.
task myZipTask(type: Zip) {
from 'first/source'
into 'firstFolderInsideZipFile'
from 'second/source'
into 'secondFolderInsideZipFile'
}
It obviously doesn't work like this, but is there a way to implement it?
You should be able to do something like this:
plugins {
id 'java'
}
task myZipTask(type: Zip) {
from('first/source') {
into('firstFolderInsideZipFile')
}
from('second/source') {
into('secondFolderInsideZipFile')
}
}

How to exclude urlrewriterules.xml and domainsplittings.xml from being copied to config dir on serverDeploy task

We are trying to put our custom domainsplittings.xml and urlrewriterules.xml files wit our url rewrite rules to share/system/config/cluster folder, but every time we run deployServer gradle task, those files are copied from bc_urlrewrite.zip from local gradle repo.
We already tried to define custom deployment/deploy.gradle file in one of our cartridges with following code:
project(':bc_urlrewrite') {
afterEvaluate {
deployment.files.share {
exclude {
new File(destinationDir, it.path) == new File(target.shareDirectory, 'system/config/cluster/domainsplittings.xml')
new File(destinationDir, it.path) == new File(target.shareDirectory, 'system/config/cluster/urlrewriterules.xml')
}
}
}
}
as stated here: https://support.intershop.com/kb/index.php/Display/282B92#Cookbook-DeploymentToolsICM7.9-Recipe:ReplaceaFileDeployedbyAnotherComponent but this does not work. Files are still copied from bc_urlrewrite.zip on deployServer task.
Are we doing something wrong? We don't need those files because they contains url rewrite rules for demo intronics store.
Thank you for your help!
Yeah, the documentation isn't very clear and it use to be that you could simple overload the setting. Can you try the following configuration.
apply plugin: com.intershop.deploy.cartridge.CartridgeDeploymentPlugin
if (target.includeShare && findProject(':bc_urlrewrite')) {
project(':bc_urlrewrite') {
def excludeFiles = {
deployment.files.share {
exclude 'system/config/cluster/urlrewriterules.xml'
exclude 'system/config/cluster/domainsplittings.xml'
}
}
if (project.state.executed) {
excludeFiles()
} else {
afterEvaluate(excludeFiles)
}
}
}

Using both Application and Distribution plugin in gradle

When using the distributions plugin I had the following code in order to set up the folder structure in the distribution output.
Now however I have to use the 'application' plugin.
1. Can these two plugins be used together? (complains about tasks with identical names)
2. If not, how does one implement the code below for the applications plugin?
distributions {
main {
baseName = appName
contents {
into('bin') { from jar.archivePath }
into('lib') { from configurations.runtime }
into('etc') { from project(':server').file('src/main/other') }
}
}
}
After discussion in comments, the following piece of code should help:
applicationDistribution.from(jar.archivePath) {
into "bin"
}
applicationDistribution.from(configurations.runtime ) {
into "lib"
}
applicationDistribution.from(project(':server').file('src/main/other')) {
into "etc"
}
Or (maybe) the shorter form (can't verify it)
with(applicationDistribution) {
from(jar.archivePath) { into "bin" }
from(configurations.runtime ) { into "lib" }
from(project(':server').file('src/main/other')) { into "etc" }
}
As already mentioned: Don't know what is baseName exactly, but suppose it also can be set.

Gradle copy without overwrite

Is there a way to avoid overwriting files, when using task type:Copy?
This is my task:
task unpack1(type:Copy)
{
duplicatesStrategy= DuplicatesStrategy.EXCLUDE
delete(rootDir.getPath()+"/tmp")
from zipTree(rootDir.getPath()+"/app-war/app.war")
into rootDir.getPath()+"/tmp"
duplicatesStrategy= DuplicatesStrategy.EXCLUDE
from rootDir.getPath()+"/tmp"
into "WebContent"
}
I want to avoid to specify all the files using exclude 'file/file*'.
It looks like that duplicatesStrategy= DuplicatesStrategy.EXCLUDE doesn't work. I read about an issue on gradle 0.9 but I'm using Gradle 2.1.
Is this problem still there?
Or am I misunderstanding how this task should be used properly?
Thanks
A further refinement of BugOrFeature's answer. It's using simple strings for the from and into parameters, uses the CopySpec's destinationDir property to resolve the destination file's relative path to a File:
task ensureLocalTestProperties(type: Copy) {
from zipTree('/app-war/app.war')
into 'WebContent'
eachFile {
if (it.relativePath.getFile(destinationDir).exists()) {
it.exclude()
}
}
}
You can always check first if the file exists in the destination directory:
task copyFileIfNotExists << {
if (!file('destination/directory/myFile').exists())
copy {
from("source/directory")
into("destination/directory")
include("myFile")
}
}
Sample based on Peter's comment:
task unpack1(type: Copy) {
def destination = project.file("WebContent")
from rootDir.getPath() + "/tmp"
into destination
eachFile {
if (it.getRelativePath().getFile(destination).exists()) {
it.exclude()
}
}
}

Extract specific JARs from dependencies

I am new to gradle but learning quickly. I need to get some specific JARs from logback into a new directory in my release task. The dependencies are resolving OK, but I can't figure out how, in the release task, to extract just logback-core-1.0.6.jar and logback-access-1.0.6.jar into a directory called 'lib/ext'. Here are the relevant excerpts from my build.gradle.
dependencies {
...
compile 'org.slf4j:slf4j-api:1.6.4'
compile 'ch.qos.logback:logback-core:1.0.6'
compile 'ch.qos.logback:logback-classic:1.0.6'
runtime 'ch.qos.logback:logback-access:1.0.6'
...
}
...
task release(type: Tar, dependsOn: war) {
extension = "tar.gz"
classifier = project.classifier
compression = Compression.GZIP
into('lib') {
from configurations.release.files
from configurations.providedCompile.files
}
into('lib/ext') {
// TODO: Right here I want to extract just logback-core-1.0.6.jar and logback-access-1.0.6.jar
}
...
}
How do I iterated over the dependencies to locate those specific files and drop them in the lib/ext directory created by into('lib/ext')?
Configurations are just (lazy) collections. You can iterate over them, filter them, etc. Note that you typically only want to do this in the execution phase of the build, not in the configuration phase. The code below achieves this by using the lazy FileCollection.filter() method. Another approach would have been to pass a closure to the Tar.from() method.
task release(type: Tar, dependsOn: war) {
...
into('lib/ext') {
from findJar('logback-core')
from findJar('logback-access')
}
}
def findJar(prefix) {
configurations.runtime.filter { it.name.startsWith(prefix) }
}
It is worth nothing that the accepted answer filters the Configuration as a FileCollection so within the collection you can only access the attributes of a file. If you want to filter on the dependency itself (on group, name, or version) rather than its filename in the cache then you can use something like:
task copyToLib(type: Copy) {
from findJarsByGroup(configurations.compile, 'org.apache.avro')
into "$buildSrc/lib"
}
def findJarsByGroup(Configuration config, groupName) {
configurations.compile.files { it.group.equals(groupName) }
}
files takes a dependencySpecClosure which is just a filter function on a Dependency, see: https://gradle.org/docs/current/javadoc/org/gradle/api/artifacts/Dependency.html

Resources