Copy all jars from subprojects to root project’s bin directory - gradle

I'd like to copy every jar generated from subprojects from their /bin/libs dir to the root project's bin dir. Unfortunately, my current code just copies everything from the subprojects into that directory:
task muleapp(type: Copy) {
from '.'
include '**/bin/libs/*.jar'
into 'bin'
}
How can I achieve the desired functionality?

Try this type of task:
task muleapp(type: Copy) {
def jars=[]
subprojects.each {
jars+=it.libsDir
}
from jars
into 'bin'
}

Related

Delete directory content from Gradle

I use this Gradle configuration to copy generated asciidoc files.
But I need to delete old outdated files also.
task copyDocument(type: Copy) {
dependsOn bootJar
from file("build/docs/asciidoc/")
into file("src/main/resources/static/")
}
How I can delete the content of directory src/main/resources/static/ before I paste the new generated files?
task copyDocument(type: Copy) {
dependsOn bootJar
from file("build/docs/asciidoc/")
into file("src/main/resources/static/")
doFirst {
delete "build/folder"
}
}

How to deal with relative paths in Gradle composite builds?

I have a Gradle composite build with the following directory structure:
compositebuildroot/
build.gradle
settings.gradle
componentbuild/
build.gradle
settings.gradle
testfile.txt
with compositebuildroot/build.gradle containing
task runComponentBuildSampleTask {
dependsOn gradle.includedBuild('componentbuild').task(':sampleTask')
}
, compositebuildroot/settings.gradle containing
rootProject.name = 'compositebuildroot'
includeBuild 'componentbuild'
, compositebuildroot/componentbuild/build.gradle containing
task sampleTask {
doLast {
println "System.getProperty(\"user.dir\"): " + System.getProperty("user.dir")
println new File("testfile.txt").text
}
}
, and compositebuildroot/componentbuild/settings.gradle containing
rootProject.name = 'componentbuild'
.
When I execute gradle sampleTask from the componentbuild directory, I get the absolute path of the componentbuild directory printed out, followed by the contents of testfile.txt (which is what I want/expect). However, when I execute gradle runComponentBuildSampleTask from the compositebuildroot directory, I get the absolute path of the compositebuildroot directory followed by a java.io.FileNotFoundException for the testfile.txt file.
How can I make gradle runComponentBuildSampleTask from compositebuildroot produce whatever output gradle sampleTask from componentbuild does? Is there some way to tell gradle to run component build tasks as if the current directory is that of the component build's settings.gradle file?
Never use new File(...)
Always use file(...)
See Project.file(Object)

processResources task in gradle doesn't copy a file

While processing resources in a gradle build, is it possible to copy certain special resources to a target directory?
processResources {
println "Project Dir : $projectDir"
copy {
from ('${projectDir}/../../../project-abc/src/main/resources/SkipColumns.properties')
into ('${projectDir}/target/classes/META-INF/project-abc')
}
}
gradle processResources
When I run above command, the build is successful. But it doesn't copy SkipColumns file.
Removing parentheses worked.
copy {
from ("$projectDir/../../../project-abc/src/main/resources/SkipColumns.properties")
into ("$projectDir/target/classes/META-INF/project-abc")
}

Gradle copy resources, processResources

I am trying to copy 2 folders from my project folder into the jar.
When I use following settings Gradle copies the files inside the folders, not the folders themself:
processResources {
from 'public'
from 'data'
}
What I need is to have folder "projectDir/data" copyied into "my.jar/data".
There is a method of 'from' that takes a closure with CopySpec.
I would recommend using this instead of either breaking up into separate tasks or restructuring your layout.
processResources {
from('data') {into 'data'}
from('publicFolder') {into 'publicFolder'}
}
The copy spec in Gradle copies contents of a folder if the specified path in from is a folder.
One solution is to specify the target folder in the jar using the into directive.
processResources {
from 'data'
from 'publicFolder'
}
task data(type: Copy) {
from 'data' into 'data'
}
task publicFolder(type: Copy) {
from 'public' into 'public'
}
This will put the contents or the original folders into the folder with the same name within the jar. But as you can see, you'll need to repeat the folder names within the into closure.
Another workaround is to have a parent folder for these resource folders and use this parent folder in the copy spec.
Here's one way you could structure the resource folders:
<project-folder>
|
--> res
|
--> public
--> data
Then in the copy spec of processResources, you can do the following:
processResources {
from 'res'
}
This will create folders public and data in the final jar file as you want.

How to add clean task - Task 'clean' not found

I am using https://github.com/eriwen/gradle-js-plugin and i would like to be able run task 'clean'. When i run 'gradle -d clean', it gives the following error
Task 'clean' not found in root project
To my understanding, gradle comes with task - 'clean', however the gradles-js-plugin doesn't seem to support that at this time or something. How do i add the task 'clean'?
Here is my build.gradle:
// Pull the plugin from Maven Central
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.eriwen:gradle-js-plugin:1.5.0'
}
}
// Invoke the plugin
apply plugin: 'js'
def jsSrcDir = 'public/js'
javascript.source {
dev {
js {
srcDir jsSrcDir
include "*.js"
exclude "*.min.js"
}
}
prod {
js {
srcDir jsSrcDir
include "*.min.js"
}
}
}
combineJs{
source = fileTree(javascript.source.dev.js.files)
dest = file("${buildDir}/all.js")
}
The clean task is introduced by the base plugin. So you need to apply this plugin to get the clean task and the clean task rules for cleaning up specific task outputs:
apply plugin:'base'
Make sure you are running the
gradle -d clean
command from your project home path.
According to this guideline, the syntax is as follows:
plugins {
id "base"
}
Just run it from your /android/ folder, i.e navigate to your android folder and run the command from there in the project dir.
you can also add
plugins {
id "base"
}
to your gradle file
On android I added settings.gradle file with the a single line
include ':library'

Resources