Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use in doLast method #8

Closed
tofi86 opened this issue Oct 18, 2017 · 6 comments
Closed

use in doLast method #8

tofi86 opened this issue Oct 18, 2017 · 6 comments

Comments

@tofi86
Copy link

tofi86 commented Oct 18, 2017

Hi, I would like to call the xslt task from within another tasks doLast method to be able to change the input directory while iterating over testfiles and performing other tasks.

This does not seem to work at the moment, the xslt task just isn't being executed.
Do you have an idea why?

task runTests(dependsOn: lessCompile) {

  // test this data
  currentTestDir = 'data1'

  // run the transformation and rendering
  doLast {

    // transform test_dev.xml to test_dev_prep.xml
    xslt {
      input file('test/' + currentTestDir + '/test_dev.xml')
      stylesheet file('xslt/prepare.xsl')
      output file('test/' + currentTestDir + '/test_dev_prep.xml')
    }

    exec {
      workingDir '.'
      commandLine 'cmd', ....
    }

    javaexec {
      classpath = buildscript.configurations.classpath
      main = '.....'
      args = ['-headless', '-f1', 'test/' + currentTestDir + '/test_dev_expected.pdf', '-f2', 'test/' + currentTestDir + '/test_dev.pdf']
    }
  }
}

The exec task fails because the output of the xslt task is missing.
If the runTests task depends on a single xslt task, it works well, but I loose the option to provide a specific directory to the xslt task because the task cannot be called with parameters.

Thanks!

@eerohele
Copy link
Owner

eerohele commented Oct 19, 2017

I haven't kept up with Gradle's recent developments, but it seems that executing a task in a doLast block isn't officially supported. So I'm not sure there's a way for me to modify the plugin so that your example will work. If there is, I don't know how.

Can you define currentTestDir outside of the runTests task?

@tofi86
Copy link
Author

tofi86 commented Oct 19, 2017

Okay, thanks.

Sure, I could define currentTestDir outside of the runTests task and it works, but this is just a break-down of my full task where I iterate over dozens of folders in the test/ dir and currentTestDir is set programmatically by iteration and then different tasks are performed in every subdir.

Maybe my Gradle skills are just too bad to get this done :-(

@eerohele
Copy link
Owner

eerohele commented Oct 19, 2017

My Gradle skills certainly aren't anything to brag about, but one option is to do something like this:

import com.github.eerohele.SaxonXsltTask

// The list of things (folders) you're iterating
def things = ['foo', 'bar', 'baz']

things.forEach { thing ->
    task "transform-${thing}"(type: SaxonXsltTask) {
        stylesheet 'my-stylesheet.xsl'
        input file("$thing-input.xml")
        output "build/$thing-output.xml"
    }

    task "test-${thing}"(dependsOn: "transform-${thing}") {
        // insert whatever the "test" task should actually do here
        doLast {
            println "Hello, $thing!"
        }
    }
}

task "test-all"(dependsOn: things.collect { "test-${it}" } )

Then:

$ gradle test-all

Of course, I don't know enough about what you're trying to do to know whether that'll actually work.

@tofi86
Copy link
Author

tofi86 commented Oct 23, 2017

thanks, that's probably a good way to go. I'll try it...

@tofi86 tofi86 closed this as completed Oct 23, 2017
@tofi86
Copy link
Author

tofi86 commented Oct 23, 2017

works, thanks!

@eerohele
Copy link
Owner

Glad to hear it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants