How to create directory and move WAR file using Gradle - gradle

By default, Gradle puts all WAR files that it creates in build/libs when I run gradle build on a Java project. How do I instruct Gradle to create a directory called dist under build and put all WAR files in that directory (i.e. build/dist)?

I created a new task called moveWar that accomplished what I wanted:
task moveWar(type: Copy) {
from 'build/libs'
into 'build/dist'
}
Then I used build.finalizedBy moveWar to move the WAR file in libs to dist after the build is finished.

Another approach I found:
war.destinationDir = file "$buildDir/dist"

Related

Copying a single file into .jar with a different name using the Gradle task "jar"

How can I copy a single file into .jar with a different name using the Gradle task "jar"?
For example
local:src/main/resources/mypkg_prod.properties → jar-file:/mypkg.properties
You can copy the file into the jar and then do a rename.
This is a link to some information on the rename method from the Gradle docs:
https://docs.gradle.org/current/userguide/working_with_files.html#sec:renaming_files
Example:
jar {
from 'my_file.txt'
rename 'my_file.txt', 'my_super_file.txt'
}
Note: If your file is already being included in the jar, just add the rename line to the jar task:
jar {
rename 'mypkg_prod.properties', 'mypkg.properties'
}

Gradle: Copy one specific file from main project directory into subproject/build/libs

Searching in vain for examples on this humble task, none worked, I need to learn how to use gradle to copy one specific file from the main directory of a gradle project (containing subprojects) and into the /build/libs folder of one of the subprojects. Structure is this:
mainProject folder
file.txt
Subproject folder
build folder
libs folder
(I want to copy file.txt here)
This copy process shall be in the build.gradle (for the Subproject) file. I am using Android Studio and the subproject is a pure java application.
Thanks for any help.
You need to implement a custom task of type Copy to copy the file to the target directory. To access the file in the root project, you may use the rootProject property of the subproject.
task copyFileFromRootProject(type: Copy) {
from "${rootProject.projectDir}/file.txt"
into 'build/libs'
}

How to create a Zip file using gradle 2.3

I have my archive at location myProject/unzip and need to:
Zip the contents within the unzip folder and create a zip file with some name at any location within the project.
Using gradle 2.3.
Can anyone help me for this?
I am not sure what you are doing differently than the examples in the documentation from the link I gave in the comments. But this works for me with Gradle 2.3:
task myZip(type: Zip){
from "$projectDir/unzip"
archiveName = "my-zip.zip"
destinationDir = buildDir
}
The input folder is called unzip and needs to present as a child in the project folder. It outputs a file my-zip.zip in the build folder.
Be sure that there actually are some resources located in the from path, or Gradle might skip it.

Gradle: Include dynamically generated file in build?

During my gradle build, I generate a temporary buildinfo.properties file containing things like Git commit info, build time, etc. I would like to include this file in my output *.jar / *.war files as a resource. However, I do not want to put this file in my project src/ folder (this would require fiddling with .gitignore and in general it just seems unnecessary to me). Ideally, the developer shouldn't even see this file at all, it should just be contained in the output archive.
How would you include a dynamically generated text file in a Gradle build?
Add that file in a jar task (Kotin DSL):
tasks {
val jar by getting(Jar::class) {
from("build/buildinfo.properties")
}
}
It will add build/buildinfo.properties file (assuming you generate it there with another taks) to the root of your JAR.
For dynamically generated file, standard gradle way to process resources is the gradle task called processResources. You can do something like this:
processResources {
dependsOn taskThatGeneratesYourBuildinfo
from("build/buildinfo.properties") {
into("desired/path/in/jar")
}
}

gradle main task to zipup sub-projects distribution files

I have a gradle project which at main is using java plugin. At subproj1 - thru subproj4 the build.gradle files are also using application plugin. It works well in sub projects to compile the jars (with a main class) and create a distribution zipfile (using resources and dist files).
Now I want to build a 'main' dist zip file, comprising of the contents of all those subproj contents. I found I can run installDist to unzip to each of the subprojects build/install/subprojN
Now at a loss howto in the main only, have a task and/or dependency to create a "main" dist zip file containing: subproj1/** subproj2/** subproj3/** subproj4/**
My thoughts are to do a copy from('.').include('subproj*/build/install//')
then zip that up. but havent figured out howto add the task only at main level, plus have it not complain: NO SOURCE
thanks in advance
Here's an example that just uses two sub-projects, and does not show the compilation of the main code. That stuff should be easy to add. The key to the solution is the zip and zipfileset via the AntBuilder.
Consider this in the main build.gradle:
task uberBuild(dependsOn: ['subProj1:build',
'subProj2:build']) {
doLast {
ant.mkdir(dir: "${projectDir}/dist")
def PATH = "build/distributions"
ant.zip(destfile: "${projectDir}/dist/uber.zip") {
zipfileset(src: "${projectDir}/subProj1/${PATH}/subProj1.zip")
zipfileset(src: "${projectDir}/subProj2/${PATH}/subProj2.zip")
}
}
}
This will write ~/dist/uber.zip, which has the contents:
$ jar tf dist/uber.zip
subProj1/
subProj1/bin/
subProj1/lib/
subProj1/bin/subProj1
subProj1/bin/subProj1.bat
subProj1/lib/subProj1.jar
subProj2/
subProj2/bin/
subProj2/lib/
subProj2/bin/subProj2
subProj2/bin/subProj2.bat
subProj2/lib/subProj2.jar

Resources