How to exclude files from only one srcDir in a sourceSet? - gradle

I have a java project with two resource directories, res and raw. I've set this up in my build.gradle like so:
sourceSets.main.resources.srcDirs = ['res', 'raw']
Both of these directories contain a subdirectory called graphics. The issue is I want to exclude the graphics directory from raw, but not from res.
I have tried:
sourceSets.main.resources.srcDirs = ['res', 'raw']
sourceSets.main.resources.exclude 'raw/**/graphics'
but this does not work, because the exclude pattern is relative to the srcDirs.
How do I set an exclude pattern only for the raw directory?

The reason that didn't work is because the expression: sourceSets.main.resources.exclude 'raw/**/graphics', creates a new live view of the files, where the excluded files do not exist.
Simply put, it does not affect sourceSets.main.resources.
To make it "permanent", you have to use the groovy DSL syntax:
sourceSets.main {
resources {
srcDirs 'res', 'raw'
exclude 'raw/**/graphics'
}
}

Related

Capture and reuse a glob

I am new to Gulp programming.
I need to define a "dynamic" scss task that compiles multiple source directories in multiple destination directories
src/main/scss/
app.scss (global resource)
modules/ (modules resources)
admin/
*.scss
ftt/
*.scss
The above is the directory layout of the source files.
I am interested in compiling each set of scss files under modules directory (which I may not know in advance) into a directory tree that includes the module itself
src/main/scss/modules/admin/*.scss ==> webapp/secure/admin/common/common.css
src/main/scss/modules/ftt/*.scss==> webapp/secure/ftt/common/common.css
I can write a glob that captures src/main/scss/modules/*/*.scss but how to reuse the star representing the directory? If I was running regex I'd capture and use numbered group $1
For a longer working code version of looping through an array of folders to build folder-based bundles, see Processing arrays within Gulp to create bundles in each directory with ordered files
I would suggest looking at glob.sync. Do a search here for [gulp] glob.sync user:836330. That's me. I have answered a few questions here similar to yours. See particularly running a gulp task on separate folders. It runs the same gulp task on different folders and then uses the folder names to set unique destinations.
glob.sync is great for something like this.
Pseudo code follows (not tested):
const moduleFolders = glob.sync('src/main/scss/modules');
// perhaps your app.scss is supposed to be bundled into each module's css?
// if so, just add a second source to the gulp.src below
const sassSrc = 'common.scss'; // or your main scss file that does the imports in each module
gulp.task('default', () => {
let stream;
// work on each folder separately
moduleFolders.forEach(function (module) {
stream = gulp.src( module + sassSrc )
.pipe(sass())
//.pipe(concat('style.min.css'))
//.pipe(autoprefixer())
//.pipe(cssmin())
.pipe(gulp.dest( "webapp/secure/" + module + '/common' ));
});
return stream;
});

how to exclude path information in gradle zip task

Apologies in advance for this simple question I am relatively new to Gradle.
I would like to run a Zip task during my Build process and create a .zip file in my "archive" directory but without the source file's directory structure. I managed to get the task working but its retaining the directory structure. I read that for linux Zip there is the option -j or -r which flattens the directory but I am not sure if this is available to me via the Gradle task.
My input file structures looks similar to the below,
./libs/
./libs/file1.jar
./scripts/script1.sh
./scripts/script2.sh
but I would like to end up with a Zip file with directory structure as follows,
./
./file1.jar
./script1.sh
./script2.sh
My current Zip task is as follows,
task makeZipForDeploy(type: Zip) {
from 'build/'
include 'libs/*' //to include all files in libs folder
include 'scripts/*' //to include all files in the scripts folder
archiveName("${archiveFileName}")
destinationDir file("${archivePath}")
}
the solution was even more trivial than I thought. The below will flatten the structure
task makeZipForDeploy(type: Zip) {
from 'build/libs' //to include all files in libs folder
from 'build/scripts' //to include all files in the scripts folder
archiveName("${archiveFileName}")
destinationDir file("${archivePath}")
}

Gradle Copy Task: How can I avoid creating subdirectories while copying subdirectories content

I am trying to copy files from a parent directory which contain multiple sub directories. The required file Tree structure is below:
MachineLogs/XXXXX/*_CORE.txt
MachineLogs/YYYYY/*_CORE.txt
I am using the following code for selecting and copying required files:
from "$localLogsDir/CoreLogsUos1"
include '*/*_CORE_*.*'
into new File(analysisChainDir, 'CORE')
includeEmptyDirs = false
exclude { details -> details.file.isDirectory()}
The above snippet is copying the CORE files correctly but it is also copying the directory in which they are present. I can not name the sub-directories as they are created dynamically according to present date.
You can try in the following way (mind the regex, it does not match at the moment):
task copyLogs(type: Copy) {
from fileTree('logs').filter { it.isFile() && it.name.matches('.*_CORE.*') }
into 'dist'
includeEmptyDirs = false
}
Here you can find a demo.

Rename files in assemble task in gradle

I need to write a simple task to create a zip from the source code. I need to include a dir called 1-dir or 2-dir depending on a system property. But the name of the directory in the resulting zip should always be dir. So basically, I want to include a dir in a zip (conditionally) and rename it.
I tried the rename method but that does not work.
Any pointers?
It will be:
task zipDir(type: Zip) {
def fromDir = project.hasProperty('from') ? project.from : 'dir1'
from(fromDir)
into('dir')
}
It can be run in the following way gradle zipDir -Pfrom=dir2. If no from property is passed dir1 will be zipped.
If you need system property instead of gradle property, pass -Dfrom=dir2 and use System.properties['from'] instead of project.from.

Buildr - exclude directory from resources

In Buildr you can exclude all files in a directory by doing the following:
resources.exclude 'scratch/*'
Is it possible to exclude the directory as well? The Buildr documentation mentions:
The filter always excludes the CVS and .svn directories, and all files
ending with .bak or ~, so no need to worry about these.
My company uses Dimensions as its source control, it creates a .metadata folder in every directory much like subversion does with the .svn folder.
These exclusions are actually inherited from Rake (rake/file_list.rb)
module Rake
...
class FileList
...
DEFAULT_IGNORE_PATTERNS = [
/(^|[\/\\])CVS([\/\\]|$)/,
/(^|[\/\\])\.svn([\/\\]|$)/,
/\.bak$/,
/~$/
]
...
end
end
so it's possible to monkey-patch the defaults, if that's what you want.
Alternatively, you can also add exclusions directly on a FileList by passing a block and calling the exclude method,
pkg_files = FileList.new('lib/**/*') do |fl|
fl.exclude(/\bCVS\b/)
end
Since Buildr filters (http://buildr.apache.org/rdoc/classes/Buildr/Filter.html) expose their underlying FileList, you can simply do:
resources.sources do |fl|
fl.exclude(/\.metadata/)
end

Resources