The task processResources from the Java plugin somehow "magically" executes other custom Copy-tasks and copies files around.
I would like to know: What does it copy? Why does it copy? How to omit it?
Related
I want to store some additional files in the JAR that gets created. Those files are in a directory that is a subdirectory of a repository which is pulled in via a git submodule.
I want to copy that submodule to my src resources directory before compiling, but I also want to make sure that any old files at that location are removed first.
How can that be achieved best with Maven plugins? I did not find any option to remove any destination files with the copy-resources goal of the maven-resources-plugin and I could not get the maven-clean-plugin to run right before the copy-resources either. So how does one accomplish such a trivial task with Maven?
UPDATE: as mentioned above, the reason why I want to do this is because what is copied should become part of what gets added to the resulting jar (and could potentially be part of what gets compiled). So I need to copy these files into the src directory and NOT the target directory. What should get copied before each build is the input to the build, not an additional output.
There is one flaw in your approach, and it probably explains most of the obstructions you encountered.
During a build, the only directory in which you may write is target. Copying files to src or changing them is strictly discouraged.
The target folder is erased by clean, so no need to tidy up yourself or to manage old files.
I have some .tar.gz-Archives and would like to only unpack the Tarball to create a sha256-checksum hash of the .tar-File. (The reason for this is that the archive will be un- and repacked later on, as we are generating patch-files.)
Now this seems like an easy task but I'm stuck. There are either Gradle examples for:
getting the unpacked tarTree (with a Gradle Copy-Task and tarTree(resources.gzip('model.tar.gz')) (from documentation: working with files)
unzipping Files (with zipTree), which does not work with gzipped files
Both approaches do not work, since I need to create a checksum of the .tar-File itself. Unfortunately I can't use commandLine or gunzip as the tasks should run on both Windows and Linux.
The only solution I can imagine of right now is unpacking the tar.gz to a fileTree and repacking it to a tar-file, but I'm not even sure the checksum would be the same.
Is there really no way to do this directly?
Finally I got it to work with help from a colleague.
Using resources.gzip(), which returns a ReadableResource, we can copy the resulting InputStream into a .tar-file with IOUtils.copy:
file("test.tar").withOutputStream { outputFile ->
IOUtils.copy(resources.gzip(file("test.tgz")).read(), outputFile)
}
We also needed to add Apache commons ("commons-io:commons-io:2.6") as a dependency.
I have a multi project Gradle build script that runs successfully on Windows 10. It reads and updates a Version.properties file that is located away from project managed directories.
All file manipulations are done using Gradle/groovy. After the Version file has been read, incremented and rewritten it is copied to a build/classes directory where it will be picked up by subsequent jar and shadowjar tasks.
Everything works as advertised if I invoke gradle as follows:
gradle build shadowjar ... etc.
However, if I invoke the clean task prior to build the file is read and incremented properly but the copy of the file fails silently.
The command used is:
gradle clean build shadowjar
My suspicion is that gradle does not wait for the clean task to finish prior to starting the build task. The file gets read and incremented but meanwhile, the multi-project clean activities have not yet finished. I have tried variations on dependencies{} blocks, doFirst{} and doLast{} to try and push the file copy back further in the build process. My main requirement is to have the Version.properties file in place prior to the jar or shadowjar task executing. I'm suspicious of trying to write into gradle's build/ directories in that it might not be possible to put anything into the build directories while gradle is performing its activities. Is there any way to ensure that the Version.properties file (or any generated file) gets copied? Or is there another location that I can use that will not be blown away by gradle at clean time yet still get picked up in the build:jar / build:shadowjar?
You are not supposed to call gradle clean 99.99% of the time, it is redundant due to gradle's incremental build feature. So as long as you correctly define your task inputs and outputs and start from ground up in each task, the problem solves itself.
Anyway in your case the wrong order could be caused by dependency between clean and other tasks, is there any?
I have found a way to write out a generated Version.properties file that will get picked up by the jar and shadowjar tasks. Use the gradle copy task and place the revised Version.properties file into a resources directory. The build activity includes the files found in resources/ in subsequent tasks (jar, shadowjar, test, etc.) My suspicion is that because clean blows away build directories gradle assumes that the activity has fully completed when it starts the build. I think that I've proven that this is not the case. doFirst{}, doLast{} and dependencies{} do not seem to work as modifiers to clean build.
If I have a simple gradle file that contains
task mytry(type:Copy){
from '/tmp/try'
into '/tmp'
}
and I have a file /tmp/try/1.txt
I expect this to copy /tmp/try/1.txt to /tmp/1.txt
However, it scans all of /tmp (the into param) before doing the copy.
There are files in /tmp (such as /tmp/.X11-unix/X0) that gradle cannot access, and so it fails.
How to I tell the copy task to not scan the into directory?
I've tried 'include' 'exclude **'
(This is an example of my real problem, the real issue is that in my project the 'into' is a massive directory with many subdirectories and gradle is scanning every single file which takes forever)
gradle version 1.12 (v2 is not an option yet)
How would I copy dependencies to somewhere in the project folder with Gradle?
I've setup SBT to do this, but I'm not sure about Gradle
Your use case makes a big impact on the ways you could do this. But, answering generically you could do something like the following to copy from a collection to a directory.
task copyDeps(type: Copy) {
from configurations.runtime
into "${buildDir}/deps"
}
Tweak to meet your use case. If you are trying to create a Java application take a look at the application plugin.