gradle delete intellij IDEA out directories - gradle

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)

Related

gradle does not create build folder

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

Use build.finalizedBy on each subproject with Gradle kotlin

I want to run a specific task after EVERY build of my subprojects. I can go into each of my subprojects build.gradle.kts file and add the following
tasks.build {
finalizedBy("afterbuildtask")
}
However, this should be possible to do in my root project build.gradle.kts file right? I tried it by doing the following:
subprojects {
this.tasks.findByName("build")?.dependsOn("afterbuildtask")
}
But nothing happens. How can I achieve this?
You can't programatically execute tasks from other tasks in newer versions of Gradle.
Instead, you are supposed to declare task dependencies and Gradle will ensure they get executed in the correct order. But I think it's not what you want
Alternatively, you could move your logic into the doLast block in the build task. eg:
build {
doLast {
println("Copying...")
copy {
from 'source'
into 'target'
include '*.war'
}
println("completed!")
}
}
good coding! ¯_(ツ)_/¯

How to create a war file from a directory in gradle

I am writing a task to unzip a war file, remove some jars and then create a war from extracted folder.
task unzipWar(type: Copy){
println 'unzipping the war'
def warFile = file("${buildDir}/temp/libs/webapps/service-app.war")
def warOutputDir = file("$buildDir/wartemp")
from zipTree(warFile)
into warOutputDir
}
task deleteJars(type: Delete){
println 'deleting the logging jars'
file("$buildDir/wartemp/WEB-INF/lib/slf4j-api-1.7.5.jar").delete();
file("$buildDir/wartemp/WEB-INF/lib/logback-classic-1.1.7.jar").delete();
file("$buildDir/wartemp/WEB-INF/lib/logback-core-1.1.7.jar").delete();
}
task createWar(type: War){
destinationDir = file("$buildDir")
baseName = "service-app"
from "$buildDir/wartemp"
dependsOn deleteJars
}
For some reason, the jars are not getting deleted and the war file is getting created which only includes MANIFEST.MF and nothing else. What am I missing here?
First thing to note, is that your createWar task depends on deleteJarstask, but deleteJars doesn't depend on unzipWar. It seems, that if you call the createWar task it won't call unzipWar task and there will be nothing to copy or delete. Note that you have a MANIFEST.MF file, because it was generated by createWar task.
And the second thing is that you are trying to delete some files in the configuration stage of the build, though your unzipWar will do it's job in the execution phase. So your delete task will try to delete this files just before they are even unzipped. You can read about build lifecycle in the official userguide. So you need to rewrite your deleteJars task, to configure it properly. Take a look into the docs, it has an example how to do it.
So if you call a
file("$buildDir/wartemp/WEB-INF/lib/slf4j-api-1.7.5.jar").delete();
it tries to delete your files at the time it's called, because it's not a task property, but an action at the configuration.
To configure it you have to do something like:
task deleteJars(type: Delete) {
delete "$buildDir/wartemp/WEB-INF/lib/slf4j-api-1.7.5.jar", "$buildDir/wartemp/WEB-INF/lib/logback-classic-1.1.7.jar", "$buildDir/wartemp/WEB-INF/lib/logback-core-1.1.7.jar"
}

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.

how to copy all source jars in the dependencies section to a directory in gradle

I can copy all the jars in dependency section for "compile" configuration like so
task('copyJars') {
ext.collection = files { genLibDir.listFiles() }
delete ext.collection
copy { from configurations.compile into genLibDir }
}
but how do I copy their source jar files somewhere?
thanks,
Dean
As of Gradle 1.0, I don't know of an easy way to deal with third-party sources Jars. You could add them as explicit dependencies (to a separate configuration) or maybe crawl the Gradle cache.
By the way, delete ext.collection is in the wrong spot. It will be executed in the configuration phase, and will delete files no matter which tasks are going to be executed. (Also listFiles() will get invoked for every build.)
Also, a task whose main purpose is copying should alway use the Copy task type rather than the copy method.

Resources