I just observed a very weird behaviour from a Gradle Tar task.
Let's take a simple example, 2 files :
/tmp/test$ ls
test1.txt ##test2##
Here is a simple Tar task :
task('testHash', type: Tar) {
from "/tmp/test"
extension = 'tar.gz'
compression = Compression.GZIP
}
The file ##test2## is skipped for some reason, after running gradle testHash :
/path/to/gradle/project/foo$ tar tvf build/distributions/foo-1.0.tar.gz
test1.txt
It seems to happen when the filename is containing # character both at the beginning and the end.
A regular tar is working well :
/tmp/test$ tar czvf test.tar.gz *
test1.txt
##test2##
/tmp/test$ tar tf test.tar.gz
test1.txt
##test2##
I am using Gradle 4.1. Any explanation ?
Thanks to Opal's comments, I adjusted my searches and found a workaround. There is maybe a cleaner way but this one works for me
task('testHash', type: Tar) {
doFirst {
org.apache.tools.ant.DirectoryScanner.defaultExcludes.each {
DirectoryScanner.removeDefaultExclude it
}
}
from "/tmp/test"
extension = 'tar.gz'
compression = Compression.GZIP
}
FYI, here are default excludes
There are a set of definitions that are excluded by default from all
directory-based tasks. As of Ant 1.8.1 they are:
**/*~
**/#*#
**/.#*
**/%*%
**/._*
**/CVS
**/CVS/**
**/.cvsignore
**/SCCS
**/SCCS/**
**/vssver.scc
**/.svn
**/.svn/**
**/.DS_Store
Ant 1.8.2 adds the following default excludes:
**/.git
**/.git/**
**/.gitattributes
**/.gitignore
**/.gitmodules
**/.hg
**/.hg/**
**/.hgignore
**/.hgsub
**/.hgsubstate
**/.hgtags
**/.bzr
**/.bzr/**
**/.bzrignore
Related
I have a huge maven project, lot of people are using it. I'm currently working on converting it to gradle. One of the last steps will be that I merge the gradle files, and delete the pom.xml files. But I'd like to add a gradle task to clean the maven target directories (of all the sub-projects). In shell I would do something like:
find . -type d -name target -exec rm -rf "{}" \;
But I prefer this to be a gradle task. How do I add it? This is what I tried but it doesn't delete anything:
task cleanMaven(type: Delete) {
delete fileTree('.').matching { include '**/target/**' }
}
below will handle all modules of root project and prints true if a target dir existed and is deleted
allprojects {
task mvnClean {
doFirst {
def targetPath = project.projectDir.toString() + '/target'
println "target dir exists: ${Files.deleteIfExists(Paths.get(targetPath))}"
}
}
}
Based on #PrasadU's answer, but this also deletes all the contents of the target/ directories:
allprojects {
task mvnClean {
doFirst {
def targetPath = project.projectDir.toString() + '/target'
project.delete(files("${targetPath}"))
}
}
}
I have a folder with a lot of projects inside it (too much to manually write build files for them)
The projects are mostly in a flat layout:
root
-project 1
-project 2
-project 3
-project 4
-project 5
( -project 5.1)
But can be nested as shown above, and I need to account for this.
Ideally the following should happen:
I can run user#user:/root gradle build and every project in the directory shoudl be built as long as it contains a gradle build file
if a build fails just continue with the next one
How can I make this possible ?
How about this one-liner (not tested):
find . -maxdepth 1 -type d \( ! -name . \) -exec bash -c "cd '{}' && gradle build || true" \;
Or, more verbose:
dirs=($(find . -type d))
for dir in "${dirs[#]}"; do
cd "$dir"
gradle build || true
done
I came up with a working solution:
def flist = []
// change to you workspace name
new File('./Workspace').eachDir {
//blacklist any folders you want
if (it.name !='.gradle' && it.name != 'master' && it.name!= 'Build-All') {
flist << it.name
}
}
// build task objects
flist.each { folder ->
task "${folder}"(type: GradleBuild) {
buildFile = "./Workspace/"+ folder + "/build.gradle"
dir = './' + folder
tasks = ['build']
}
}
// create super task
task (all, dependsOn: flist) {
}
You need to invoke it as such in the root directory: gradle :all --continue this has the benefit that any failing project builds will not halt the other builds.
Another bonus is that gradle gives a neat report about all failing builds.
I have a directory structure like this:
file1.txt
file2.txt
dir1/
file3.txt
file4.txt
I want to use Gradle to copy that entire structure into another directory. I tried this:
task mytest << {
copy {
from "file1.txt"
from "file2.txt"
from "dir1"
into "mytest"
}
}
But this results in the following:
mytest/
file1.txt
file2.txt
file3.txt
file4.txt
See, the copy from dir1 copied the files in dir1, whereas I want to copy dir1 itself.
Is it possible to do this directly with Gradle copy?
So far, I have only been able to come up with this solution:
task mytest << {
copy {
from "file1.txt"
from "file2.txt"
into "mytest"
}
copy {
from "dir1"
into "mytest/dir1"
}
}
For my simple example there's not much to it, but in my actual case there are many directories I want to copy, and I'd like to not have to repeat so much.
You can use . as the directory path and include to specify, which files and directories you want to copy:
copy {
from '.'
into 'mytest'
include 'file*.txt'
include 'dir1/**'
}
If both from and into are directories, you'll end up with the full copy of the source directory in the destination directory.
I know this is a bit late, but I tried #Andrew solution above and it copied everything inside the directory.
"." is not required nowadays to represent a direct in gradle.
So I did some research
and found this
and created the following code (with Up-to-date check) based on it:
task resourcesCopy() {
doLast {
copy {
from "src/main/resources"
into "./target/dist/WEB-INF/classes"
}
copy {
from "GeoIP2.conf"
into "./target/dist/WEB-INF"
}
}
}
I don't know how long this syntax has been around, but it seems a little clearer.
task copyToRelease(dependsOn: [deletePreviousRelease], type: Copy) {
from('build/dist') {
include '**/*.*'
}
destinationDir(new File('../htmlrelease/src/main/webapp/canvas'))
}
Maybe also helpful: Usage of fileTree to copy an entire directory recursively, e.g.,
task mytest << {
copy {
from fileTree('.')
into "mytest"
}
}
I have a gradle build script in which I'm using the 'Zip' task to generate a zip archive directly from a source directory.
In addition to copying over all the files inside the source directory structure into the zip archive, I'm looking for a way to include a dynamically generated file that's not in the source directory.
Do you guys know how I can do this?
Here's pseudo code of what I want to do:
task('archive', type: Zip) {
...
from 'source'
newFile('dynamically-generated.txt', 'dynamic file content')
}
And here are the source and destination structures:
[i71178#bddev01-shell01 ~]$ tree source/
source/
├── file1.txt
├── file2.txt
└── file3.txt
0 directories, 3 files
[i71178#bddev01-shell01 ~]$ unzip -l destination.zip
Archive: destination.zip
Length Date Time Name
--------- ---------- ----- ----
0 02-26-2016 16:56 source/
0 02-26-2016 16:56 source/file2.txt
0 02-26-2016 16:56 source/dynamically-generated.txt
0 02-26-2016 16:56 source/file1.txt
0 02-26-2016 16:56 source/file3.txt
--------- -------
0 5 files
Combining my comments above and your getTemporaryDir comment:
task generateThenZip()<<{
def tempDir = getTemporaryDir()
newFile("$tempDir/dynamically-generated.txt", 'dynamic file content')
zip{
from 'source'
from tempDir
}
}
This is what I ended up doing:
subprojects {
apply plugin: 'myPlugin'
//The 'build' task is set up by the plugin
build {
//Customization to keep consistent with artifacts being currently generated.
doFirst {
new File(getTemporaryDir(), 'artifact-fullname.txt').text = "${project.name}-${project.version}\n"
}
archiveName = "${project.name}.${project.build.extension}"
from getTemporaryDir()
}
}
Thanks!
I have managed to achieve it quite easyly on Gradle 6.6.1
by just creating the file in the from section.
distributions {
main {
contents {
into("/") {
from {
if (!buildDir.exists()) {
buildDir.mkdir()
}
def f = new File("$buildDir/all")
f.text = "project_version: ${project.version}\n"
f
}
}
...
}
}
}
btw. I think $buildDir is safer than /tmp
I would like to create a package that contains a file but renames it inside the package.
For example:
Rake::PackageTask.new("rake", "1.2.3") do |p|
p.package_files.include("aa.rb")
end
I would like aa.rb to be named bb.rb inside the package.
How can I do this elegantly?
Looking at the PackageTask source, it seems you could define a new task (say rename_files) that depends on on the p.package_dir_path task defined by Rake::PackageTask. In rename_files task you can rename the file link(s) which package_dir_path task made in package_dir. Then you add your new rename_files task as a dependency for each of the "#{package_dir}/#{[tar|zip|etc]_file}" task targets you care about.
With these dependencies, the order of operations should become:
set up package_dir with links to source files from package_files
rename links with your injected dependency
execute archive creation command on package_dir
If this isn't clear enough to get you there, I'll try and post some actual code later.
[LATER] Ok, some code. I made a sample project which looks like this:
$ find .
.
./lib
./lib/aa.rb
./lib/foo.rb
./Rakefile
And in the Rakefile, I define a package task as:
require 'rake/packagetask'
Rake::PackageTask.new('test', '1.2.3') do |p|
p.need_tar = true
p.package_files.include('lib/**/*')
task :rename_files => [ p.package_dir_path ] do
fn = File.join( p.package_dir_path, 'lib', 'aa.rb' )
fn_new = File.join( p.package_dir_path, 'lib', 'bb.rb' )
File.rename( fn, fn_new )
end
[
[p.need_tar, p.tgz_file, "z"],
[p.need_tar_gz, p.tar_gz_file, "z"],
[p.need_tar_bz2, p.tar_bz2_file, "j"],
[p.need_zip, p.zip_file, ""]
].each do |(need, file, flag)|
task "#{p.package_dir}/#{file}" => [ :rename_files ]
end
end
The logic here is what I explained above. Running it, you can see that the hard link made in the package dir is renamed from "aa.rb" to "bb.rb", then we tar the directory and viola!
$ rake package
(in /Users/dbenhur/p)
mkdir -p pkg
mkdir -p pkg/test-1.2.3/lib
rm -f pkg/test-1.2.3/lib/aa.rb
ln lib/aa.rb pkg/test-1.2.3/lib/aa.rb
rm -f pkg/test-1.2.3/lib/foo.rb
ln lib/foo.rb pkg/test-1.2.3/lib/foo.rb
cd pkg
tar zcvf test-1.2.3.tgz test-1.2.3
a test-1.2.3
a test-1.2.3/lib
a test-1.2.3/lib/bb.rb
a test-1.2.3/lib/foo.rb
cd -
Here's the tar manifest with "bb.rb" instead of "aa.rb":
$ tar tf pkg/test-1.2.3.tgz
test-1.2.3/
test-1.2.3/lib/
test-1.2.3/lib/bb.rb
test-1.2.3/lib/foo.rb