gradle does not create build folder - gradle

I have a simple gradle project, created by gradle init
The build.gradle file contains the next content:
plugins {
id 'base'
}
tasks.register('copyFile', Copy) {
from 'desc'
into "$buildDir/desc"
}
running the copyFile or the build task doesn't create a build folder

I can not reproduce the issue you are having, Am using gradle 6.9 , And tested with 7.5
I tried the following :
Having desc folder, With any folder/files inside
calling gradle copyFile
This resulted in success and copyed all the files and folders into /build/desc/
The task will create both build folder + desc folder but it will only work if desc is not empty.
Otherwise, It will not execute any commands as there are no files nor folders in /desc folder
So, Just make sure the /desc is not empty, and call the function with gradle copyFile
If you want to link this task with gradle build , you can add the following line to your code.
tasks.named("build") { finalizedBy("copyFile") }

Related

Create a directory in Debian file creation task

I have the following gradle task that I use to create a debian archive:
val packDeb by tasks.registering(Deb::class) {
mkdir("/var/lib/salam/")
}
But I get the following error when I run ./gradlew build
org.gradle.api.UncheckedIOException: Failed to create directory '/var/lib/salam'
What am I doing wrong here?
The mkdir method that you are using is not part of the Deb task (but rather comes from the project object). The method is called at the time your Gradle project is configured, i.e., Gradle tries to create that directory at the time when Gradle starts up. This most likely fails because the user account with which you run Gradle does not have write permissions under /var/lib.
To create that directory when installing the DEB package, you could do something like this:
plugins {
id("nebula.deb") version "6.2.0"
}
import com.netflix.gradle.plugins.deb.Deb
val packDeb by tasks.registering(Deb::class) {
into("/")
// TODO assumes that the (non-empty) "salam" dir is prepared in your
// project dir
from("salam") {
into("var/lib/salam")
}
}

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

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"

Create zip of the source code in 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.

Resources