Using wildcard in Gradle Files then copy if it exists - gradle

In very brief, I want to find all files that ends with *.sql and copy them if they exist.
There might be 0 or more files in etc directory.
File sqlfiles = file('etc/' + '*.sql')
logger.info("Looking for SQL files: " + sqlfiles);
if (sqlfiles.exists())
{
logger.info("Found log SQL file: " + sqlfiles);
copy
{
from sqlfiles
into "$rpmStoredir"
}
}
else
{
logger.warn("No SQL file found - skipping");
}
With my code, the wildcard is not working here.
So adding "include" to the copy as in the below is working but I just want to figure how to add a logger if the file does not exist
copy
{
from "etc/"
include "*.sql"
into "$rpmStoredir"
}

file(...) is the wrong method to use as this returns a single java.io.File
You could do something like
FileTree myTree = fileTree('etc') {
include '*.sql'
}
if (myTree.empty) {
...
} else {
copy {
from myTree
...
}
}
See Project.fileTree(Object, Closure) and FileTree

Related

How can you copy from subdirectories in Gradle?

Suppose I have the following folder structure:
folder
-subfolderA
-module1.mod
-module1.a
-module1.b
-module1.c
-module2.mod
-module2.a
-module2.b
-module2.c
-module1.d
-subfolderB
-module3.mod
-module3.a
-module3.b
-module3.c
-module3.d
I'd like to flatten away just the "subfolder" tier of directories, producing the following:
outputFolder
-module1.mod
-module1.a
-module1.b
-module1.c
-module2.mod
-module2.a
-module2.b
-module2.c
-module3.mod
-module3.a
-module3.b
-module3.c
-module1.d
-module3.d
I expected this to be extremely simple, with:
copy {
from "folder/*/"
into "outputFolder"
}
But this didn't work. What's the easiest way to flatten away one (or more) layers of subdirectories?
You could probably do it as
copy {
from 'folder'
include '*/**/*.*'
eachFile { FileCopyDetails fcd ->
int slashIndex = fcd.path.indexOf('/')
fcd.path = fcd.path.substring(slashIndex+1)
}
into "outputFolder"
}
Or perhaps
copy {
from { file('folder').listFiles().findAll { it.directory } }
into "outputFolder"
}
I eventually settled on the following as the best combination of clean and configurable. By modifying n, you can flatten as many directories as you like:
copy {
from {
file("folder")
include "**/*"
eachFile { file ->
file.relativePath = new RelativePath(true, file.relativePath.segments.drop(n))
}
includeEmptyDirs = false
}
into "outputFolder"
}

Gradle issue using file and filter

I am trying to loop over a map, for each key read some property files, use filter to replace some tokens. My issue is, looping happens. At the end, instead of having 5 folders, I've just 1 folder inside config. Do I need to close any resources like file/inputstream before going on to the next?
task copyResByHost(type: Copy) {
java.util.HashMap hostMap = new HashMap();
hostMap.put("devserver01","env_dev.properties");
hostMap.put("devserver02","env_dev.properties");
hostMap.put("devserver03","env_dev.properties");
hostMap.put("devserver04","env_dev.properties");
hostMap.put("devserver05","env_dev.properties");
hostMap.each { key, value ->
from "$projectDir/resources/templates"
into("build/config/${key}")
def myProps = new Properties()
file("$projectDir/resources/properties/${value}").withInputStream {
myProps.load(it);
}
file("$projectDir/resources/properties/${key}.properties").withInputStream {
myProps.load(it);
}
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: myProps)
}
}
Can you let me know where am I making a mistake which is causing 1 folder only created instead of 5 inside config?
Thanks for your guidance.
from and into were being overwritten everytime causing only 1 folder to be created.
So code should be changed as follows:
hostMap.each { key, value ->
println "Creating configs for $key"
//inputs.dir '$projectDir/resources/templates'
// outputs.dir 'build/config/${key}'
doLast {
copy {
from("$projectDir/resources/templates")
into("build/config/${key}")
def myProps = new Properties()
new File("$projectDir/resources/properties/${value}").withInputStream { stream ->
myProps.load(stream);
}
new File("$projectDir/resources/properties/${key}.properties").withInputStream { stream ->
myProps.load(stream);
}
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: myProps)
}
}
}
from & into, file stream everything should appear within closure doLast { copy { } }

