Gradle zip task ignores files having filename starting with .# - gradle

I have a simple gradle zip task,
I found out any files starting with .# from input folder are ignored
task zipIt(type: Zip) {
from 'input/'
archiveName = 'output.zip'
}
Does someone knows why is so? And how can I override this behavior so I include those files in the zip?
Later edit:
Adding the file pattern explicitly doesn't seem to help
On the other hand, looking over #opal links led me to a solution:
import org.apache.tools.ant.DirectoryScanner
task zipIt(type: Zip) {
doFirst{
DirectoryScanner.removeDefaultExclude("**/.#*")
}
from 'input/'
archiveName = 'output.zip'
}

Expounding on Opal's answer, Gradle uses ANT's default excludes which are as follows:
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
The .# is by default excluded. Updating your build.gradle file with a task similar to the following should allow you to overwrite the default excludes.
task copyPoundFiles(type: Copy) {
from '/path/to/files'
into '/dest/for/files'
include '**/.#*'
}

As far as I remember gradle used the same default excludes as ant does. Have you tried including the file explicitly?
Please have some further reading here and here.

Related

Gradle bootJar task skip encoding some file

Here is a keystore file in springboot resources directory, when I ran bootJar task I found this file got bigger. I found it was because of a change in a way it was encodes.
I knew maven can solve this issue by adding nonFilteredFileExtension config. But how to solve this problem in Gradle
I solved this problem by setting processResource config.
First I set DupicatesStrategy to EXCLUDE to make sure the first founded file can be inclueded. After that I exclude and then include that file to achieve the purpose.
processResources {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from('/src/main/resources') {
exclude("*.keystore")
}
from('/src/main/resources') {
include("*.keystore")
}
}

Include dependencies in Zip file created with Gradle's Zip task using 'into' (before v4 it worked fine)

I am working in an old project which was using Gradle Wrapper v3.2.1, but I want to update it to the latest version (currently v5.4.1).
I have tried updating it to v4.10.2 first but it fails too, so I guess it is something that was not backwards compatible between v3->v4.
The code we have in our build.gradle is:
task buildZip(type: Zip) {
group 'Build'
description 'Assembles a zip archive containing the main classes.'
baseName = "someName"
from compileJava
from processResources
into('lib') {
from configurations.runtime
}
}
Using gradle v3 it included all the libraries (as .jar files) in the .zip file under "lib/" folder, but if I use v4 or later, it does not fail, but it does not include the libraries neither. I have achieved to get the .class files of the dependencies, but that does not work for what I need (AWS Lambda function).
Any idea on how to get the .jar dependencies into the .zip file?
Cheers!
Francisco Robles Martin
So, thanks to Opal comment, I kept looking for a bit more and got a solution, but it seems to not be very correct as I am forcing implementation to allow be resolved:
configurations.implementation.setCanBeResolved(true)
task buildZip(type: Zip) {
group 'Build'
description 'Assembles a zip archive containing the main classes.'
baseName = "someName"
from compileJava
from processResources
into('lib') {
from configurations.implementation
}
}
It works, but I guess there should be a better way to do it without the first line.

Compile task is running successful but not generating any class files

I want to implement a gradle build script which compiles some java classes and copy it to to a tomcat directory. I dont want to use Gradle Java plugin since it does many things which are not relevant. I want to define my own compile & deploy tasks which does it. I have implemented it as below -
task compile (type: JavaCompile) {
source = fileTree('$srcdir')
destinationDir = file('$builddir')
classpath = files('lib')
sourceCompatibility = '1.8'
}
task deploy (type: Copy) {
dependsOn compile
from fileTree('build') {
include fileTree('classes')
}
from fileTree('lib') {
include '*'
}
into '${tomcathome}//${projectname}'
}
I have not touched deploy task yet. When i am running compile tasks it is running successful but not generating any class files. I am expecting it to be generated under /build directory.
Please suggest.
Thanks
To summarise the comments in the answer, you need to use GString like #lu.koerfer stated
this way it will always be interpreted as the literal location (a subfolder called $srcdir in this case)
This is needed when using variables inside a string, if don't need to use it in a string then don't (then you don't need a dollar sign).
Not sure how your variables are defined but for build and source directories you should ideally use Gradle provided variables
buildDir to point the build directory
sourceSets.main.java.getSrcDirs() to get source directories (depending on the project structure)
or sourceSets.main.java.srcDirs but note this is going to return the collection of your source directories, depending how you specified your sourceSets, or if you haven't at all then by default is going to return a maven convention structure src/main/java
For some global variables please read about Ext variables

How to reference the output file of an other Gradle task as ziptree?

I have a ShadowJar and a Proguard task that produce two jar files in my Gradle build.
task obfuscate(type: ProguardTask) {
outjars ..
}
shadowJar {
...
}
task release(type: Jar) {
from shadowJar
from obfuscate
classifier 'all'
}
My problem is that in this case the release jar file contains the shadow jar and the obfuscated jar files as two files in the jar itself. I would like to make these as zipTree inputs.
My problem is that I don't know how to turn the task reference into a zipTree of the actual output of that task.
My attempts lead me to from zipTree(shadowJar.outputs.getFiles()) but this still fails:
> Cannot convert the provided notation to a File or URI: task 'shadowJar' output files.
The following types/formats are supported:
- A String or CharSequence path, for example 'src/main/java' or '/usr/include'.
- A String or CharSequence URI, for example 'file:/usr/include'.
- A File instance.
- A URI or URL instance.
How can I refer the output jar file of the preceding tasks properly?
Gradle docs on zipTree:
Creates a new FileTree which contains the contents of the given ZIP file. The given zipPath path is evaluated as per Project.file(java.lang.Object).
Therefor, zipTree can only handle single files, but outputs.files provides a file collection. This reveals two options:
You can assume the task to provide a file collection containing only one file. In this case, you can simply take your approach and use the getSingleFile method:
from zipTree(shadowJar.outputs.files.singleFile)
As long as you use a task of type Jar, this should not cause any problems, because any task of this type will only create one jar file. But if you use other task types, that may have multiple output files (e.g. custom tasks), the getSingleFile method will fail.
The second approach is to iterate over each output file, unzip it and add the revealed contents via from:
shadowJar.outputs.files.each { outFile ->
from zipTree(outFile)
}
I am not quite sure if the following works, but maybe its an even more Gradle-style solution:
from shadowJar.outputs.files.collect{ zipTree(it) }

Exclude base directory inside Gradle tar/zip artifacts

The maven assembly plugin has an includeBaseDirectory option that (when set to false) avoids having a single top-level directory inside the tar/zip artifact with the same name as the artifact itself.
I'd like to achieve the same result with Gradle, but I don't see how. I'm using a configuration like this:
task distTar(type: Tar) {
compression Compression.GZIP
extension "tar.gz"
}
I don't see any options for the Tar task that do what I want. How can I exclude the base directory in my archive with Gradle?
By reconfiguring the distribution plugin (which gets implicitly applied by the application plugin) you can simply do (in Kotlin DSL):
distributions {
main {
contents {
into("/")
}
}
}
This affects the output in both Tar and Zip formats.
(Disclaimer: This answer is loosely based on this Gradle forum post).
Ok, I figured it out. It was simpler than I thought. To copy the library dependencies into lib at the root of the archive, I use a CopySpec:
task distTar(type: Tar) {
into('lib') {
from libsDir
include '*.jar'
}
}
Similar CopySpecs can be used to copy e.g. bin and conf directories.

Resources