How to execute JavaExec multiple times in a single task using Gradle? - gradle

I have a task that runs a simple JavaExec.
What I cant seem to get working is the ability to run the JavaExec multiple times while iterating a Filetree object (containing the files) each of while I want to pass into the main JavaExec class one by one. Unfortunately the compiler or code generation tool as it is doesnot accept a directory as an arg so I need to pass the file as an arg per loop. Here's what I have:
task generateClasses(type: JavaExec) {
description = 'Generates Json Classes...'
classpath configurations.all
main = "org.apache.gora.compiler.Compiler"
FileTree tree = fileTree(dir: 'src/main')
tree.include '**/*.json'
tree.each {File file ->
println file
args = [ "src/main/json/$file.name", "$buildDir/generated-src/src/main/java" ]
}
}
compileJava.source generateClasses.outputs.files, sourceSets.main.java
From the above it works and I get all files listed but the JavaExec is called just the once on the very last file read.
How do I address the above? Please help.

How about using the project.javaexec method? See the API Documentation or the
DSL ref.
task generateClasses {
description = 'Generate Json Classes'
fileTree(dir: 'src/main', include:'**/*.json').each { file ->
doLast {
javaexec {
classpath configurations.all
main = 'org.apache.gora.compiler.Compiler'
args = ["src/main/json/$file.name", "$buildDir/generated-src/src/main/java"]
}
}
}
}

Related

In gradle, what is the difference betweeen javaexec and a JavaExec task?

For example, I can have a JavaExec task:
task javaExecCaseA(type: JavaExec) {
javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(11)
}
classpath = files("MySimpleProgram.jar")
}
or, inside a generic task:
task javaExecCaseB {
doLast {
javaexec {
classpath = files("MySimpleProgram.jar")
}
}
}
I haven't figured out how to specify the JavaLanguageVersion in the 2nd case (javaExecCaseB).
The bigger question though, is what is the difference?
I've tried various ways to set the version in javaExecCaseB, but I end up with an error like:
Could not set unknown property 'javaLauncher' for object of type org.gradle.process.internal.DefaultJavaExecAction_Decorated
I have found that the task is the gradle "JavaExec" task.
And the 2nd case, javaexec is a Project method.
I began this quest to find a way to run Java programs using a different JVM than gradle itself is using (set from an environment variable or command line when running gradle).
I was able to get it to work in both cases:
ext {
MyJvmVersion = 11
}
task SampleJavaExec1(type: JavaExec) {
// Example task for using a custom JVM version with a JavaExec task
javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(MyJvmVersion as int)
}
environment['JAVA_HOME'] = javaLauncher.get().metadata.installationPath.asFile.absolutePath
classpath = files("MySimpleProgram.jar")
}
task SampleJavaExec2 {
// Example task for using a custom JVM version with the javaexec method
doLast {
javaexec {
environment['JAVA_HOME'] = "C:\\Program Files\\AdoptOpenJDK\\jdk-11.0.10.9-hotspot"
executable = "C:\\Program Files\\AdoptOpenJDK\\jdk-11.0.10.9-hotspot\\bin\\java.exe"
classpath = files("MySimpleProgram.jar")
}
}
}
In the 2nd case, javaexec() doesn't appear to have a "javaLauncher".
Instead of hardcoding a path, I also found that I can use javaLauncher to find it for me by adding this code inside the javaexec{} block:
javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(MyJvmVersion as int)
}
environment['JAVA_HOME'] = javaLauncher.get().metadata.installationPath.asFile.absolutePath
This should invoke the auto download JVM resolution as well, but I've not tested that part.

Gradle: Not able to run zipTree in doFirst block

I am trying to explode some jar files in a doFirst block as follows -
task copyBinaries(type: Copy){
def Jar1 = ""
def Jar2 = ""
def Jar3 = ""
doFirst {
Jar1 = configurations.Lib1.singleFile
Jar2 = configurations.Lib2.singleFile
Jar3 = configurations.Lib3.singleFile
}
inputs.files configurations.Lib1
inputs.files configurations.Lib2
inputs.files configurations.Lib3
from(zipTree(file(Jar1))) {
into('jar_folder1')
}
from(zipTree(file(Jar2))) {
into('jar_folder2')
}
from(zipTree(file(Jar3))) {
into('jar_folder3')
}
into('build/libs/')
}
}
In order the avoid resolution of dependencies in the configuration phase, I am extracting the file names from the configurations in the doFirst block. The problem is, since the Copy task needs the source and destination of copy during configuration phase, gradle sees Jar1, Jar2, Jar3 as empty strings and throws and error.
I am looking for a way to let the Copy task receive arguments (source file) during execution phase (after doFirst block is executed).
How can I address this situation? Thanks.
I've had a similar situation where I had to copy from a path that was created as output of another one.
I solved this with just declaring the copy logic in my task's doLast block:
task copyStuff {
doLast {
copy {
from zipTree('path/to/archive')
into 'destination/dir'
}
}
}
You just have to make sure, that your task runs after the task that creates the needed outputs with using dependsOn for example.