Why doesn't gradle exec work with scripts?

Is there any way to make gradle exec work like a shell exec? ie - understand executable files in the path?
We have code that needs to work on windows and unix - and many many scripts that are obviously different on both machines. While I can do a hack like this:
npmCommand = Os.isFamily(Os.FAMILY_WINDOWS) ? 'npm.cmd' : '/usr/local/bin/npm'
and then run that command - the paths for some scripts are not necessarily set in stone - and it's just horrible code.
Is there any way to fix the problem - ie extend the exec task to find executable files in the path and run them?
How about something like:
if (System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows'))
{
commandLine 'cmd', '/c', 'commandGoesHere'
}
else
{
commandLine 'sh', '-c', 'commandGoesHere'
}
I'd love a better way - but I made a simple function that I put in our common that you use like this:
exec {
commandLine command("npm"), "install"
}
the function looks like this:
//
// find a command with this name in the path
//
String command( String name )
{
def onWindows = (System.env.PATH==null);
def pathBits = onWindows ? System.env.Path.split(";") : System.env.PATH.split(":");
def isMatch = onWindows ? {path ->
for (String extension : System.env.PATHEXT.split(";"))
{
File theFile = new File( path, name + extension);
if (theFile.exists())
return theFile;
}
return null;
} : {path -> def file = new File(path,name);if (file.exists() && file.canExecute()) return file;return null;}
def foundLocal = isMatch(file("."));
if (foundLocal)
return foundLocal;
for (String pathBit : pathBits)
{
def found = isMatch(pathBit);
if (found)
return found;
}
throw new RuntimeException("Failed to find " + name + " in the path")
}

How to define and call custom methods in build.gradle?

As part of my project, I need to read files from a directory and do some operations all these in build script. For each file, the operation is the same(reading some SQL queries and execute it). I think its a repetitive task and better to write inside a method. Since I'm new to Gradle, I don't know how it should be. Please help.
One approach given below:
ext.myMethod = { param1, param2 ->
// Method body here
}
Note that this gets created for the project scope, ie. globally available for the project, which can be invoked as follows anywhere in the build script using myMethod(p1, p2) which is equivalent to project.myMethod(p1, p2)
The method can be defined under different scopes as well, such as within tasks:
task myTask {
ext.myMethod = { param1, param2 ->
// Method body here
}
doLast {
myMethod(p1, p2) // This will resolve 'myMethod' defined in task
}
}
If you have defined any methods in any other file *.gradle - ext.method() makes it accessible project wide. For example here is a
versioning.gradle
// ext makes method callable project wide
ext.getVersionName = { ->
try {
def branchout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD'
standardOutput = branchout
}
def branch = branchout.toString().trim()
if (branch.equals("master")) {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags'
standardOutput = stdout
}
return stdout.toString().trim()
} else {
return branch;
}
}
catch (ignored) {
return null;
}
}
build.gradle
task showVersion << {
// Use inherited method
println 'VersionName: ' + getVersionName()
}
Without ext.method() format , the method will only be available within the *.gradle file it is declared. This is the same with properties.
You can define methods in the following way:
// Define an extra property
ext.srcDirName = 'src/java'
// Define a method
def getSrcDir(project) {
return project.file(srcDirName)
}
You can find more details in gradle documentation Chapter 62. Organizing Build Logic
An example with a root object containing methods.
hg.gradle file:
ext.hg = [
cloneOrPull: { source, dest, branch ->
if (!dest.isDirectory())
hg.clone(source, dest, branch)
else
hg.pull(dest)
hg.update(dest, branch)
},
clone: { source, dest, branch ->
dest.mkdirs()
exec {
commandLine 'hg', 'clone', '--noupdate', source, dest.absolutePath
}
},
pull: { dest ->
exec {
workingDir dest.absolutePath
commandLine 'hg', 'pull'
}
},
]
build.gradle file
apply from: 'hg.gradle'
hg.clone('path/to/repo')
Somehow, maybe because it's five years since the OP, but none of the
ext.someMethod = { foo ->
methodBody
}
approaches are working for me. Instead, a simple function definition seems to be getting the job done in my gradle file:
def retrieveEnvvar(String envvar_name) {
if ( System.getenv(envvar_name) == "" ) {
throw new InvalidUserDataException("\n\n\nPlease specify environment variable ${envvar_name}\n")
} else {
return System.getenv(envvar_name)
}
}
And I call it elsewhere in my script with no prefix, ie retrieveEnvvar("APP_PASSWORD")
This is 2020 so I'm using Gradle 6.1.1.
#ether_joe the top-voted answer by #InvisibleArrow above does work however you must define the method you call before you call it - i.e. earlier in the build.gradle file.
You can see an example here. I have used this approach with Gradle 6.5 and it works.
With Kotlin DSL (build.gradle.kts) you can define regular functions and use them.
It doesn't matter whether you define your function before the call site or after it.
println(generateString())
fun generateString(): String {
return "Black Forest"
}
tasks.create("MyTask") {
println(generateString())
}
If you want to import and use a function from another script, see this answer and this answer.
In my react-native in build.gradle
def func_abc(y){return "abc"+y;}
then
def x = func_abc("y");
If you want to check:
throw new GradleException("x="+x);
or
println "x="+x;

