Gradle task Orgnaization

(Sharad Goyal) #1

How I can organize my task in gradle script? The only change in the task is define the arguments

def arguments_sjc = ["-c", “a.xml”, “-n”, “test”]
def arguments_sjcmto = ["-c", “b.xml”, “-n”, “test”]

task(run1, type: JavaExec) {
main = 'main.Startup’
args = arguments_sjc
classpath = sourceSets.main.runtimeClasspath
jvmArgs = jvmArguments
systemProperty ‘simple.message’, 'Hello '

task(run2, type: JavaExec) {
main = 'main.Startup2’
args = arguments_sjcmto
classpath = sourceSets.main.runtimeClasspath
jvmArgs = jvmArguments
systemProperty ‘simple.message’, 'Hello '

(Janito Vaqueiro Ferreira Filho) #2

I think creating a custom Task here would be a good idea. Something similar to this (haven’t tested it though):

task(run1, type: MyRunTask) {
    main = 'main.Startup'
    xmlFile = 'a.xml'

task(run2, type: MyRunTask) {
    main = 'main.Startup2'
    xmlFile = 'b.xml'

class MyRunTask extends JavaExec {
     MyRunTask() {
         classpath = sourceSets.main.runtimeClasspath
         jvmArgs = jvmArguments
         systemProperty 'simple.message', 'Hello '
     void setXmlFile(String xmlFile) {
         args = ["-c", xmlFile, "-n", "test"]

(Sharad Goyal) #3

How do I run this task? In addition the a.xml, b.xml are files from resources directory.
s it possible to define an array and get all the a.zml, b.xml, c.xml from resources directory?

So if I want to run the task I should be able to do like this
gradle atask
gradle btask
gradle ctask

(Janito Vaqueiro Ferreira Filho) #4

I’m sorry, I didn’t understand your question. Do you mean:

  1. You want to automatically generate the tasks for all xml files in a known sub-directory?
  2. Create a run task that calls all known MyRunTasks?

(Sharad Goyal) #5
  1. Yes I would like to automatically generate the tasks for all xml files in a known sub-directory.
  2. Yes but I should be able to run it individually as well as combined. It means

runAll must read all xml and run the task
runA must be able to run task with a.xml
runB must be able to run task with b.xml
runC must be able to run task with c.xml

  • runAll must read all xml and run the task with a.xml, b.xml and c.xml

(Janito Vaqueiro Ferreira Filho) #6

Ok, I think I’m starting to understand.

For number one, there are probably a few different ways to do it. The simplest would be to include something like the following in the build.gradle file:

fileTree('my/path/to/resources').include('**/*.xml').each { file ->
    fileNameWithoutExtension ='.').with {it != -1 ?[0..<it] :}

    task('run$fileNameWithoutExtension', type: MyRunTask) {
        main = 'main.Startup'
        xmlFile =

For number two, you would have to collect all tasks into a runAll task. Therefore we need to configure a runAll task that depends on all created tasks. So based on the previous example, we can extend it with something like:

task runAll

fileTree('my/path/to/resources').include('**/*.xml').each { file ->
    def fileNameWithoutExtension ='.').with {it != -1 ?[0..<it] :}
    def taskName = "run$fileNameWithoutExtension"
    task(taskName, type: MyRunTask) {
        main = 'main.Startup'
        xmlFile =
    runAll.dependsOn taskName

This is probably not the most elegant way to do things, but I think it should work. Hope this helps :slight_smile:

(Sharad Goyal) #7

Ok I have tried but looks like I am missing something. It returns the followg
Task ‘runa’ not found in root project ‘my project’.

Please help

Thanks again and appreciate your help

class MyRunTask extends JavaExec { MyRunTask() { main = 'main.HelloWorld' classPath = sourceSets.main.runtimeClasspath jvmArgs = jvmArguments systemProperty 'simple.message', 'Hello ' }
 void setXmlFile(String xmlFile) {
     args = ["-c", xmlFile, "-n", "test"]


fileTree(‘config/spring’).include(’**/.xml’).each { file ->
fileNameWithoutExtension =’.’).with {it != -1 ?[0…<it] :}
def taskName = "run$fileNameWithoutExtension"
task(taskName, type: MyRunTask) {
xmlFile =

config/spring/.xml has

(Janito Vaqueiro Ferreira Filho) #8

Okay, I think we’re going to need to add some debug information to try to find the problem.

First, add System.err.println("Found file: $file") as the first line inside the fileTree closure (i.e., before the fileNameWithoutExtension line).

If when running the build it successfully prints both xml files, then the problem is task creation. If not, the problem is probably in the fileTree line. I can see that the '**/*.xml' specifier is different in your post, but it might be a formatting error on the post.

To find out if the tasks were created, you can run ./gradlew tasks and see if runa and runb appear. If not, add a System.err.println("Creating task $taskName") before the line creating the task to see if the name is as expected.

Let me know what’s happening, if the files are found correctly and if any tasks were created.

BTW: if you prefer, you can change it to generate runA and runB instead of runa and runb by changing the taskName to be def taskName = "run${fileNameWithoutExtension.capitalize()}"

(Sharad Goyal) #9

It is not printing anything

indent preformatted text by 4 spaces

fileTree(‘config/spring’).include(’**/*.xml’).each { file ->
System.err.println(“Found file: $file”)
def fileNameWithoutExtension =’.’).with {it != -1 ?[0…<it] :}
def taskName = "run${fileNameWithoutExtension.capitalize()}"
task(taskName, type: MyRunTask) {
System.err.println(“Creating task $taskName”)
xmlFile =

(Sharad Goyal) #10

I am getting the following error

Received result Failure[value=org.gradle.initialization.ReportedException: org.gradle.internal.exceptions.LocationAwareException: A problem was found with the configuration of task ':startScripts

“No value has been specified for property ‘mainClassName’.”

(Janito Vaqueiro Ferreira Filho) #11

Ok, the problem seems to be that the main property isn’t set for the generated tasks. I think you need to add main = 'main.Startup' or something similar before (or after) the xmlFile = line.

Sorry I didn’t see that before :confused:

(Sharad Goyal) #12

It did not work while specifying the main class name before xml file, but it works when I specified
mainClassName = ‘main.Startup2’ as separate line of entry in my build.gradle file.

However, I am still not able to see all the tasks even though my build is successfully this time.

gradle tasks

It did not return the list of expected tasks.
Please help

(Janito Vaqueiro Ferreira Filho) #13

If it’s not printing anything, it might not be finding the files. Let’s check if the path is correct:

fileTree('config/spring').each { System.err.println("Found file: $it") }

This should print all files in the directory. If it still doesn’t print anything, the (relative) path is probably incorrect. If that’s the case, try fileTree(new File(".")) { System.err.println("Found file: $it") } to see the files in the current directory. Make sure it doesn’t print anything unexpected.

Just in case it’s not a post formatting error (*/* becomes an italic / if it’s not marked as code), make sure the include call is .include('**/*.xml'). The way it’s currently displayed would only get files called .xml, not with the extension .xml.

(Sharad Goyal) #14

The files are included as a jar dependencies in another separate project. Does that can create an issue?

(Janito Vaqueiro Ferreira Filho) #15

I don’t think I understand. Are the files inside an external Jar file, or are they added to a Jar during the build?

(Sharad Goyal) #16

They have been added to a jar during the build.

(Janito Vaqueiro Ferreira Filho) #17

In that case, it shouldn’t be a problem, as long as the path config/spring is the path of the files in the build. But if it’s the path inside the Jar, you need to change the file tree to use the build path (for example: src/main/res/config/spring).

(Sharad Goyal) #18

I am so sorry. Looks like I misunderstood your question.
The resources are in external jar which has a folder config/spring. It means the script folder does not have these files. They have been referred from an external jar which is included in a build

(Janito Vaqueiro Ferreira Filho) #19

Ah, ok :slight_smile:

In that case we have to change the fileTree to zipTree('path/to/lib.jar'). This should work as long as you have the Jar file locally.

If the Jar is downloaded by Gradle, you can do a quick check to see if this works by using the path of the downloaded Jar (should be somewhere either in .gradle or in a hidden directory in your home directory). The problem is that this path can change. Let me know if you need to use the Jar downloaded by Gradle, because it’s a more elaborate work.

I’ll be out for about an hour, but I’ll check the post as soon as I get back :slight_smile:

(Sharad Goyal) #20

Is it possible to define maven dependency? I do not want to hard code the jar path in my gradle build file.