Run executable jar with parameters from gradle

I wish to run executable jar from my gradle file. I have tied:
task runJar(dependsOn:jar) << {
javaexec {
main="-jar"; args "C:/Development/AndroidStudioProjects/AndroidDev/Test.jar"
}
}
But I get "Could not find property 'jar' on project ':MyProj"
I also tried:
task runScheduleReader << {
javaexec {
main = "MainClass"
classpath = "C:/Development/AndroidStudioProjects/AndroidDev/Test.jar"
args('1')
}
}
I am relatively new to groovy, can you please help me with that?
P.S... I put those function outside of android {}
In your first code snippet dependsOn in task declaration means, that task runJar should be executed only after the jar task. Exception you get, says, that your current project doesn't have such a task. So, if you really don't need to execute jar task just before, you can simply not declere this task dependency:
task runJar() << {
javaexec {
main="-jar"; args "C:/Development/AndroidStudioProjects/AndroidDev/Test.jar"
}
}
Though, this is a little bit strange case, when you have to execute some jar without relative path, this solution should work.
The second snippet should pass the jar as the argument too, but this time, it should be an arguments array, something like this:
task runScheduleReader() << {
javaexec {
main="-jar";
args = [
"C:/Development/AndroidStudioProjects/AndroidDev/Test.jar",
"1"
]
}
}

how to download external files in gradle?

I have a gradle project which requires some data files available somewhere on the internet using http. The goal is that this immutable remote file is pulled once upon first build. Subsequent build should not download again.
How can I instruct gradle to fetch the given file to a local directory?
I've tried
task fetch(type:Copy) {
from 'http://<myurl>'
into 'data'
}
but it seems that copy task type cannot deal with http.
Bonus question: is there a way to resume a previously aborted/interrupted download just like wget -c does?
How about just:
def f = new File('the file path')
if (!f.exists()) {
new URL('the url').withInputStream{ i -> f.withOutputStream{ it << i }}
}
You could probably use the Ant task Get for this. I believe this Ant task does not support resuming a download.
In order to do so, you can create a custom task with name MyDownload. That can be any class name basically. This custom task defines inputs and outputs that determine whether the task need to be executed. For example if the file was already downloaded to the specified directory then the task is marked UP-TO-DATE. Internally, this custom task uses the Ant task Get via the built-in AntBuilder.
With this custom task in place, you can create a new enhanced task of type MyDownload (your custom task class). This task set the input and output properties. If you want this task to be executed, hook it up to the task you usually run via task dependencies (dependsOn method). The following code snippet should give you the idea:
task downloadSomething(type: MyDownload) {
sourceUrl = 'http://www.someurl.com/my.zip'
target = new File('data')
}
someOtherTask.dependsOn downloadSomething
class MyDownload extends DefaultTask {
#Input
String sourceUrl
#OutputFile
File target
#TaskAction
void download() {
ant.get(src: sourceUrl, dest: target)
}
}
Try like that:
plugins {
id "de.undercouch.download" version "1.2"
}
apply plugin: 'java'
apply plugin: 'de.undercouch.download'
import de.undercouch.gradle.tasks.download.Download
task downloadFile(type: Download) {
src 'http://localhost:8081/example/test-jar-test_1.jar'
dest 'localDir'
}
You can check more here: https://github.com/michel-kraemer/gradle-download-task
For me works fine..
The suggestion in Ben Manes's comment has the advantage that it can take advantage of maven coordinates and maven dependency resolution. For example, for downloading a Derby jar:
Define a new configuration:
configurations {
derby
}
In the dependencies section, add a line for the custom configuration
dependencies {
derby "org.apache.derby:derby:10.12.1.1"
}
Then you can add a task which will pull down the right files when needed (while taking advantage of the maven cache):
task deployDependencies() << {
String derbyDir = "${some.dir}/derby"
new File(derbyDir).mkdirs();
configurations.derby.resolve().each { file ->
//Copy the file to the desired location
copy {
from file
into derbyDir
// Strip off version numbers
rename '(.+)-[\\.0-9]+\\.(.+)', '$1.$2'
}
}
}
(I learned this from https://jiraaya.wordpress.com/2014/06/05/download-non-jar-dependency-in-gradle/).
Using following plugin:
plugins {
id "de.undercouch.download" version "3.4.3"
}
For a task which has the purpose of only downloading
task downloadFile(type: Download) {
src DownloadURL
dest destDir
}
For including download option into your task:
download {
src DownloadURL
dest destDir
}
For including download option with multiple downloads into your task:
task downloadFromURLs(){
download {
src ([
DownloadURL1,
DownloadURL2,
DownloadURL3
])
dest destDir
}
}
Hope it helped :)
just now ran into post on upcoming download task on gradle forum.
Looks like the perfect solution to me.. Not (yet) available in an official gradle release though
Kotlin Version of #Benjamin's Answer
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath("com.android.tools.build:gradle:4.0.1")
}
}
tasks.register("downloadPdf"){
val path = "myfile.pdf"
val sourceUrl = "https://file-examples-com.github.io/uploads/2017/10/file-sample_150kB.pdf"
download(sourceUrl,path)
}
fun download(url : String, path : String){
val destFile = File(path)
ant.invokeMethod("get", mapOf("src" to url, "dest" to destFile))
}

