I try to delete directory in 'foo/dir' with name 'public-someHash'. 'SomeHash' was created dynamically (eg 'dsflsdfn') and always new. I tried to use 'fileTree' but directory still present. There is my code:
tasks.create(name: 'delete', type : Delete) {
delete fileTree(dir: 'foo/dir/', include: 'public-*/**')
}
What is wrong with my mask?
UDP: I have similar task in Ant and all works fine:
<target name="delete">
<delete includeemptydirs="true">
<fileset dir="foo/dir/">
<include name="public-*/**"/>
</fileset>
</delete>
</target>
EDIT: Apologies, as original answer was based on a misreading of the question.
Here's one way to do it, but not the most elegant:
task myDelete(type: Delete) {
def files = new HashSet()
new File('foo/dir').eachFile { file ->
if (file.isDirectory() && (file.name ==~ /public-.*/)) {
files << file
}
}
delete files
}
Related
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"
}
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
for some reasons I don't use the java plugin for gradle, but I invoke ant.javacdynamically. How can I build a dynamic javac include() based on a list ?
for example:
def srcToCompile=["**/File1.java","**/File2.java","**/FileN.java"];
(...)
ant.javac(
destdir: tmpDir,
srcdir: srcDir
includeantruntime:false,
failonerror: true,
fork: true,
classpath : classpath1,
debug: true
) {
include(name: srcToCompile) //<< DOESN'T WORK, I also tested srcToCompile.join(":")
}
thanks.
EDIT: by 'doesn't work', I mean ant.javac doesn't interpret a List or a colon-separated-string: no source is found and nothing is compiled. ant.javac expects something like
include(name:"**/File1.java")
include(name:"**/File2.java")
include(name:"**/FileN.java")
but I want to generate this list of include when gradle is invoked.
If you look an ant javac docs you'll see that includes and include both accept a string but you are trying to pass a collection
Eg:
<javac destdir="${build}"
classpath="xyz.jar"
debug="on">
<src path="${src}"/>
<src path="${src2}"/>
<include name="mypackage/p1/**"/>
<include name="mypackage/p2/**"/>
<exclude name="mypackage/p1/testpackage/**"/>
</javac>
and
<javac srcdir="${src}"
destdir="${build}"
includes="mypackage/p1/**,mypackage/p2/**"
excludes="mypackage/p1/testpackage/**"
classpath="xyz.jar"
debug="on"/>
In Gradle this would be
ant.javac(includes: srcToCompile.join(','), ...)
or
ant.javac(
...
) {
srcToCompile.each {
include(name: it)
}
}
I'm used to working with Makefiles but my current project uses .qbs files. How do I run a simple terminal command through qbs without creating or requiring files? Similar to a phony rule in make.
The following works and shows "awesome" in my terminal.
import qbs 1.0
Project {
name: "cli"
Product {
name: "helloworld"
type: "application"
files: "TEST.c"
Depends { name: "cpp" }
}
Product {
type: ["custom-image"]
Depends { name: "helloworld" }
Rule {
inputsFromDependencies: ["application"]
Artifact {
fileTags: ["custom-image"]
}
prepare: {
var cmd = new Command("echo", "awesome")
return cmd
}
}
}
}
However I have to touch my dummy TEST.c file before each run. Without the helloworld dependency the Rule does not run.
Any ideas? Thank you very much!
It's buried in the documentation in a very non obvious place and further obscured by Command (which is not the correct way, lol). I've had your problem too.
What you need is this:
http://doc.qt.io/qbs/jsextension-process.html
I'm not sure what your end goal is but you could use Transformer{} instead of a Rule{}. The biggest difference between a Rule{} and a Transformer{} is you don't need any inputs for the Transformer{} to run.
Also see Transformer.alwaysRun property.
https://doc.qt.io/qbs/transformer-item.html
I'm having trouble running from ant. When I run straight from the class
file as follows it compiles and runs
Jasons-MacBook-Pro:src js$ java Hw5
Student ID: 1
First Name: Jason
Last Name: S
Phone: 555-220-5169
Email: js#ucsd.edu
Personal Tagline: Never say never
The following line of code is used when it runs correctly as above
Connection connection = DriverManager.getConnection("jdbc:derby:/Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/lib/Hw5Db");
my code
import java.sql.*;
import java.util.Enumeration;
public class Hw5{
public static void main (String [] args){
try{
Connection connection = DriverManager.getConnection("jdbc:derby:/Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/lib/Hw5Db");
//Connection connection = DriverManager.getConnection("jdbc:derby:Hw5Db");//ant file code
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM STUDENT");
while(resultSet.next()){
int studentID = resultSet.getInt("STUDENT_ID");
String firstName = resultSet.getString("FIRSTNAME");
String lastName = resultSet.getString("LASTNAME");
String phone = resultSet.getString("PHONE");
String email = resultSet.getString("EMAIL");
String mantra = resultSet.getString("PERSONAL_TAGLINE");
System.out.println("Student ID: " + studentID + "\n" +
"First Name: " + firstName + "\n" +
"Last Name: " + lastName + "\n" +
"Phone: " + phone + "\n" +
"Email: " + email + "\n" +
"Personal Tagline: " + mantra + "\n");
}
resultSet.close();
statement.close();
connection.close();
}
catch(SQLException sqle){
System.err.println("SQL Exception: " + sqle);
}
}
}
but when I try from my build.xml i change the following line of code to
Connection connection = DriverManager.getConnection("jdbc:derby:Hw5Db");
because i think database jar file (Hw5Db) is in the classpath of my build.xml
my build.xml
<?xml version="1.0"?>
<project name="Hw5" default="compile" basedir=".">
<property environment="env"/>
<property name="src" value="${basedir}/src"/>
<property name="bin" value="${basedir}/bin"/>
<property name="lib" value="${basedir}/lib"/>
<property name="doc" value="${basedir}/doc"/>
<property name="build" value="${basedir}/build"/>
<target name="prepare" description="Setting up temporary directory to support build">
<mkdir dir="${build}"/>
<mkdir dir="${bin}"/>
</target>
<target name="compile" depends="prepare" description="compile java sources">
<javac srcdir="${src}" destdir="${build}" includes="**/*.java" listfiles="yes" includeantruntime="false">
</javac>
</target>
<target name="deploy" depends="compile">
<jar destfile="${bin}/Hw5.jar" basedir="${build}"/>
<jar destfile="${bin}/Hw5Db.jar" basedir="${lib}"/>
</target>
<target name="run" depends="deploy" description="run the project">
<java fork="true" classname="Hw5">
<classpath path="${bin}/Hw5.jar"/>
<classpath path="${bin}/Hw5Db.jar"/>
</java>
</target>
<target name="clean">
<delete dir="${build}"/>
<delete dir="${bin}"/>
</target>
</project>
Jasons-MacBook-Pro:HW5_JDBC jsteindorf$ ant run
Buildfile: /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/build.xml
prepare:
[mkdir] Created dir: /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/build
[mkdir] Created dir: /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/bin
compile:
[javac] Compiling 1 source file to /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/build
[javac] /Users/js/Desktop/JavaDevelopmentAnt/HW5_JDBC/src/Hw5.java
deploy:
[jar] Building jar: /Users/jsteindorf/Desktop/JavaDevelopmentAnt/HW5_JDBC/bin/Hw5.jar
[jar] Building jar: /Users/jsteindorf/Desktop/JavaDevelopmentAnt/HW5_JDBC/bin/Hw5Db.jar
run:
[java] SQL Exception: java.sql.SQLException: No suitable driver found for jdbc:derby:Hw5Db
BUILD SUCCESSFUL
Total time: 2 seconds
I'm trying, I just can't get it to work
Not sure but maybe you need register the driver before use it.
Class.forName("class_of_driver").getInstance();
And after:
Connection connection = DriverManager.getConnection("jdbc:derby:./Hw5Db");
The duplicate classpath on the java task looks a bit strange to me. Try the following instead:
<java fork="true" classname="Hw5">
<classpath path="${bin}/Hw5.jar:${bin}/Hw5Db.jar"/>
</java>
i fixed the issue, the build.xml wasn't finding to the database drivers
classpath path="${env.DERBY_HOME}/lib/derby.jar
classpath path="${env.DERBY_HOME}/lib/derbytools.jar
and the connection path to the database should have been
jdbc:derby:./lib/Hw5Db