How Do I Call 7zip From Gradle To Unsign A Jar

I want to batch Unsign some jars with in gradle but I don't want to use the ant jar method as it is too slow.
Using the 7zip command line is much faster:
7z.exe d activemq-pool-5.7.0.jar META-INF/SIGFILE.*
Where SIGFILE is the name of the previous signature.
I am trying to do it in gradle like this
println "Unsigning jars"
file(unsignedFolder + "/jars").listFiles().each { File file ->
exec {
workingDir '../tools'
commandLine '7z.exe', 'd', file.absolutePath, 'META-INF/SIGFILE.*'
}
}
However, I get the error:
Starting process 'command '7z.exe''. Working directory: D:\code\project\tools Command: 7z.exe d D:\code\project\build\unsigned\jars\activemq-pool-5.7.0.jar META-INF/SIGFILE.*
:signWebstart FAILED
:signWebstart (Thread[Daemon,5,main]) completed. Took 0.109 secs.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':unsignJars'.
> A problem occurred starting process 'command '7z.exe''
Thanks to this post I realised what it should be.
I am using the command line version of 7zip now - 7za. There is also a unix version at http://p7zip.sourceforge.net/ so I am packaging them both with my script and using something along the lines of the following:
import org.apache.tools.ant.taskdefs.condition.Os
task unSignJars() {
if(Os.isFamily(Os.FAMILY_WINDOWS)) {
println "*** WINDOWS "
exec {
executable "7za.exe"
args "d", "temp.jar", "META-INF/SIGN.RSA"
}
} else if(Os.isFamily(Os.FAMILY_UNIX)) {
println "*** UNIX "
exec {
executable "7za"
args "d", "temp.jar", "META-INF/SIGN.RSA"
}
} else {
println "*** NOT SUPPORTED "
}
}
This method is twice as fast as using Java nio http://thinktibits.blogspot.ca/2013/02/Delete-Files-From-ZIP-Archive-Java-Example.html which in itself is twice as fast as the ant method mention in the OP.
import java.util.*;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.*;
import java.nio.file.StandardCopyOption;
public class ZPFSDelete {
public static void main(String [] args) throws Exception {
/* Define ZIP File System Properies in HashMap */
Map<String, String> zip_properties = new HashMap<>();
/* We want to read an existing ZIP File, so we set this to False */
zip_properties.put("create", "false");
/* Specify the path to the ZIP File that you want to read as a File System */
URI zip_disk = URI.create("jar:file:/my_zip_file.zip");
/* Create ZIP file System */
try (FileSystem zipfs = FileSystems.newFileSystem(zip_disk, zip_properties)) {
/* Get the Path inside ZIP File to delete the ZIP Entry */
Path pathInZipfile = zipfs.getPath("source.sql");
System.out.println("About to delete an entry from ZIP File" + pathInZipfile.toUri() );
/* Execute Delete */
Files.delete(pathInZipfile);
System.out.println("File successfully deleted");
}
}
}
However, unix zip -d is twice as fast again but it isn't portable.

Resources