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

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'
}

Related

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

How to create directory and move WAR file using 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"

How do I prevent Gradle from building a non-project directory?

In the Gradle samples (included with version 2.2.1) there is a java/multiproject project.
The settings.gradle file defines the following projects:
include "shared", "api", "services:webservice", "services:shared"
Note that services is not itself a project, merely a directory which contains the webservice and shared projects.
When I run the command gradle build from the root directory, I notice that after gradle successfully builds it creates inside the /services directory a /build directory containing /lib and a /tmp directories.
Inside of /services/build/lib is a jar: services-1.0.jar which contains very little; specifically just a META-INF/MANIFEST.MF file containing:
Manifest-Version: 1.0
provider: gradle
So what is causing Gradle to build a jar for this non-project? And how can I prevent this behavior in my similarly structured multiproject project?
/services isn't a project, I don't want to create anything inside /build folder at all. Yes I could just delete it, but I would like to avoid the unnecessary work of building this jar/running any tasks on this non-project in the first place.
To be honest I've no reasonable idea why gradle builds this folder. I guess that because it's a kind of a transient folder. However it can be excluded by adding the following piece of code to main build.gradle script:
project(':services').jar { onlyIf { false } }
Desired effect (services.jar elimination) can be also obtained with the following settings.gradle content:
include "shared", "api", "services/webservice", "services/shared"
File instead of project paths are included.
My guess would be that this is a combination of the next 2 gradle rules:
When you're including subprojects in the build.settings file using the include keyword according to Gradle Documentation here:
the inclusion of the path 'services:hotels:api' will result in
creating 3 projects: 'services', 'services:hotels' and
'services:hotels:api'.
In simple words, this means that the inclusion of services::webservice will also build the services project
The bulid.gradle file in your root that applies the 'java' plugin. According to Gradle Documentation here every configuration defined in the root.gradle takes effect for all sub projects. This means that it will also hold as the default configuration for the services project. As the 'java' plugin was applied a jar will be created, but as there is no src/main folder under the services directory nothing will be compiled and the jar will include only a META-INF/MANIFEST.MF file.

Resources