Get the names of all the files/folders in an array - gradle

Basically I have a folder in that I have 4 zip files. I want to get the names of these zip files in an array.
Requirement :
I have a folder AggregatedComponetLibraries: I have my lib zips inside it. a.zip,b.zip,c.zip,d.zip. I want to get the name of the zips insde an array componentNames in gradle that means my array should contain : a,b,c,d

You can get it with FileTree so:
def names = []
fileTree(dir: 'AggregatedComponetLibraries', include: '**/*.zip').visit {
FileVisitDetails details ->
names << details.file.name
}
task printNames << {
println names
}
Here is the names array defined and then created a FileTree instance, for directory named "AggregatedComponetLibraries", this tree includes all the files with zip extention. After that, script traverses over the tree elements and add the element's names into the array.
Task printNames here is just to show the result.

Related

Groovy remove array element

i have this gradle task to get zip content and put it into another zip.
From src zip, i want to take all in directory 'r' and copy it into the target zip directory 'x/y/z'.
The code works, but I wonder if it can be more elegant.
from( zipTree("a.zip") ) {
include "r/**"
includeEmptyDirs = false
into "x/y/z"
eachFile { fcd ->
def segs1 = [fcd.relativePath.segments].flatten().findAll { it2 -> it2 != null };
segs1.removeAt(3)
fcd.relativePath = new RelativePath(true, segs1.toArray(new String[0]))
}
}
The problem I had is that fcd.relativePath.segments is String[], where i want to remove element with index 3.
Here i convert to list and back to array, brrr.
Ideas?
Frank
groovy based on java
and in java:
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
so, you could convert it to List, modify size, and then convert back to array
or create a new array with new size and copy required elements into it.

How to run multiple filters on various file types in processResources

I'm trying to do some processing on some source before moving it to the build directory. Specifically, for files with a .template name, replace instances of #timestamp# with the timeStamp variable i've defined. Additionally, for .jsp files, I would like to remove whitespace between tags.
The first part of this, replacing the timestamp works. Replacing the whitespace in the jsps does not.
processResources {
def timeStamp = Long.toString(System.currentTimeMillis())
from ('../src/resources/webapp') {
include '**/*.template'
filter {
it.replace('#timestamp#', timeStamp)
}
rename '(.*)\\.template', '$1'
}
from ('../src/resources/webapp') {
include '**/*.jsp'
filter {
it.replace('>\\s+<', '><')
}
}
}
Previous to using processResources, I had done something like this for the minification:
task minifyJSPs(type: Copy) {
from ('../src/resources/webapp') {
include '**/*.jsp'
filter {
it.replace('>\\s+<', '><')
}
}
into 'gbuild'
}
Filtering the files like this using copy worked, however, I noticed I wasn't able to copy from a location to itself -- the file would end up empty. This meant that I had to copy files from one intermediate directory to another, applying a filter at each step.
I want to be able to apply various transformations to my source in one step. How can I fix my processResources task?

Gradle Zip DuplicatesStrategy not working correctly

Suppose you have something like:
task zip(type: Zip) {
archiveName = "out.zip"
duplicatesStrategy = 'exclude'
into('TARGET_FOLDER_IN_ZIP') {
from("$rootDir/customizations/folder1")
from("$rootDir/customizations/folder2")
}
}
According to http://www.gradle.org/docs/current/javadoc/org/gradle/api/file/DuplicatesStrategy.html Exclude means
Do not allow duplicates by ignoring subsequent items to be created at the same path.
So if you have the same filename in folder1 & folder2 only the file from folder1 should end up in the zip. If you change the two "from" lines in the buildfile, only the file from folder2 should end up there. This seems not to be whats happening (gradle 1.10). Instead always the same file is used. Seems like nested "from"s do not preserve their order.
The only solution I've found is to split up the conflicting parts:
into('TARGET_FOLDER_IN_ZIP') {
from("$rootDir/customizations/folder1")
}
into('TARGET_FOLDER_IN_ZIP') {
from("$rootDir/customizations/folder2")
}
now the order is preserved and the output is deterministic

Exclude files from FileTree in Gradle

I want to exclude src\main and src\test files from src
FileCollection files =
project.fileTree(/src/).minus(project.fileTree(/src\main/)).minus(project.fileTree(/src\test/))
How can I exclude this directories without double minus usage?
The idiomatic way to exclude subdirectories from a FileTree is:
def files = fileTree("src").matching {
exclude "main", "test" // relative to the file tree's root directory
}
PS: Instead of .minus, you can use -.

multiple into sections in gradle zip seems to fail

Very similar to this question: Gradle Zip task to do multiple sub-trees? Which I don't believe is fully answered, merely circumvented..
I have a project with child projects, built with gradle 1.6, and I need to assemble some results into multiple paths, but I too see the last path surviving.
task zip (type: Zip) {
from ('src/resources') {
into '/'
}
from ('web') {
into '/'
}
from project('A').reference { into ('A') }
from project('B').reference { into ('B') }
}
(Essentially the reference task creates a few directories which are named the same in A and B, so needs to prepend the project name)..
Obviously the references all end up into /B/** in the zip file. When I reverse the order of the two lines, they end up in /A/**.
The other two goes correctly into /. If I move the subproject up before the root resources, they would still go in either /A or /B depending on their order, but the normal resources end in / as assumed.
I would essentially like to include the subprojects dynamically, i.e.
project.subprojects.each {
def pname = it.name
from project(pname).reference {
into "$pname"
}
}
but all my attempts so far has been in vain.
Any pointers welcome
The syntax doesn't look right. It should be from(project('A').reference) { into ('A') }. (Same for B.) Does this make a difference?
PS: into "/" is redundant and can be omitted.

Resources