Gradle aggregation task

In my gradle scripts, I've built a task that runs a java process. This process depends on a target property. The task is defined by:
task('bulk', type: JavaExec, dependsOn: 'classes', description : 'Bulk data import on a target (defined by -Ptarget=[event|member|...]]') {
//available imports
ext{
event = relativePath('src/main/scripts/events.csv')
member = relativePath('src/main/scripts/member.csv')
membership = relativePath('src/main/scripts/membership.csv')
}
//check the target is set
doFirst {
if(!project.hasProperty('target')){
println "\nUsage:"
println "\tgradle bulk -Ptarget=[event|member|...]"
println "where target is : "
bulk.ext.each{ println it }
throw new GradleException('Target argument required')
} else {
println "\nBulk import of $target\n"
}
}
main = 'org.yajug.users.bulkimport.BulkImport'
classpath = sourceSets.main.runtimeClasspath
if(project.hasProperty('target')){
bulk{
args target
args bulk.ext[target]
debug false
}
}
}
And to run it:
gradle bulk -Ptarget=event
It's working fine, but know I have to run this process for different targets:
gradle bulk -Ptarget=event
gradle bulk -Ptarget=member
gradle bulk -Ptarget=membership
...
How can I group all these calls into an other single task with the gradle's dependency model ? (I know the list of targets)
SOLUTION
task bulk;
['event','member','membership'].each {target ->
task("bulk${target}", type: JavaExec, dependsOn: 'classes', description : "Bulk data import of ${target}s") {
//available imports
ext{
event = relativePath('src/main/scripts/events.csv')
member = relativePath('src/main/scripts/member.csv')
membership = relativePath('src/main/scripts/membership.csv')
}
//check the target is set
doFirst {
println "\nBulk import of $target\n"
}
main = 'org.yajug.users.bulkimport.BulkImport'
classpath = sourceSets.main.runtimeClasspath
args target
args ext[target]
debug false
}
bulk.dependsOn("bulk${target}")
}
How can I group all these calls into an other single task with the gradle's dependency model ?
You can't, because a task (instance) will be executed at most once per build. Instead, the way to go is to declare multiple task instances. You could do this by putting the task declaration in a loop, putting it in a method and calling it multiple times, or by writing a task class and instantiating it multiple times. Then you'll add one other task that depends on all bulk tasks. Assuming the execution order between bulk tasks is irrelevant, that's it.
By adding a helper method or two, you can create a nice little API around this, to improve readability and allow reuse in other places.
Another way to tackle this is with a task rule. You can read more on task rules in the Gradle User Guide.
What about creating a -Ptarget=all, and deal with that specific case in the build.gradle file. This could be done as:
['event','member','membership'].each { t ->
task("bulk${t}", ...) {
onlyIf project.hasProperty("target") && (project.getProperty("target").equals(t) || project.getProperty("target").equals("all"))
args target
args bulk.ext[target]
debug false
}
}

Resources