Create zip of the source code in gradle - gradle

I want to create a zip of my entire code base using a gradle task. I am facing one issue to get the parentDir . I have tried using project , project.rootDir, project.rootProject, projectDir, project, sourceSets*.allSource . I am not getting any correct output. I f I use project alone, it gives me a zip containing only the subproject files. Any pointers?
My task looks like this :
task srcZip(type:Zip){
from project
exclude('**/*//*.iml', '*//*build/', '*//*dist')
exclude('*.zip')
destinationDir file('dist')
}

I build my source code zip from the root with a list of projects like so:
from ('./') {
include ([':proja',':projb',...].collect {
relativePath(project(it).projectDir) + '/'
})
}
It lets me select a sub-set of projects to archive. Don't forget to filter out some temp dirs like build/ and .gradle/.
relativePath(project.rootDir) or relativePath(rootProject.projectDir) should give you access to the top of your multi-project repo.

If you put the task in your root project build.gradle, you can do the following:
task srcZip(type: Zip) {
subprojects.each { project ->
from project.sourceSets.main.allSource
}
exclude('**/*//*.iml', '*//*build/', '*//*dist')
exclude('*.zip')
destinationDir file("dist")
baseName = "sourcecode"
}
This code assumes a few things:
1) All of your subprojects have source sets. If this is not the case, you will have to add a check.
2) There is no source code in the root project. If there is, and you wish to add it, you can add the following line:
from project.sourceSets.main.allSource

project.parent.projectDir worked for me . Initially when I used it, the task wouldn't stop and keep executing without giving any results but I tried in another system, it worked. Thanks for all the reponses but I think this is the perfect solution for me.

Related

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")
}
}

How to set the gradle outout folder for the kotlin2JS plugin?

I have a KotlinJs only project which I use official kotlin2js gradle to build, and no problems there.
How to setup the output folder, currently, the building of subproject will result in a build which locates inside the subproject folder, how to set it to somewhere else? I tried:
sourceSets {
main {
kotlin.outputDir = new File(‘./out/‘)
}
}
and
sourceSets {
main.kotlin.outputDir = new File(‘./out/’)
}
No luck.
What I want is to no matter how many subprojects are there, the output folder should be in some path like ./build/projectA and ./build/projectB, rather than all in their own folder. How to do this?
Currently, it's done through the task configuration, namely setting its kotlinOptions.outputFile:
compileKotlin2Js.kotlinOptions.outputFile = "out/output.js"
It's briefly mentioned in the tutorial: Getting Started with Kotlin and JavaScript with Gradle

gradle delete intellij IDEA out directories

I use Intellij IDEA and gradle for multiple module development environment .
Intellij by default create directory named "out" in gradle module for output compile path .
So some times i want to clean up whole project .
I configure build.gradle and override clean task for this subject , But not work .
task clean {
doLast {
delete 'build', 'target', fileTree("${projectDir}") { include '**/out' }
}
}
actually , I want to delete all subdirectories named "out" .
how can fix this ?
If you want to delete "out" section, you can use the task
task makePretty(type: Delete) {
delete 'out'
}
For more, you can go through this tutorial: How to delete an empty directory (or the directory with all contents recursively) in gradle?
Just to add to SkyWalkers answer, you can also bind your task to the java plug-in 'clean' task, so it runs with the standard clean, by making clean dependent on it:
task cleanBuildDir(type: Delete) {
delete "${projectDir}/out"
}
tasks.clean.dependsOn(cleanBuildDir)

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

Gradle embedded Project

Is it possible to embed multiple projects in a single build.gradle?
Something along the lines of:
project projX {
task a << {
}
}
project projY {
task a << {
}
}
Both within the same build.gradle. Is this possible?
I am asking this because I have multiple projects with equivalent task names which I want to execute from the root project, e.g.
gradle a
However the projects contain only automation tasks, which require no source files or resource files at all. Creating subdirectories just for the build.gradle files to be stored seems very ugly to me.
I could live with a solution with different .gradle files for each project, such as: build.gradle (root)
projA.gradle, projB.gradle within the same directory, however embedding project objects in the root build.gradle seems like the better option, if it is available.
project(":projX") { ... }
project(":projY") { ... }
Note that you still need a settings.gradle.
PS: It's not clear to me why you would want multiple projects in your case.

Resources